Route::getMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the package neoblack/free-at-home-api.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE file that was distributed with this source code.
9
 */
10
11
namespace NeoBlack\FreeAtHomeApi\Entity;
12
13
use NeoBlack\FreeAtHomeApi\Exception\MethodNotAllowedException;
14
15
class Route
16
{
17
    public const METHOD_GET = 'GET';
18
    public const METHOD_POST = 'POST';
19
    public const METHOD_PUT = 'PUT';
20
    public const METHOD_DELETE = 'DELETE';
21
22
    protected $path;
23
    protected $method;
24
    protected $parameters;
25
26
    public function __construct(string $path, string $method, array $parameters = [])
27
    {
28
        if (!\in_array($method, [self::METHOD_GET, self::METHOD_POST, self::METHOD_PUT, self::METHOD_DELETE], true)) {
29
            throw new MethodNotAllowedException('The method "' . $method . '" is not allowed', 1536352833);
30
        }
31
        $this->path = $path;
32 10
        $this->method = $method;
33
        $this->parameters = $parameters;
34 10
    }
35 5
36
    /**
37 5
     * @return string
38 5
     */
39 5
    public function getPath(): string
40
    {
41
        $tmpPath = $this->path;
42
        foreach ($this->parameters as $key => $value) {
43
            $tmpPath = str_replace('{' . $key . '}', $value, $tmpPath);
44 5
        }
45
        return $tmpPath;
46 5
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getMethod(): string
52 5
    {
53
        return $this->method;
54 5
    }
55
}
56