Http   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

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