Completed
Push — master ( 99dbe8...35d4fc )
by Sergii
14:14
created

Client::onClose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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 3
     * @var bool
35
     *
36 3
     * @author Donii Sergii <[email protected]>
37 3
     */
38
    protected $connectionRetry = true;
39 2
40
    /**
41 2
     * Session start event
42 2
     *
43
     * @param \Thruway\ClientSession                $session
44
     * @param \Thruway\Transport\TransportInterface $transport
45
     *
46
     * @author Donii Sergii <[email protected]>
47 1
     */
48
    public function onSessionStart($session, $transport)
49 1
    {
50 1
        $this->includeRoutes($session, $transport);
51 1
    }
52
53
    public function setRoutePath($path)
54
    {
55
        $this->routePath = $path;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 3
    public function start($startLoop = true)
62
    {
63 3
        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
    }
66
67 2
    /**
68 2
     * {@inheritdoc}
69
     */
70
    public function onClose($reason, $retry = true)
71 2
    {
72
        $this->connectionRetry = $retry;
73 2
74 1
        parent::onClose($reason);
75 1
    }
76
77
    /**
78 1
     * {@inheritdoc}
79 1
     */
80
    public function retryConnection()
81 1
    {
82 1
        if (!$this->connectionRetry) {
83 1
            return;
84
        }
85
86 1
        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 1
    }
88
89
    /**
90
     * Include routes
91 1
     *
92
     * @param \Thruway\ClientSession                $session   Client session
93
     * @param \Thruway\Transport\TransportInterface $transport Transport provider
94
     *
95
     * @author Donii Sergii <[email protected]>
96
     */
97
    protected function includeRoutes($session, $transport)
98
    {
99
        if (!is_dir($this->routePath) && !is_file($this->routePath)) {
100
            return;
101
        }
102
103
        $session = $this->session = $session;
104
        $client = $this;
105
106
        /* @scrutinizer ignore-call */
107
        app()->wampRouter->setClient($client);
108
109
        if (is_file($this->routePath)) {
110
            require $this->routePath;
111
            return;
112
        }
113
114
        if (is_dir($this->routePath)) {
115
            $files = scandir($this->routePath);
116
117
            foreach ($files as $file) {
118
                if ($file === '.' || $file === '..') {
119
                    continue;
120
                }
121
122
                if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
123
                    require $this->routePath.DIRECTORY_SEPARATOR.$file;
124
                }
125
            }
126
        }
127
    }
128
}
129