Completed
Push — master ( 9bf1e4...223d51 )
by Sergii
04:53
created

Client::includeRoutes()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 7
nop 2
dl 0
loc 26
ccs 0
cts 20
cp 0
crap 90
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
    protected $routePath = null;
23
24
    protected $session;
25
26
    /**
27
     * Session start event
28
     *
29
     * @param \Thruway\ClientSession                $session
30
     * @param \Thruway\Transport\TransportInterface $transport
31
     *
32
     * @author Donii Sergii <[email protected]>
33
     */
34
    public function onSessionStart($session, $transport)
35
    {
36
        $this->includeRoutes($session, $transport);
37
    }
38
39
    public function setRoutePath($path)
40
    {
41
        $this->routePath = $path;
42
    }
43
44
    /**
45
     * Include routes
46
     *
47
     * @param \Thruway\ClientSession                $session   Client session
48
     * @param \Thruway\Transport\TransportInterface $transport Transport provider
49
     *
50
     * @author Donii Sergii <[email protected]>
51
     */
52
    protected function includeRoutes($session, $transport)
53
    {
54
        if (!is_dir($this->routePath) && !is_file($this->routePath)) {
55
            return;
56
        }
57
58
        $session = $this->session = $session;
59
        $client = $this;
60
61
        app()->wampRouter->setClient($client);
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

61
        /** @scrutinizer ignore-call */ 
62
        app()->wampRouter->setClient($client);
Loading history...
62
63
        if (is_file($this->routePath)) {
64
            require $this->routePath;
65
            return;
66
        }
67
68
        if (is_dir($this->routePath)) {
69
            $files = scandir($this->routePath);
70
71
            foreach ($files as $file) {
72
                if ($file === '.' || $file === '..') {
73
                    continue;
74
                }
75
76
                if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
77
                    require $this->routePath . DIRECTORY_SEPARATOR . $file;
78
                }
79
            }
80
        }
81
    }
82
}
83