Completed
Push — master ( e1f74a...6d0f2c )
by Restu
15:04
created

RegexDispatcher::dispatch()   D

Complexity

Conditions 17
Paths 12

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 17.4315

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 17
eloc 27
c 2
b 1
f 1
nc 12
nop 2
dl 0
loc 46
ccs 31
cts 35
cp 0.8857
crap 17.4315
rs 4.9679

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace JayaCode\Framework\Core\Route\Dispatcher;
3
4
use JayaCode\Framework\Core\Route\Collector\RouteCollector;
5
use JayaCode\Framework\Core\Route\Route;
6
use JayaCode\Framework\Core\Route\Status;
7
8
/**
9
 * Class RegexDispatcher
10
 * @package JayaCode\Framework\Core\Route\Dispatcher
11
 */
12
class RegexDispatcher implements Dispatcher
13
{
14
    protected $routeCollector;
15
16
    protected $data;
17
    /**
18
     * @var
19
     */
20
    protected $path;
21
22
    /**
23
     * @var array
24
     */
25
    protected $explodedPath;
26
27
    /**
28
     * @var
29
     */
30
    protected $httpMethod;
31
32 3
    public function __construct(RouteCollector $routeCollector, $data = null)
33
    {
34 3
        $this->routeCollector = $routeCollector;
35 3
        $this->data = $data;
36 3
    }
37
38
    /**
39
     * @param $httpMethod
40
     * @param $path
41
     */
42 3
    public function dispatch($httpMethod, $path)
43
    {
44 3
        $this->setPath($path);
45 3
        $this->httpMethod = $httpMethod;
46
47 3
        $result = [
48
            Status::NOT_FOUND
49 3
        ];
50 3
51 3
        $data = is_null($this->data)?$this->routeCollector->getData():$this->data;
52
        $countData = count($data);
53 3
        $countExplodePath = count($this->explodedPath);
54 3
55
        $i = 0;
56 3
        while ($result[0] == Status::NOT_FOUND && $countData > $i) {
57
            /** @var Route $route */
58 3
            $route = $data[$i];
59 3
60 3
            if ($this->httpMethod == $route->method && $countExplodePath == count($route->explodePath)) {
61 3
                $result[0] = Status::FOUND;
62 3
                if ($this->path != $route->path) {
63
                    for ($j = 0; $j < $countExplodePath && $result[0] == Status::FOUND; $j++) {
64 3
                        $countMatches = count($route->ruleMatches[$j]);
65 3
66 3
                        if ($countMatches == 0 ||
67 3
                            $countMatches == 2 && $this->explodedPath[$j] != $route->explodePath[$j] ||
68 3
                            $countMatches == 5 &&
69 3
                            !preg_match("/^".$route->ruleMatches[$j][4]."$/", $this->explodedPath[$j])) {
70 3
                            $result[0] = Status::NOT_FOUND;
71
                        } elseif ($countMatches == 3 || $countMatches == 5) {
72
                            $result[2][$route->ruleMatches[$j][2]] = $this->explodedPath[$j];
73 3
                        }
74
                    }
75 3
                }
76
77
78 3
                if ($result[0] == Status::FOUND) {
79 3
                    $result[1] = $route->handle;
80
                }
81
            }
82 3
83 3
            $i++;
84 3
        }
85 3
86
        return $result;
87 3
    }
88 3
89
    /**
90 3
     * @param mixed $path
91
     */
92
    public function setPath($path)
93
    {
94
        $this->path = static::normalizePath($path);
95
96 3
        $this->explodedPath = static::explodePath($this->path);
97
    }
98 3
99
    /**
100 3
     * @param $path
101 3
     * @return string
102
     */
103
    public static function normalizePath($path)
104
    {
105
        $path = trim($path, '/');
106
        return $path == ""?'/':$path;
107 3
    }
108
109 3
    /**
110 3
     * @param $path
111
     * @return array
112
     */
113
    public static function explodePath($path)
114
    {
115
        return (array) explode("/", $path);
116
    }
117
}
118