Client::includeRoutes()   B
last analyzed

Complexity

Conditions 10
Paths 7

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10.1167

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 10
eloc 18
c 1
b 1
f 0
nc 7
nop 2
dl 0
loc 34
ccs 17
cts 19
cp 0.8947
crap 10.1167
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 *
5
 * @author Donii Sergii <[email protected]>
6
 * Date: 10/25/17
7
 * Time: 12:18 PM
8
 */
9
10
namespace sonrac\WAMP;
11
12
use Thruway\ClientSession;
13
use Thruway\Message\Message;
14
use Thruway\Peer\Client as PeerClient;
15
16
/**
17
 * Class Client
18
 * Client class.
19
 *
20
 * @author Donii Sergii <[email protected]>
21
 */
22
class Client extends PeerClient
23
{
24
    /**
25
     * Path to WAMP routes
26
     *
27
     * @var string|null
28
     *
29
     * @author Donii Sergii <[email protected]>
30
     */
31
    protected $routePath = null;
32
33
    /**
34
     * Retry connection on close or no
35
     *
36
     * @var bool
37
     *
38
     * @author Donii Sergii <[email protected]>
39
     */
40
    protected $connectionRetry = true;
41
42
    /**
43
     * Realm
44
     *
45
     * @var string
46
     *
47
     * @author Donii Sergii <[email protected]>
48
     */
49
    protected $realm;
50
51
    /**
52
     * Session start event
53
     *
54
     * @param \Thruway\ClientSession                $session
55
     * @param \Thruway\Transport\TransportInterface $transport
56
     *
57
     * @author Donii Sergii <[email protected]>
58
     */
59 2
    public function onSessionStart($session, $transport)
60
    {
61 2
        $this->includeRoutes($session, $transport);
62 2
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function start($startLoop = true)
68
    {
69
        parent::start($startLoop);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function onClose($reason, $retry = true)
76
    {
77
        $this->connectionRetry = $retry;
78
79
        parent::onClose($reason);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function retryConnection()
86
    {
87
        if (!$this->connectionRetry) {
88
            return;
89
        }
90
91
        return parent::retryConnection();
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::retryConnection() targeting Thruway\Peer\Client::retryConnection() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
92
    }
93
94 2
    public function setRoutePath($path)
95
    {
96 2
        $this->routePath = $path;
97 2
    }
98
99
    /**
100
     * Set attribute connectionRetry
101
     *
102
     * @return bool
103
     *
104
     * @author Donii Sergii <[email protected]>
105
     */
106
    public function isConnectionRetry(): bool
107
    {
108
        return $this->connectionRetry;
109
    }
110
111
    /**
112
     * Set connection retry
113
     *
114
     * @param bool $connectionRetry
115
     *
116
     * @author Donii Sergii <[email protected]>
117
     */
118
    public function setConnectionRetry(bool $connectionRetry)
119
    {
120
        $this->connectionRetry = $connectionRetry;
121
    }
122
123
    /**
124
     * Include routes
125
     *
126
     * @param \Thruway\ClientSession                $session   Client session
127
     * @param \Thruway\Transport\TransportInterface $transport Transport provider
128
     *
129
     * @author Donii Sergii <[email protected]>
130
     */
131 2
    protected function includeRoutes($session, $transport)
132
    {
133 2
        if (!is_dir($this->routePath) && !is_file($this->routePath)) {
134
            return;
135
        }
136
137 2
        if ($session instanceof ClientSession) {
0 ignored issues
show
introduced by
$session is always a sub-type of Thruway\ClientSession.
Loading history...
138
            $this->session = $session;
139
        }
140 2
        $client = $this;
141
142
        /* @scrutinizer ignore-call */
143 2
        app()->wampRouter->setClient($client);
144
145 2
        if (is_file($this->routePath)) {
146 1
            require $this->routePath;
147 1
            app()->wampRouter->parseGroups();
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

147
            /** @scrutinizer ignore-call */ 
148
            app()->wampRouter->parseGroups();
Loading history...
148 1
            return;
149
        }
150
151 1
        if (is_dir($this->routePath)) {
152 1
            $files = scandir($this->routePath);
153
154 1
            foreach ($files as $file) {
155 1
                if ($file === '.' || $file === '..') {
156 1
                    continue;
157
                }
158
159 1
                if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
160 1
                    require $this->routePath.DIRECTORY_SEPARATOR.$file;
161
                }
162
            }
163
        }
164 1
        app()->wampRouter->parseGroups();
165 1
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function processOther(ClientSession $session = null, Message $msg = null)
171
    {
172
        if (!$session) {
173
            return;
174
        }
175
176
        parent::processOther($session, $msg);
0 ignored issues
show
Bug introduced by
It seems like $msg can also be of type null; however, parameter $msg of Thruway\Peer\Client::processOther() does only seem to accept Thruway\Message\Message, 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

176
        parent::processOther($session, /** @scrutinizer ignore-type */ $msg);
Loading history...
177
    }
178
}
179