Completed
Push — master ( c12270...10319f )
by Sergii
04:52
created

Client   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onSessionStart() 0 3 1
D includeRoutes() 0 27 9
A setRoutePath() 0 3 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
    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 2
    public function onSessionStart($session, $transport)
35
    {
36 2
        $this->includeRoutes($session, $transport);
37 2
    }
38
39 2
    public function setRoutePath($path)
40
    {
41 2
        $this->routePath = $path;
42 2
    }
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 2
    protected function includeRoutes($session, $transport)
53
    {
54 2
        if (!is_dir($this->routePath) && !is_file($this->routePath)) {
55
            return;
56
        }
57
58 2
        $session = $this->session = $session;
59 2
        $client = $this;
60
61
        /** @scrutinizer ignore-call */
62 2
        app()->wampRouter->setClient($client);
63
64 2
        if (is_file($this->routePath)) {
65 1
            require $this->routePath;
66 1
            return;
67
        }
68
69 1
        if (is_dir($this->routePath)) {
70 1
            $files = scandir($this->routePath);
71
72 1
            foreach ($files as $file) {
73 1
                if ($file === '.' || $file === '..') {
74 1
                    continue;
75
                }
76
77 1
                if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
78 1
                    require $this->routePath . DIRECTORY_SEPARATOR . $file;
79
                }
80
            }
81
        }
82 1
    }
83
}
84