diff options
Diffstat (limited to 'src/Http/Redirector.php')
-rw-r--r-- | src/Http/Redirector.php | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/Http/Redirector.php b/src/Http/Redirector.php new file mode 100644 index 00000000..4149a8ec --- /dev/null +++ b/src/Http/Redirector.php @@ -0,0 +1,55 @@ +<?php + +namespace Engelsystem\Http; + +class Redirector +{ + /** @var Request */ + protected $request; + + /** @var Response */ + protected $response; + + /** + * @param Request $request + * @param Response $response + */ + public function __construct(Request $request, Response $response) + { + $this->request = $request; + $this->response = $response; + } + + /** + * @param string $path + * @param int $status + * @param array $headers + * @return Response + */ + public function to(string $path, int $status = 302, array $headers = []): Response + { + return $this->response->redirectTo($path, $status, $headers); + } + + /** + * @param int $status + * @param array $headers + * @return Response + */ + public function back(int $status = 302, array $headers = []): Response + { + return $this->to($this->getPreviousUrl(), $status, $headers); + } + + /** + * @return string + */ + protected function getPreviousUrl(): string + { + if ($header = $this->request->getHeader('referer')) { + return array_pop($header); + } + + return '/'; + } +} |