Completed
Pull Request — master (#3)
by Бабичев
02:07
created

Loader::simplify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 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 5
    public function __construct($config, ?Rule $parent = null)
39
    {
40 5
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
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 5
        $this->parent = $parent;
42 5
    }
43
44
    /**
45
     * @return \Generator
46
     */
47 5
    public function routes(): \Generator
48
    {
49 5
        foreach ($this->config as $key => $item) {
50 5
            if (empty($item['type'])) {
51 1
                throw new Exceptions\NotFound\Data(
52 1
                    \sprintf('The type parameter for key `%s` is not found', $key)
53
                );
54
            }
55
56 4
            yield $key => $this->rule($item['type'], $key, $item);
57
        }
58 3
    }
59
60
    /**
61
     * @return PatternRule[]
62
     */
63 5
    public function simplify(): array
64
    {
65 5
        $routes = [];
66 5
        foreach ($this->routes() as $key => $route) {
67 3
            $routes[] = $this->_simplify($route, $key);
68
        }
69
70 3
        return \array_merge(...$routes);
71
    }
72
73
    /**
74
     * @param Rule $rule
75
     * @param string $key
76
     *
77
     * @return PatternRule[]
78
     */
79 3
    protected function _simplify(Rule $rule, string $key): array
80
    {
81 3
        if ($rule instanceof PatternRule) {
82 3
            return [$key => $rule];
83
        }
84
85
        /**
86
         * @var PrefixRule $rule
87
         */
88 2
        $rules = [];
89 2
        foreach ($rule->resolver() as $index => $item) {
90 2
            $location = $key . '.' . $index;
91 2
            $rules[] = $this->_simplify($item, $location);
92
        }
93
94 2
        return \array_merge(...$rules);
95
    }
96
97
    /**
98
     * @param string $type
99
     *
100
     * @return bool
101
     */
102 4
    protected function validate(string $type): bool
103
    {
104 4
        return \array_key_exists($type, $this->rules);
105
    }
106
107
    /**
108
     * @param string $type
109
     * @param string $key
110
     * @param array  $item
111
     *
112
     * @return Rule
113
     */
114 4
    public function rule(string $type, string $key, array $item): Rule
115
    {
116 4
        if (!$this->validate($type)) {
117
            throw new Runtime(\sprintf('Undefined type `%s`', $type));
118
        }
119
120 4
        $class = $this->rules[$type];
121 4
        return new $class($key, $item, $this->parent);
122
    }
123
124
}
125