Passed
Push — master ( 8220b9...00b675 )
by Gabor
03:04
created

Result::setResource()   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\Result;
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 string */
32
    private $resource;
33
    /** @var array */
34
    private $statusReason = [
35
        self::CODE_FOUND      => 'Resource found.',
36
        self::CODE_NOT_FOUND  => 'The requested resource cannot be found.',
37
        self::CODE_BAD_METHOD => 'Bad request method was used by the client.'
38
    ];
39
    /** @var array */
40
    private $parameters;
41
42
    /**
43
     * Define clone behaviour.
44
     */
45 7
    public function __clone()
46
    {
47 7
        unset($this->status);
48 7
        unset($this->matchedMiddleware);
49 7
        unset($this->parameters);
50 7
        unset($this->resource);
51 7
    }
52
53
    /**
54
     * Sets resource.
55
     *
56
     * @param string $resource
57
     * @return Result
58
     */
59 5
    public function setResource(string $resource) : Result
60
    {
61 5
        $this->resource = $resource;
62
63 5
        return $this;
64
    }
65
66
    /**
67
     * Gets resource.
68
     *
69
     * @return string
70
     */
71 3
    public function getResource() : string
72
    {
73 3
        return $this->resource;
74
    }
75
76
    /**
77
     * Sets status code.
78
     *
79
     * @param int $status
80
     * @throws InvalidArgumentException
81
     * @return Result
82
     */
83 9
    public function setStatus(int $status) : Result
84
    {
85 9
        if (!isset($this->statusReason[$status])) {
86 1
            throw new InvalidArgumentException(sprintf('The parameter "%s" is not a valid router status.', $status));
87
        }
88
89 8
        $this->status = $status;
90
91 8
        return $this;
92
    }
93
94
    /**
95
     * Gets status code.
96
     *
97
     * @return int
98
     */
99 8
    public function getStatus() : int
100
    {
101 8
        return $this->status;
102
    }
103
104
    /**
105
     * Gets reason for the status set.
106
     *
107
     * @return string
108
     */
109 3
    public function getStatusReason() : string
110
    {
111 3
        return $this->statusReason[$this->status] ?? '';
112
    }
113
114
    /**
115
     * Sets matched middleware.
116
     *
117
     * @param string $matchedMiddleware
118
     * @return Result
119
     */
120 6
    public function setMatchedMiddleware(string $matchedMiddleware)
121
    {
122 6
        $this->matchedMiddleware = $matchedMiddleware;
123
124 6
        return $this;
125
    }
126
127
    /**
128
     * Gets matched middleware.
129
     *
130
     * @return string
131
     */
132 6
    public function getMatchedMiddleware() : string
133
    {
134 6
        return $this->matchedMiddleware;
135
    }
136
137
    /**
138
     * Sets the parameters.
139
     *
140
     * @param array $parameters
141
     * @return Result
142
     */
143 6
    public function setParameters(array $parameters) : Result
144
    {
145 6
        $this->parameters = $parameters;
146
147 6
        return $this;
148
    }
149
150
    /**
151
     * Gets the parameters.
152
     *
153
     * @return array
154
     */
155 4
    public function getParameters() : array
156
    {
157 4
        return $this->parameters;
158
    }
159
}
160