Passed
Push — master ( 05b7e1...957c9d )
by Бабичев
03:54
created

src/Router/Configure.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Bavix\Router;
4
5
use Bavix\Exceptions;
6
use Bavix\Slice\Slice;
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 array|\Traversable|Slice $data
33
     * @param CacheItemPoolInterface|null $pool
34
     */
35
    public function __construct($data, CacheItemPoolInterface $pool = null)
36
    {
37
        $this->slice = Slice::from($data);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Bavix\Slice\Slice::from($data) of type object<self> is incompatible with the declared type object<Bavix\Slice\Slice> of property $slice.

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...
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