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

Configure   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 113
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A data() 0 19 3
B routes() 0 27 3
A build() 0 11 2
1
<?php
2
3
namespace Bavix\Router;
4
5
use Bavix\Slice\Slice;
6
use Bavix\Exceptions;
7
use Psr\Cache\CacheItemPoolInterface;
8
9
class Configure
10
{
11
12
    use HelperThrows;
13
14
    /**
15
     * @var CacheItemPoolInterface
16
     */
17
    protected $pool;
18
19
    /**
20
     * @var Slice
21
     */
22
    protected $slice;
23
24
    /**
25
     * @var Slice
26
     */
27
    protected $build;
28
29
    /**
30
     * Configure constructor.
31
     *
32
     * @param Slice                       $slice
33
     * @param CacheItemPoolInterface|null $pool
34
     */
35
    public function __construct(Slice $slice, CacheItemPoolInterface $pool = null)
36
    {
37
        $this->slice = $slice;
38
        $this->pool  = $pool;
39
40
        $this->types = [
41
            'http'    => Type\Http::class,
42
            'prefix'  => Type\Prefix::class,
43
            'pattern' => Type\Pattern::class,
44
        ];
45
    }
46
47
    /**
48
     * @return Slice
49
     *
50
     * @return Slice|mixed
51
     * @throws \Psr\Cache\InvalidArgumentException
52
     */
53
    public function data()
54
    {
55
        if ($this->pool instanceof CacheItemPoolInterface)
56
        {
57
            $item = $this->pool->getItem($this->slice);
58
59
            if (!$item->isHit())
60
            {
61
                $item->set($result = $this->build());
62
                $this->pool->save($item);
63
64
                return $result;
65
            }
66
67
            return $item->get();
68
        }
69
70
        return $this->build();
71
    }
72
73
    /**
74
     * @return Route[]
75
     *
76
     * @throws Exceptions\NotFound\Data
77
     */
78
    protected function routes()
79
    {
80
        $routes = [];
81
82
        foreach ($this->slice->asGenerator() as $key => $slice)
83
        {
84
            if ($key{0} === '@')
85
            {
86
                // for xml
87
                continue;
88
            }
89
90
            $type  = $this->getType($slice, $key);
91
            $class = $this->types[$type];
92
93
            /**
94
             * @var $object Type
95
             */
96
            $object = new $class($this, $slice, [
97
                'key'    => $key
98
            ]);
99
100
            $routes += $object->build();
101
        }
102
103
        return $routes;
104
    }
105
106
    /**
107
     * @return Slice
108
     */
109
    public function build()
110
    {
111
        if (!$this->build)
112
        {
113
            $this->build = $this->slice->make(
114
                $this->routes()
115
            );
116
        }
117
118
        return $this->build;
119
    }
120
121
}
122