Completed
Pull Request — master (#16)
by Flo
07:29
created

Http::triggerRedirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
/**
3
 * Class Uri
4
 *
5
 * @package Faulancer\Http
6
 * @author Florian Knapp <[email protected]>
7
 */
8
namespace Faulancer\Http;
9
10
use Faulancer\Exception\InvalidArgumentException;
11
12
/**
13
 * Class Uri
14
 */
15
class Http extends AbstractHttp
16
{
17
18
    /**
19
     * Redirect to specific uri path
20
     *
21
     * @param string  $location The path as string
22
     * @param integer $code     The status code
23
     * @return mixed
24
     * @throws InvalidArgumentException
25
     */
26
    public function redirect(string $location, int $code = 301)
27
    {
28
        if (in_array($code, array_keys(Response::HTTP_STATUS_CODES))) {
29
            $this->triggerRedirect($location, $code);
30
            return true;
31
        }
32
33
        throw new InvalidArgumentException('Target url is invalid or status code is unknown');
34
    }
35
36
    /**
37
     * Workaround to mock this method in phpunit
38
     *
39
     * @param string $location
40
     * @param int    $code
41
     *
42
     * @codeCoverageIgnore
43
     * @return void
44
     */
45
    protected function triggerRedirect($location = '/', $code = 301)
46
    {
47
        header('HTTP/2 ' . $code . ' ' . Response::HTTP_STATUS_CODES[$code]);
48
        header('Location: ' .  $location);
49
50
        exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method triggerRedirect() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
51
    }
52
53
}