Completed
Push — master ( 91d83f...255971 )
by Matthew
02:37
created

Route::setParams()   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 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace Fyuze\Routing;
3
4
use Closure;
5
use InvalidArgumentException;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
class Route
9
{
10
    /**
11
     * @var
12
     */
13
    protected $uri;
14
15
    /**
16
     * @var
17
     */
18
    protected $name;
19
20
    /**
21
     * @var
22
     */
23
    protected $action;
24
25
    /**
26
     * @var
27
     */
28
    protected $params;
29
30
    /**
31
     * @param $uri
32
     * @param $name
33
     * @param $action
34
     */
35 14
    public function __construct($uri, $name, $action)
36
    {
37 14
        $this->uri = $uri;
38 14
        $this->name = $name;
39 14
        $this->action = $action;
40 14
    }
41
42
    /**
43
     * @return mixed
44
     */
45 11
    public function getUri()
46
    {
47 11
        return $this->uri;
48
    }
49
50
51
    /**
52
     * @throws InvalidArgumentException
53
     * @return mixed Closure|Controller
54
     */
55 7
    public function getAction()
56
    {
57 7
        if ($this->action instanceof Closure) {
58
59 2
            return $this->action;
60
        }
61
62 5
        list($controller, $method) = explode('@', $this->action);
63
64 5
        if (!class_exists($controller)) {
65 1
            throw new InvalidArgumentException('Invalid controller specified');
66
        }
67
68 4
        return [$controller, $method];
69
    }
70
71
    /**
72
     * Check if route matches given url
73
     *
74
     * @param ServerRequestInterface $request
75
     * @return bool
76
     */
77 10
    public function matches(ServerRequestInterface $request)
78
    {
79 10
        return (new Matcher($request, $this))->resolves();
80
    }
81
82 2
    public function getQueryParams() {
83 2
        return $this->params;
84
    }
85
86 2
    public function setParams(array $params = [])
87
    {
88 2
        $this->params = $params;
89
90 2
        return $this;
91
    }
92
}
93