Completed
Push — master ( e41b48...e132fe )
by Sergii
05:32
created

Client::setRoutePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
    /**
54
     * {@inheritdoc}
55
     */
56 1
    public function start($startLoop = true)
57
    {
58 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

58
        /** @scrutinizer ignore-call */ 
59
        app()->wampRouter->parseGroups();
Loading history...
59 1
        parent::start($startLoop);
60 1
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function onClose($reason, $retry = true)
66
    {
67
        $this->connectionRetry = $retry;
68
69
        parent::onClose($reason);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function retryConnection()
76
    {
77
        if (!$this->connectionRetry) {
78
            return;
79
        }
80
81
        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...
82
    }
83
84 2
    public function setRoutePath($path)
85
    {
86 2
        $this->routePath = $path;
87 2
    }
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
112 1
            return;
113
        }
114
115 1
        if (is_dir($this->routePath)) {
116 1
            $files = scandir($this->routePath);
117
118 1
            foreach ($files as $file) {
119 1
                if ($file === '.' || $file === '..') {
120 1
                    continue;
121
                }
122
123 1
                if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
124 1
                    require $this->routePath.DIRECTORY_SEPARATOR.$file;
125
                }
126
            }
127
        }
128 1
    }
129
}
130