LivereloadProtocol::sendCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Fructify\Reload\Protocol;
3
4
use React\Socket\Connection as SocketConnection;
5
use Fructify\Reload\Response\ResponseWebSocketFrame;
6
use Fructify\Reload\Application\ServerApplication;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class LivereloadProtocol
10
{
11
    protected $conn;
12
    protected $app;
13
    protected $connected = false;
14
15
    public function __construct(SocketConnection $conn, ServerApplication $app)
16
    {
17
        $this->conn = $conn;
18
        $this->app = $app;
19
        $this->app->addClient($this);
20
        $this->initEvent();
21
    }
22
23
    public function reload($file, $config)
24
    {
25
        $this->app->getOutput()->writeln(strftime('%T')." - info - Browser reload $file", OutputInterface::VERBOSITY_VERBOSE);
26
        $this->sendCommand(array(
27
            'command' => 'reload',
28
            'path' => $file,
29
            'liveCSS' => $config['liveCSS'],
30
        ));
31
    }
32
33
    protected function shutdown()
34
    {
35
        $this->conn->removeAllListeners();
36
        $this->app->getOutput()->writeln(strftime('%T')." - info - Browser disconnected", OutputInterface::VERBOSITY_VERBOSE);
37
        $this->app->removeClient($this);
38
        unset($this->app);
39
        unset($this->conn);
40
    }
41
42
    protected function initEvent()
43
    {
44
        $this->conn->on('command', function($command){
45
            $this->dispatchCommand($command);
46
        });
47
        $this->conn->on('close', function(){
48
            $this->shutdown();
49
        });
50
    }
51
52
    protected function sendRaw($data)
53
    {
54
        $response = new ResponseWebSocketFrame(WebSocket\Frame::generate($data));
55
        $this->conn->write($response);
56
    }
57
58
    protected function sendCommand($command)
59
    {
60
        $this->sendRaw(json_encode($command));
61
    }
62
63
    protected function dispatchCommand($command)
64
    {
65
        switch($command['command']){
66
            case 'hello':
67
                $this->processCommandHello($command);
68
                break;
69
            default:
70
                //$this->conn->end();
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
        }
72
    }
73
74
    protected function processCommandHello($command)
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        if($this->connected){
77
           return;
78
        }
79
        $this->app->getOutput()->writeln(strftime('%T')." - info - Livereload protocol initialized.", OutputInterface::VERBOSITY_VERBOSE);
80
        $this->connected = true;
81
        $this->sendCommand(array(
82
            'command' => 'hello',
83
            'protocols' => array(
84
                'http://livereload.com/protocols/official-7',
85
            ),
86
            'serverName' => 'php-livereload',
87
        ));
88
    }
89
}