Connection   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 161
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 3

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A connect() 0 13 2
A isConnected() 0 4 2
A log() 0 6 2
B connectionLoop() 0 26 6
A getSocket() 0 4 1
A setSocket() 0 4 1
A handleRaw() 0 5 1
A handleTick() 0 4 1
A disconnect() 0 11 3
A sendMessage() 0 4 1
A sendRaw() 0 5 1
A setLogger() 0 4 1
1
<?php
2
namespace Buttress\IRC\Connection;
3
4
use Buttress\IRC\Action\ActionManagerInterface;
5
use Buttress\IRC\Message\MessageInterface;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\LogLevel;
8
9
class Connection implements ConnectionInterface
10
{
11
12
    protected $connected = false;
13
14
    /**
15
     * @type resource
16
     */
17
    protected $socket;
18
    protected $server;
19
    protected $port;
20
21
    /**
22
     * @var LoggerInterface
23
     */
24
    protected $logger;
25
26
    /**
27
     * @var ActionManagerInterface
28
     */
29
    protected $manager;
30
31
    public function __construct(ActionManagerInterface $manager, $server, $port = 6667)
32
    {
33
        $this->server = $server;
34
        $this->port = $port;
35
        $this->manager = $manager;
36
    }
37
38
    public function connect()
39
    {
40
        if ($this->isConnected()) {
41
            return false;
42
        }
43
44
        $this->log("Connecting to {$this->server} : {$this->port}");
45
46
        $this->connected = true;
47
        $this->connectionLoop();
48
49
        return true;
50
    }
51
52
    /**
53
     * @return bool
54
     */
55
    public function isConnected()
56
    {
57
        return ($this->connected && is_resource($this->socket));
58
    }
59
60
    public function log($message, array $context = array(), $level = LogLevel::NOTICE)
61
    {
62
        if ($this->logger) {
63
            $this->logger->log($level, $message, $context);
64
        }
65
    }
66
67
    protected function connectionLoop()
68
    {
69
        $socket = $this->getSocket();
70
        if (!$socket) {
71
            $this->socket = $socket = fsockopen($this->server, $this->port);
72
            $this->manager->handleConnect($this);
73
            socket_set_blocking($this->socket, false);
74
        }
75
76
        while ($this->isConnected() && !feof($socket)) {
77
            $raw = fgets($socket);
78
79
            if ($raw) {
80
                $this->handleRaw($raw);
81
            }
82
83
            $this->handleTick();
84
85
            if ($raw === false) {
86
                // no data, slow down so we don't burn cpu cycles
87
                usleep(10000);
88
            }
89
        }
90
91
        $this->disconnect();
92
    }
93
94
    /**
95
     * @return resource
96
     */
97
    public function getSocket()
98
    {
99
        return $this->socket;
100
    }
101
102
    /**
103
     * @param resource $socket
104
     */
105
    public function setSocket($socket)
106
    {
107
        $this->socket = $socket;
108
    }
109
110
    /**
111
     * @param string $raw
112
     */
113
    public function handleRaw($raw)
114
    {
115
        $this->log($raw, array(), 'debug');
116
        $this->manager->handleRaw($this, $raw);
117
    }
118
119
    public function handleTick()
120
    {
121
        $this->manager->handleTick($this);
122
    }
123
124
    /**
125
     * @return boolean|null
126
     */
127
    public function disconnect()
128
    {
129
        if ($this->connected && $socket = $this->getSocket()) {
130
            fclose($socket);
131
        }
132
133
        $this->log('Disconnected.');
134
135
        $this->socket = null;
136
        $this->connected = false;
137
    }
138
139
    /**
140
     * @param MessageInterface $message
141
     * @return boolean|null
142
     */
143
    public function sendMessage(MessageInterface $message)
144
    {
145
        $this->sendRaw($message->getRaw());
146
    }
147
148
    /**
149
     * @param string $raw
150
     * @return boolean|null
151
     */
152
    public function sendRaw($raw)
153
    {
154
        $this->log("Sending {$raw}");
155
        fwrite($this->getSocket(), $raw . PHP_EOL);
156
    }
157
158
    /**
159
     * Sets a logger instance on the object
160
     *
161
     * @param LoggerInterface $logger
162
     * @return null
163
     */
164
    public function setLogger(LoggerInterface $logger)
165
    {
166
        $this->logger = $logger;
167
    }
168
169
}
170