summaryrefslogtreecommitdiff
path: root/src/Http/Redirector.php
blob: 4149a8ec672ac028aedc8ec68d808acec8a65e62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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 '/';
    }
}