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

Route   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 2 Features 2
Metric Value
wmc 8
c 8
b 2
f 2
lcom 1
cbo 1
dl 0
loc 85
ccs 21
cts 21
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getUri() 0 4 1
A getAction() 0 15 3
A matches() 0 4 1
A getQueryParams() 0 3 1
A setParams() 0 6 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