Loader   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
wmc 14
eloc 33
dl 0
loc 125
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A rule() 0 8 2
A simplify() 0 8 2
A validate() 0 3 1
A __construct() 0 4 1
A routes() 0 10 3
A _simplify() 0 16 3
A way() 0 6 2
1
<?php
2
3
namespace Bavix\Router;
4
5
use Bavix\Exceptions;
6
use Bavix\Exceptions\Runtime;
7
use Bavix\Router\Rules\PatternRule;
8
use Bavix\Router\Rules\PrefixRule;
9
10
class Loader
11
{
12
13
    /**
14
     * @var array
15
     */
16
    protected $rules = [
17
        'http' => Rules\HttpRule::class,
18
        'prefix' => Rules\PrefixRule::class,
19
        'pattern' => Rules\PatternRule::class,
20
    ];
21
22
    /**
23
     * @var null|Rule
24
     */
25
    protected $parent;
26
27
    /**
28
     * @var array
29
     */
30
    protected $config;
31
32
    /**
33
     * Loader constructor.
34
     *
35
     * @param iterable $config
36
     * @param null|Rule $parent
37
     */
38 7
    public function __construct($config, ?Rule $parent = null)
39
    {
40 7
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by Babichev Maxim
It seems like $config of type iterable is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41 7
        $this->parent = $parent;
42 7
    }
43
44
    /**
45
     * @return PatternRule[]
46
     */
47 7
    public function simplify(): array
48
    {
49 7
        $routes = [];
50 7
        foreach ($this->routes() as $key => $route) {
51 5
            $routes[] = $this->_simplify($route, $key);
52
        }
53
54 5
        return \array_merge(...$routes);
55
    }
56
57
    /**
58
     * @return \Generator
59
     */
60 7
    public function routes(): \Generator
61
    {
62 7
        foreach ($this->config as $key => $item) {
63 7
            if (empty($item['type'])) {
64 1
                throw new Exceptions\NotFound\Data(
65 1
                    \sprintf('The type parameter for key `%s` is not found', $this->way($key))
66
                );
67
            }
68
69 6
            yield $key => $this->rule($item['type'], $key, $item);
70
        }
71 5
    }
72
73
    /**
74
     * @param string $key
75
     *
76
     * @return string
77
     */
78 1
    protected function way(string $key): string
79
    {
80 1
        if ($this->parent) {
81
            return $this->parent->getName() . '.' . $key;
82
        }
83 1
        return $key;
84
    }
85
86
    /**
87
     * @param string $type
88
     * @param string $key
89
     * @param array $item
90
     *
91
     * @return Rule
92
     */
93 6
    public function rule(string $type, string $key, array $item): Rule
94
    {
95 6
        if (!$this->validate($type)) {
96
            throw new Runtime(\sprintf('Undefined type `%s`', $type));
97
        }
98
99 6
        $class = $this->rules[$type];
100 6
        return new $class($key, $item, $this->parent);
101
    }
102
103
    /**
104
     * @param string $type
105
     *
106
     * @return bool
107
     */
108 6
    protected function validate(string $type): bool
109
    {
110 6
        return \array_key_exists($type, $this->rules);
111
    }
112
113
    /**
114
     * @param Rule $rule
115
     * @param string $key
116
     *
117
     * @return PatternRule[]
118
     */
119 5
    protected function _simplify(Rule $rule, string $key): array
120
    {
121 5
        if ($rule instanceof PatternRule) {
122 5
            return [$key => $rule];
123
        }
124
125
        /**
126
         * @var PrefixRule $rule
127
         */
128 4
        $rules = [];
129 4
        foreach ($rule->resolver() as $index => $item) {
130 4
            $location = $key . '.' . $index;
131 4
            $rules[] = $this->_simplify($item, $location);
132
        }
133
134 4
        return \array_merge(...$rules);
135
    }
136
137
}
138