Completed
Push — master ( 20348e...611870 )
by Sergii
06:13
created

Client::includeRoutes()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 7
nop 2
dl 0
loc 27
ccs 16
cts 16
cp 1
crap 9
rs 4.909
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: 12:18 PM
8
 */
9
10
namespace sonrac\WAMP;
11
12
use Thruway\Peer\Client as PeerClient;
13
14
/**
15
 * Class Client
16
 * Client class.
17
 *
18
 * @author Donii Sergii <[email protected]>
19
 */
20
class Client extends PeerClient
21
{
22
    /**
23
     * Path to WAMP routes
24
     *
25
     * @var string|null
26
     *
27
     * @author Donii Sergii <[email protected]>
28
     */
29
    protected $routePath = null;
30
31
    /**
32
     * Retry connection on close or no
33
     *
34
     * @var bool
35
     *
36
     * @author Donii Sergii <[email protected]>
37
     */
38
    protected $connectionRetry = true;
39
40
    /**
41
     * Session start event
42
     *
43
     * @param \Thruway\ClientSession                $session
44
     * @param \Thruway\Transport\TransportInterface $transport
45
     *
46
     * @author Donii Sergii <[email protected]>
47
     */
48 3
    public function onSessionStart($session, $transport)
49
    {
50 3
        $this->includeRoutes($session, $transport);
51 3
    }
52
53 2
    public function setRoutePath($path)
54
    {
55 2
        $this->routePath = $path;
56 2
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function start($startLoop = true)
62
    {
63 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

63
        /** @scrutinizer ignore-call */ 
64
        app()->wampRouter->parseGroups();
Loading history...
64 1
        parent::start($startLoop);
65 1
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function onClose($reason, $retry = true)
71
    {
72
        $this->connectionRetry = $retry;
73
74
        parent::onClose($reason);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function retryConnection()
81
    {
82
        if (!$this->connectionRetry) {
83
            return;
84
        }
85
86
        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...
87
    }
88
89
    /**
90
     * Include routes
91
     *
92
     * @param \Thruway\ClientSession                $session   Client session
93
     * @param \Thruway\Transport\TransportInterface $transport Transport provider
94
     *
95
     * @author Donii Sergii <[email protected]>
96
     */
97 3
    protected function includeRoutes($session, $transport)
98
    {
99 3
        if (!is_dir($this->routePath) && !is_file($this->routePath)) {
100 1
            return;
101
        }
102
103 2
        $session = $this->session = $session;
104 2
        $client = $this;
105
106
        /* @scrutinizer ignore-call */
107 2
        app()->wampRouter->setClient($client);
108
109 2
        if (is_file($this->routePath)) {
110 1
            require $this->routePath;
111 1
            return;
112
        }
113
114 1
        if (is_dir($this->routePath)) {
115 1
            $files = scandir($this->routePath);
116
117 1
            foreach ($files as $file) {
118 1
                if ($file === '.' || $file === '..') {
119 1
                    continue;
120
                }
121
122 1
                if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
123 1
                    require $this->routePath.DIRECTORY_SEPARATOR.$file;
124
                }
125
            }
126
        }
127 1
    }
128
}
129