Passed
Push — master ( d15981...86c3d2 )
by Gabor
03:48
created

Result::setMatchedMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Router;
15
16
use InvalidArgumentException;
17
18
/**
19
 * Class Result.
20
 */
21
class Result
22
{
23
    public const CODE_FOUND = 200;
24
    public const CODE_NOT_FOUND = 404;
25
    public const CODE_BAD_METHOD = 405;
26
27
    /** @var int */
28
    private $status;
29
    /** @var string */
30
    private $matchedMiddleware;
31
    /** @var array */
32
    private $statusReason = [
33
        self::CODE_FOUND      => 'Resource found.',
34
        self::CODE_NOT_FOUND  => 'The requested resource cannot be found.',
35
        self::CODE_BAD_METHOD => 'Bad request method was used by the client.'
36
    ];
37
    /** @var array */
38
    private $parameters;
39
40
    /**
41
     * Sets status code.
42
     *
43
     * @param int $status
44
     * @throws InvalidArgumentException
45
     * @return Result
46
     */
47 8
    public function setStatus(int $status) : Result
48
    {
49 8
        if (!isset($this->statusReason[$status])) {
50 1
            throw new InvalidArgumentException(sprintf('The parameter "%s" is not a valid router status.', $status));
51
        }
52
53 7
        $this->status = $status;
54
55 7
        return $this;
56
    }
57
58
    /**
59
     * Gets status code.
60
     *
61
     * @return int
62
     */
63 7
    public function getStatus() : int
64
    {
65 7
        return $this->status;
66
    }
67
68
    /**
69
     * Gets reason for the status set.
70
     *
71
     * @return string
72
     */
73 3
    public function getStatusReason() : string
74
    {
75 3
        return $this->statusReason[$this->status] ?? '';
76
    }
77
78
    /**
79
     * Sets matched middleware.
80
     *
81
     * @param string $matchedMiddleware
82
     * @return Result
83
     */
84 5
    public function setMatchedMiddleware(string $matchedMiddleware)
85
    {
86 5
        $this->matchedMiddleware = $matchedMiddleware;
87
88 5
        return $this;
89
    }
90
91
    /**
92
     * Gets matched middleware.
93
     *
94
     * @return string
95
     */
96 5
    public function getMatchedMiddleware() : string
97
    {
98 5
        return $this->matchedMiddleware;
99
    }
100
101
    /**
102
     * Sets the parameters.
103
     *
104
     * @param array $parameters
105
     * @return Result
106
     */
107 5
    public function setParameters(array $parameters) : Result
108
    {
109 5
        $this->parameters = $parameters;
110
111 5
        return $this;
112
    }
113
114
    /**
115
     * Gets the parameters.
116
     *
117
     * @return array
118
     */
119 3
    public function getParameters() : array
120
    {
121 3
        return $this->parameters;
122
    }
123
}
124