Completed
Push — master ( 59d48e...ffd6fa )
by Sergii
04:52
created

RouterTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 67.74%

Importance

Changes 0
Metric Value
dl 0
loc 124
ccs 21
cts 31
cp 0.6774
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A parseCallback() 0 18 4
A getRouter() 0 3 1
A getClientSession() 0 3 1
A setRouter() 0 3 1
A getClient() 0 3 1
A getGroups() 0 2 1
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
     * 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 1
        return $this->groups;
67
    }
68
69
    /**
70
     * Get router
71
     *
72
     * @return mixed|null|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router|\Thruway\Peer\RouterInterface
73
     *
74
     * @author Donii Sergii <[email protected]>
75
     */
76 2
    public function getRouter()
77
    {
78 2
        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

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

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

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