Passed
Push — master ( 39666f...8b4565 )
by Бабичев
09:02
created

Http   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 73
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A storage() 0 14 1
A __construct() 0 9 1
B build() 0 24 2
1
<?php
2
3
namespace Bavix\Router\Type;
4
5
use Bavix\Exceptions\NotFound;
6
use Bavix\Router\Configure;
7
use Bavix\Router\Type;
8
use Bavix\Slice\Slice;
9
10
class Http extends Type
11
{
12
13
    /**
14
     * Http constructor.
15
     *
16
     * @param Configure $configure
17
     * @param Slice     $slice
18
     * @param array     $options
19
     *
20
     * @throws NotFound\Data
21
     */
22
    public function __construct(Configure $configure, Slice $slice, array $options)
23
    {
24
        $this->types = [
25
            'prefix'  => Prefix::class,
26
            'pattern' => Pattern::class,
27
        ];
28
29
        parent::__construct($configure, $slice, $options);
30
    }
31
32
    /**
33
     * @param string $key
34
     * @param Slice  $slice
35
     *
36
     * @return array
37
     * @throws NotFound\Data
38
     */
39
    protected function storage($key, Slice $slice)
40
    {
41
        list($path, $regex) = $this->path($slice);
42
43
        return [
44
            'protocol' => $this->slice->getData('protocol', $this->protocol),
45
            'host'     => $this->slice->getData('host', $this->host),
46
            'regex'    => $regex,
47
            'path'     => $this->path . $path, // http add path
48
            'key'      => $this->key . '.' . $key,
49
            'defaults' => $this->slice->getData('defaults', []),
50
            'methods'  => $this->slice->getData('methods', []),
51
        ];
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function build()
58
    {
59
        $routes   = [];
60
        $resolver = $this->slice->getSlice('resolver');
61
62
        foreach ($resolver->asGenerator() as $key => $slice)
63
        {
64
            $type  = $this->getType($slice, $this->key . '.' . $key);
65
            $class = $this->types[$type];
66
67
            /**
68
             * @var $object Type
69
             */
70
            $object = new $class(
71
                $this->configure,
72
                $slice,
73
                $this->storage($key, $slice)
74
            );
75
76
            $routes += $object->build();
77
        }
78
79
        return $routes;
80
    }
81
82
}
83