Completed
Push — master ( e41b48...e132fe )
by Sergii
05:32
created

RouterTrait::setRouter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Peer\RouterInterface;
13
14
/**
15
 * Trait RouterTrait.
16
 * Base router trait.
17
 */
18
trait RouterTrait
19
{
20
    /**
21
     * Controllers list
22
     *
23
     * @var \sonrac\WAMP\Abstracts\WAMPControllerInterface[]
24
     *
25
     * @author Donii Sergii <[email protected]>
26
     */
27
    protected $controllers;
28
29
    /**
30
     * Router groups.
31
     *
32
     * @var null|array
33
     */
34
    protected $groups = null;
35
36
    /**
37
     * Main router
38
     *
39
     * @var null|\Thruway\Peer\RouterInterface|\sonrac\WAMP\Routers\Router
40
     *
41
     * @author Donii Sergii <[email protected]>
42
     */
43
    protected $router = null;
44
45
    /**
46
     * Route path prefix.
47
     *
48
     * @var string|null
49
     *
50
     * @author Donii Sergii <[email protected]>
51
     */
52
    private $prefix = null;
53
54
    /**
55
     * Controller namespace.
56
     *
57
     * @var string|null
58
     *
59
     * @author Donii Sergii <[email protected]>
60
     */
61
    private $groupControllerNamespace = null;
62
63
    /**
64
     * Middleware list.
65
     *
66
     * @var null|array
67
     *
68
     * @author Donii Sergii <[email protected]>
69
     */
70
    private $middleware = null;
71
72
    /**
73
     * Group routes.
74
     *
75
     * @param array    $config Group config
76
     * @param \Closure $runner Closure runner group
77
     *
78
     * @author Donii Sergii <[email protected]>
79
     */
80 2
    public function group(array $config, \Closure $runner)
81
    {
82 2
        $middleware = isset($config['middleware']) ? explode('|', $config['middleware']) : [];
83 2
        $namespace = isset($config['namespace']) ? $config['namespace'] : 'App\Controllers\WAMP';
84
85 2
        $this->groups[] = [
86 2
            'middleware' => $middleware,
87 2
            'namespace'  => $namespace,
88 2
            'prefix'     => isset($config['prefix']) ? $config['prefix'] : '',
89 2
            'callback'   => $runner,
90
        ];
91 2
    }
92
93
    /**
94
     * Parse groups
95
     *
96
     * @return \sonrac\WAMP\GroupsConfigInterface[]|\stdClass[]
97
     *
98
     * @author Donii Sergii <[email protected]>
99
     */
100 2
    public function parseGroups()
101
    {
102 2
        if (!is_array($this->groups) || !count($this->groups)) {
103 1
            return;
104
        }
105 1
        gc_enable();
106 1
        $callbacks = [];
107 1
        foreach ($this->groups as $group) {
108 1
            $this->prefix = $group['prefix'];
109 1
            $this->groupControllerNamespace = $group['namespace'];
110 1
            $this->middleware = $group['middleware'];
111 1
            $callbacks[] = $group['callback']($this->getClientSession(), $this->getClient());
112
        }
113
114 1
        $this->groups = null;
115 1
        unset($this->groups);
116 1
        $this->groups = [];
117
118 1
        $this->prefix = null;
119 1
        $this->groupControllerNamespace = null;
120
121 1
        gc_collect_cycles();
122 1
        gc_disable();
123
124 1
        return $callbacks;
125
    }
126
127
    /**
128
     * Get route groups
129
     *
130
     * @return null|\sonrac\WAMP\GroupsConfigInterface[]
131
     *
132
     * @author Donii Sergii <[email protected]>
133
     */
134 1
    public function getGroups()
135
    {
136 1
        return $this->groups;
137
    }
138
139
    /**
140
     * Get router
141
     *
142
     * @return mixed|null|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router|\Thruway\Peer\RouterInterface
143
     *
144
     * @author Donii Sergii <[email protected]>
145
     */
146 3
    public function getRouter()
147
    {
148 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

148
        return $this->router ?? $this->router = /** @scrutinizer ignore-call */ app()->wampRouter;
Loading history...
149
    }
150
151
    /**
152
     * Set router.
153
     *
154
     * @author Donii Sergii <[email protected]>
155
     */
156 2
    public function setRouter(RouterInterface $router)
157
    {
158 2
        $this->router = $router;
159 2
    }
160
161
    /**
162
     * Get client session.
163
     *
164
     * @return \Thruway\ClientSession
165
     *
166
     * @author Donii Sergii <[email protected]>
167
     */
168 2
    public function getClientSession()
169
    {
170 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

170
        return $this->getRouter()->/** @scrutinizer ignore-call */ getClient()->getSession();
Loading history...
171
    }
172
173
    /**
174
     * Get client.
175
     *
176
     * @return \sonrac\WAMP\Client|\Thruway\Peer\ClientInterface
177
     *
178
     * @author Donii Sergii <[email protected]>
179
     */
180 1
    public function getClient()
181
    {
182 1
        return $this->getRouter()->getClient();
183
    }
184
185
    /**
186
     * Parse callback function
187
     *
188
     * @param string|\Closure $callback  Callback
189
     * @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...
190
     *
191
     * @return \Closure
192
     *
193
     * @author Donii Sergii <[email protected]>
194
     */
195 3
    public function parseCallback($callback, $namespace = null)
196
    {
197 3
        if ($callback instanceof \Closure) {
198 2
            return $callback;
199
        }
200
201 2
        $namespace = $namespace ? $namespace.'\\' : '';
202
203 2
        $callback = explode('&', $callback);
204
205 2
        return function () use ($callback, $namespace) {
206
            if (count($callback) === 1) {
207
                return call_user_func_array([$this, $callback[0]], func_get_args());
208
            }
209
210
            if (isset($this->controllers[$callback[0]])) {
211
                return call_user_func_array([$this->controllers[$callback[0]], $callback[1]], func_get_args());
212
            }
213
214
            $className = class_exists($callback[0]) ? $callback[0] : $namespace.$callback[0];
215
216
            $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

216
            $this->controllers[$callback[0]] = /** @scrutinizer ignore-call */ app()->make($className);
Loading history...
217
218
            return call_user_func_array([$this->controllers[$callback[0]], $callback[1]], func_get_args());
219 2
        };
220
    }
221
222
    /**
223
     * Prepare path.
224
     *
225
     * @param \Closure|string $callback Callback
226
     *
227
     * @return array
228
     *
229
     * @author Donii Sergii <[email protected]>
230
     */
231 1
    protected function prepareCallback($callback)
232
    {
233 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

233
        $namespace = $this->groupControllerNamespace ?? $this->getRouter()->/** @scrutinizer ignore-call */ getControllerNamespace();
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