Passed
Push — master ( 0f9c27...eb20d7 )
by Sergii
04:50
created

RouterTrait::parseCallback()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 2
dl 0
loc 18
ccs 7
cts 11
cp 0.6364
crap 4.7691
rs 9.2
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\ClientSession;
13
use Thruway\Peer\RouterInterface;
14
15
/**
16
 * Trait RouterTrait.
17
 * Base router trait.
18
 */
19
trait RouterTrait
20
{
21
    /**
22
     * Router groups.
23
     *
24
     * @var null|\sonrac\WAMP\GroupsConfigInterface[]
25
     */
26
    protected $groups = null;
27
28
    /**
29
     * Main router
30
     *
31
     * @var null|\Thruway\Peer\RouterInterface|\sonrac\WAMP\Routers\Router
32
     *
33
     * @author Donii Sergii <[email protected]>
34
     */
35
    protected $router = null;
36
37
    /**
38
     * Group routes
39
     *
40
     * @param array    $config Group config
41
     * @param \Closure $runner Closure runner group
42
     *
43
     * @author Donii Sergii <[email protected]>
44
     */
45 1
    public function group(array $config, \Closure $runner)
46
    {
47 1
        $middleware = isset($config['middleware']) ? explode('|', $config['middleware']) : [];
48 1
        $namespace = isset($config['namespace']) ? $config['namespace'] : 'App\Controllers\WAMP';
49
50 1
        $this->groups[] = (object) [
51 1
            'middleware' => $middleware,
52 1
            'namespace'  => $namespace,
53 1
            'prefix'     => isset($config['prefix']) ? $config['prefix'] : '',
54 1
            'callback'   => $runner,
55
        ];
56 1
    }
57
58
    /**
59
     * Get route groups
60
     *
61
     * @return null|\sonrac\WAMP\GroupsConfigInterface[]
62
     *
63
     * @author Donii Sergii <[email protected]>
64
     */
65 1
    public function getGroups()
66
    {
67 1
        return $this->groups;
68
    }
69
70
    /**
71
     * Get router
72
     *
73
     * @return mixed|null|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router|\Thruway\Peer\RouterInterface
74
     *
75
     * @author Donii Sergii <[email protected]>
76
     */
77 4
    public function getRouter()
78
    {
79 4
        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

79
        return $this->router ?? $this->router = /** @scrutinizer ignore-call */ app()->wampRouter;
Loading history...
80
    }
81
82
    /**
83
     * Set router.
84
     *
85
     * @author Donii Sergii <[email protected]>
86
     */
87 1
    public function setRouter(RouterInterface $router)
88
    {
89 1
        $this->router = $router;
90 1
    }
91
92
    /**
93
     * Get client session.
94
     *
95
     * @return \Thruway\ClientSession
96
     *
97
     * @author Donii Sergii <[email protected]>
98
     */
99 3
    public function getClientSession()
100
    {
101 3
        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

101
        return $this->getRouter()->/** @scrutinizer ignore-call */ getClient()->getSession();
Loading history...
102
    }
103
104
    /**
105
     * Get client.
106
     *
107
     * @return \sonrac\WAMP\Client|\Thruway\Peer\ClientInterface
108
     *
109
     * @author Donii Sergii <[email protected]>
110
     */
111
    public function getClient()
112
    {
113
        return $this->getRouter()->getClient();
114
    }
115
116
    /**
117
     * Parse callback function
118
     *
119
     * @param string|\Closure $callback  Callback
120
     * @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...
121
     *
122
     * @return \Closure
123
     *
124
     * @author Donii Sergii <[email protected]>
125
     */
126 2
    protected function parseCallback($callback, $namespace = null)
127
    {
128 2
        if ($callback instanceof \Closure) {
129 1
            return $callback;
130
        }
131
132 1
        $namespace = $namespace ? $namespace . '\\' : '';
133
134 1
        $callback = explode('&', $callback);
135 1
        $self = $this;
136 1
        return function (ClientSession $clientSession) use ($callback, $namespace, $self) {
137
            if (count($callback) === 1) {
138
                return $this->{$callback[0]}($clientSession, $self->getClient());
139
            }
140
141
            $class = app()->make($namespace . $callback[0]);
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

141
            $class = /** @scrutinizer ignore-call */ app()->make($namespace . $callback[0]);
Loading history...
142
143
            return $class->{$callback[1]}($clientSession, $self->getClient());
144 1
        };
145
    }
146
}
147