Completed
Pull Request — master (#6)
by Florian
04:02 queued 12s
created

Scope   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
eloc 49
dl 0
loc 152
c 0
b 0
f 0
ccs 22
cts 22
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 7 1
A apply() 0 7 2
A __construct() 0 21 1
B scopify() 0 22 7
A middleware() 0 8 4
A seed() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Lead\Router;
5
6
/**
7
 * Scope
8
 */
9
class Scope implements ScopeInterface
10
{
11
    /**
12
     * The router instance.
13
     *
14
     * @var object
15
     */
16
    protected $_router = null;
17
18
    /**
19
     * The parent instance.
20
     *
21
     * @var object
22
     */
23
    protected $_parent = null;
24
25
    /**
26
     * The middleware array.
27
     *
28
     * @var array
29
     */
30
    protected $_middleware = [];
31
32
    /**
33
     * The scope data.
34
     *
35
     * @var array
36
     */
37
    protected $_scope = [];
38
39
    /**
40
     * The constructor.
41
     *
42
     * @param array $config The config array.
43
     */
44
    public function __construct($config = [])
45
    {
46 50
        $defaults = [
47 50
            'router' => null,
48
            'parent' => null,
49 50
            'middleware' => [],
50 50
            'scope'  => []
51 50
        ];
52
        $config += $defaults;
53
54
        $this->_router = $config['router'];
55
        $this->_parent = $config['parent'];
56
        $this->_middleware = $config['middleware'];
57
        $this->_scope = $config['scope'] + [
58
            'name'           => '',
59
            'scheme'         => '*',
60 50
            'host'           => '*',
61
            'methods'        => '*',
62
            'prefix'         => '',
63
            'namespace'      => '',
64
            'persist'        => []
65
        ];
66
    }
67
68
    /**
69
     * Creates a new sub scope based on the instance scope.
70
     *
71
     * @param  array $options The route options to scopify.
72
     * @return $this          The new sub scope.
73
     */
74
    public function seed(array $options): ScopeInterface
75 14
    {
76
        return new static(
77
            [
78
            'router' => $this->_router,
79
            'parent' => $this,
80
            'scope'  => $this->scopify($options)
81
            ]
82
        );
83
    }
84
85
    /**
86 40
     * Scopes an options array according to the instance scope data.
87
     *
88
     * @param  array $options The options to scope.
89 9
     * @return array          The scoped options.
90
     */
91
    public function scopify(array $options): array
92
    {
93 13
        $scope = $this->_scope;
94 13
95
        if (!empty($options['name'])) {
96
            $options['name'] = $scope['name'] ? $scope['name'] . '.' . $options['name'] : $options['name'];
97
        }
98 2
99
        if (!empty($options['prefix'])) {
100
            $options['prefix'] = $scope['prefix'] . trim($options['prefix'], '/');
101
            $options['prefix'] = $options['prefix'] ? $options['prefix'] . '/' : '';
102 1
        }
103
104
        if (isset($options['persist'])) {
105 40
            $options['persist'] = ((array) $options['persist']) + $scope['persist'];
106
        }
107
108
        if (isset($options['namespace'])) {
109
            $options['namespace'] = $scope['namespace'] . trim($options['namespace'], '\\') . '\\';
110
        }
111
112
        return $options + $scope;
113
    }
114
115
    /**
116 3
     * Middleware generator.
117
     *
118
     * @return callable
119
     */
120 1
    public function middleware()
121
    {
122
        foreach ($this->_middleware as $middleware) {
123
            yield $middleware;
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $middleware returns the type Generator which is incompatible with the documented return type callable.
Loading history...
124
        }
125
        if ($this->_parent) {
126
            foreach ($this->_parent->middleware() as $middleware) {
127
                yield $middleware;
128
            }
129
        }
130
    }
131
132
    /**
133 3
     * Adds a middleware to the list of middleware
134
     *
135 3
     * @param object|\Closure A callable middleware
0 ignored issues
show
Bug introduced by
The type Lead\Router\A was not found. Did you mean A? If so, make sure to prefix the type with \.
Loading history...
136
     * @return \Lead\Router\ScopeInterface
137
     */
138
    public function apply($middleware): ScopeInterface
139
    {
140
        foreach (func_get_args() as $mw) {
141
            array_unshift($this->_middleware, $mw);
142
        }
143
144
        return $this;
145
    }
146 1
147 1
    /**
148 1
     * Delegates calls to the router instance
149 1
     *
150
     * @param string $name The method name
151
     * @param array $params The parameters
152
     * @return mixed
153
     */
154
    public function __call(string $name, array $params)
155
    {
156
        $this->_router->pushScope($this);
157
        $result = call_user_func_array([$this->_router, $name], $params);
158
        $this->_router->popScope();
159
160
        return $result;
161
    }
162
}
163