Completed
Push — master ( 2bc5ee...3171bd )
by Sergii
04:59
created

RouterTrait   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Test Coverage

Coverage 85.25%

Importance

Changes 0
Metric Value
dl 0
loc 225
ccs 52
cts 61
cp 0.8525
rs 10
c 0
b 0
f 0
wmc 22

9 Methods

Rating   Name   Duplication   Size   Complexity  
B parseCallback() 0 25 6
A getRouter() 0 3 1
A getClientSession() 0 3 1
A setRouter() 0 3 1
A getClient() 0 3 1
A getGroups() 0 3 1
B prepareCallback() 0 14 5
A parseGroups() 0 22 2
A group() 0 10 4
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 *
5
 * @author Donii Sergii <[email protected]>
6
 * Date: 10/25/17
7
 * Time: 11:57 AM
8
 */
9
10
namespace sonrac\WAMP\Routers;
11
12
use Thruway\ClientSession;
13
use Thruway\Peer\RouterInterface;
14
15
/**
16
 * Trait RouterTrait.
17
 * Base router trait.
18
 */
19
trait RouterTrait
20
{
21
    /**
22
     * Controllers list
23
     *
24
     * @var \sonrac\WAMP\Abstracts\WAMPControllerInterface[]
25
     *
26
     * @author Donii Sergii <[email protected]>
27
     */
28
    protected $controllers;
29
30
    /**
31
     * Router groups.
32
     *
33
     * @var null|array
34
     */
35
    protected $groups = null;
36
37
    /**
38
     * Main router
39
     *
40
     * @var null|\Thruway\Peer\RouterInterface|\sonrac\WAMP\Routers\Router
41
     *
42
     * @author Donii Sergii <[email protected]>
43
     */
44
    protected $router = null;
45
46
    /**
47
     * Route path prefix.
48
     *
49
     * @var string|null
50
     *
51
     * @author Donii Sergii <[email protected]>
52
     */
53
    private $prefix = null;
54
55
    /**
56
     * Controller namespace.
57
     *
58
     * @var string|null
59
     *
60
     * @author Donii Sergii <[email protected]>
61
     */
62
    private $groupControllerNamespace = null;
63
64
    /**
65
     * Middleware list.
66
     *
67
     * @var null|array
68
     *
69
     * @author Donii Sergii <[email protected]>
70
     */
71
    private $middleware = null;
72
73
    /**
74
     * Group routes.
75
     *
76
     * @param array    $config Group config
77
     * @param \Closure $runner Closure runner group
78
     *
79
     * @author Donii Sergii <[email protected]>
80
     */
81 2
    public function group(array $config, \Closure $runner)
82
    {
83 2
        $middleware = isset($config['middleware']) ? explode('|', $config['middleware']) : [];
84 2
        $namespace = isset($config['namespace']) ? $config['namespace'] : 'App\Controllers\WAMP';
85
86 2
        $this->groups[] = [
87 2
            'middleware' => $middleware,
88 2
            'namespace'  => $namespace,
89 2
            'prefix'     => isset($config['prefix']) ? $config['prefix'] : '',
90 2
            'callback'   => $runner,
91
        ];
92 2
    }
93
94
    /**
95
     * Parse groups
96
     *
97
     * @return \sonrac\WAMP\GroupsConfigInterface[]|\stdClass[]
98
     *
99
     * @author Donii Sergii <[email protected]>
100
     */
101 1
    public function parseGroups()
102
    {
103 1
        gc_enable();
104 1
        $callbacks = [];
105 1
        foreach ($this->groups as $group) {
106 1
            $this->prefix = $group['prefix'];
107 1
            $this->groupControllerNamespace = $group['namespace'];
108 1
            $this->middleware = $group['middleware'];
109 1
            $callbacks[] = $group['callback']($this->getClientSession(), $this->getClient());
110
        }
111
112 1
        $this->groups = null;
113 1
        unset($this->groups);
114 1
        $this->groups = [];
115
116 1
        $this->prefix = null;
117 1
        $this->groupControllerNamespace = null;
118
119 1
        gc_collect_cycles();
120 1
        gc_disable();
121
122 1
        return $callbacks;
123
    }
124
125
    /**
126
     * Get route groups
127
     *
128
     * @return null|\sonrac\WAMP\GroupsConfigInterface[]
129
     *
130
     * @author Donii Sergii <[email protected]>
131
     */
132 1
    public function getGroups()
133
    {
134 1
        return $this->groups;
135
    }
136
137
    /**
138
     * Get router
139
     *
140
     * @return mixed|null|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router|\Thruway\Peer\RouterInterface
141
     *
142
     * @author Donii Sergii <[email protected]>
143
     */
144 3
    public function getRouter()
145
    {
146 3
        return $this->router ?? $this->router = app()->wampRouter;
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
        return $this->router ?? $this->router = /** @scrutinizer ignore-call */ app()->wampRouter;
Loading history...
147
    }
148
149
    /**
150
     * Set router.
151
     *
152
     * @author Donii Sergii <[email protected]>
153
     */
154 2
    public function setRouter(RouterInterface $router)
155
    {
156 2
        $this->router = $router;
157 2
    }
158
159
    /**
160
     * Get client session.
161
     *
162
     * @return \Thruway\ClientSession
163
     *
164
     * @author Donii Sergii <[email protected]>
165
     */
166 2
    public function getClientSession()
167
    {
168 2
        return $this->getRouter()->getClient()->getSession();
0 ignored issues
show
Bug introduced by
The method getClient() does not exist on Thruway\Peer\RouterInterface. It seems like you code against a sub-type of Thruway\Peer\RouterInterface such as sonrac\WAMP\Routers\Router. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
        return $this->getRouter()->/** @scrutinizer ignore-call */ getClient()->getSession();
Loading history...
169
    }
170
171
    /**
172
     * Get client.
173
     *
174
     * @return \sonrac\WAMP\Client|\Thruway\Peer\ClientInterface
175
     *
176
     * @author Donii Sergii <[email protected]>
177
     */
178 1
    public function getClient()
179
    {
180 1
        return $this->getRouter()->getClient();
181
    }
182
183
    /**
184
     * Parse callback function
185
     *
186
     * @param string|\Closure $callback  Callback
187
     * @param null            $namespace Controller namespace
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $namespace is correct as it would always require null to be passed?
Loading history...
188
     *
189
     * @return \Closure
190
     *
191
     * @author Donii Sergii <[email protected]>
192
     */
193 3
    public function parseCallback($callback, $namespace = null)
194
    {
195 3
        if ($callback instanceof \Closure) {
196 2
            return $callback;
197
        }
198
199 2
        $namespace = $namespace ? $namespace.'\\' : '';
200
201 2
        $callback = explode('&', $callback);
202 2
        $self = $this;
203
204 2
        return function (ClientSession $clientSession) use ($callback, $namespace, $self) {
205
            if (count($callback) === 1) {
206
                return $this->{$callback[0]}($clientSession, $self->getClient());
207
            }
208
209
            if (!isset($this->controllers[$callback[0]])) {
210
                return $this->controllers[$callback[0]]->{$callback[1]}($clientSession, $this->getClient());
211
            }
212
213
            $className = class_exists($callback[0]) ? $callback[0] : $namespace.$callback[0];
214
215
            $this->controllers[$callback[0]] = app()->make($className);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

215
            $this->controllers[$callback[0]] = /** @scrutinizer ignore-call */ app()->make($className);
Loading history...
216
217
            return $this->controllers[$callback[0]]->{$callback[1]}($clientSession, $self->getClient());
218 2
        };
219
    }
220
221
    /**
222
     * Prepare path.
223
     *
224
     * @param \Closure|string $callback Callback
225
     *
226
     * @return array
227
     *
228
     * @author Donii Sergii <[email protected]>
229
     */
230 1
    protected function prepareCallback($callback)
231
    {
232 1
        $namespace = $this->groupControllerNamespace ?? $this->getRouter()->getControllerNamespace();
0 ignored issues
show
Bug introduced by
The method getControllerNamespace() does not exist on Thruway\Peer\RouterInterface. It seems like you code against a sub-type of Thruway\Peer\RouterInterface such as sonrac\WAMP\Routers\Router. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

232
        $namespace = $this->groupControllerNamespace ?? $this->getRouter()->/** @scrutinizer ignore-call */ getControllerNamespace();
Loading history...
233 1
        $callbackAdditional = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $callbackAdditional is dead and can be removed.
Loading history...
234 1
        if ($this->groupControllerNamespace && $this->groupControllerNamespace
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->groupControllerNamespace of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
235
            && is_string($callback) && count(explode('&', $callback)) === 2) {
236
            $callback = rtrim($namespace, '\\').$callback;
237
        }
238
239
        return [
240 1
            'prefix'     => $this->prefix,
241 1
            'namespace'  => $namespace,
242 1
            'callback'   => $this->parseCallback($callback, $namespace),
0 ignored issues
show
Bug introduced by
It seems like $namespace can also be of type string; however, parameter $namespace of sonrac\WAMP\Routers\RouterTrait::parseCallback() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

242
            'callback'   => $this->parseCallback($callback, /** @scrutinizer ignore-type */ $namespace),
Loading history...
243 1
            'middleware' => $this->middleware
244
        ];
245
    }
246
}
247