Completed
Push — master ( 08b3e1...f647c6 )
by Martin
14:11
created

Client   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 173
rs 10
wmc 21
lcom 1
cbo 6

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A connection() 0 4 1
A channel() 0 4 1
A user() 0 4 1
A socket() 0 4 1
A connect() 0 16 1
A read() 0 4 1
A write() 0 7 1
A logCore() 0 14 3
A log() 0 7 1
A logNewLine() 0 4 1
A logLine() 0 4 1
A logHeading() 0 12 1
A logDebug() 0 4 1
A logRead() 0 4 1
A logWrite() 0 4 1
B run() 0 26 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Spires\Irc;
5
6
use Spires\Contracts\Core\Core;
7
use Spires\Core\Dispatcher;
8
use Spires\Irc\Message\Inbound\RawMessage;
9
10
class Client
11
{
12
    /**
13
     * @var Core
14
     */
15
    private $core;
16
17
    /**
18
     * @var Connection
19
     */
20
    private $connection;
21
22
    /**
23
     * @var User
24
     */
25
    private $user;
26
27
    /**
28
     * @var resource
29
     */
30
    private $socket;
31
32
    /**
33
     * @var Dispatcher
34
     */
35
    private $dispatcher;
36
37
    public function __construct(Core $core, Connection $connection, User $user, Dispatcher $dispatcher)
38
    {
39
        $this->core = $core;
40
        $this->connection = $connection;
41
        $this->user = $user;
42
        $this->dispatcher = $dispatcher;
43
    }
44
45
    public function connection() : Connection
46
    {
47
        return $this->connection;
48
    }
49
50
    public function channel() : string
51
    {
52
        return $this->connection()->channel();
53
    }
54
55
    public function user() : User
56
    {
57
        return $this->user;
58
    }
59
60
    public function socket() : resource
61
    {
62
        return $this->socket;
63
    }
64
65
    public function connect()
66
    {
67
        $this->logHeading('Spires connecting');
68
69
        $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
70
71
        $isConnected = socket_connect(
0 ignored issues
show
Unused Code introduced by
$isConnected is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72
            $this->socket,
73
            $this->connection()->server(),
74
            $this->connection()->port()
75
        );
76
77
        $this->write("NICK {$this->user()->nickname()}");
78
        $this->write("USER {$this->user()->username()} {$this->user()->usermode()} * :{$this->user()->realname()}");
79
        $this->write("JOIN {$this->connection()->channel()}");
80
    }
81
82
    public function read()
83
    {
84
        return socket_read($this->socket, 2048, PHP_NORMAL_READ);
85
    }
86
87
    public function write(string $response)
88
    {
89
        $response = trim($response);
90
91
        $this->logWrite($response);
92
        return socket_write($this->socket, $response . "\r\n");
93
    }
94
95
    public function logCore(Core $core)
96
    {
97
        $this->logHeading('Spires booted');
98
99
        $this->logDebug("Providers:");
100
        foreach ($core->getLoadedProviders() as $provider => $active) {
101
            $this->logDebug("  - " . $provider);
102
        }
103
104
        $this->logDebug("Plugins:");
105
        foreach ($core->getPlugins() as $name => $plugin) {
106
            $this->logDebug("  - " . $name);
107
        }
108
    }
109
110
    public function log(string $title, string $string)
111
    {
112
        $time = date('H:i:s');
113
        $title = str_pad($title, 8, ' ', STR_PAD_RIGHT);
114
115
        fwrite(STDOUT, "[{$time}|{$title}]: " . $string . "\n");
116
    }
117
118
    public function logNewLine()
119
    {
120
        fwrite(STDOUT, "\n");
121
    }
122
123
    public function logLine()
124
    {
125
        fwrite(STDOUT, str_repeat('_', 80) . "\n");
126
    }
127
128
    public function logHeading(string $title)
129
    {
130
        $title = str_repeat('=', 5) . ' ' . $title . ' ';
131
        $title = str_pad($title, 60, '=', STR_PAD_RIGHT);
132
        $line = str_repeat('=', 60);
133
134
        $this->logNewLine();
135
        $this->log('debug', $line);
136
        $this->log('debug', $title);
137
        $this->log('debug', $line);
138
        $this->logNewLine();
139
    }
140
141
    public function logDebug(string $string)
142
    {
143
        $this->log('debug', $string);
144
    }
145
146
    public function logRead(string $string)
147
    {
148
        $this->log('read', $string);
149
    }
150
151
    public function logWrite(string $string)
152
    {
153
        $this->log('write', $string);
154
    }
155
156
    public function run()
157
    {
158
        $this->logHeading('Spires listening');
159
160
        $parser = new Parser();
161
162
        while ($raw = $this->read()) {
163
            if (!$raw = trim($raw)) {
164
                continue;
165
            }
166
            $this->logLine();
167
            $this->logRead($raw);
168
            $messageArray = $parser->parse($raw . "\r\n");
169
170
            $message = new RawMessage(
171
                $messageArray['nickname'],
172
                $messageArray['username'],
173
                $messageArray['hostname'],
174
                $messageArray['serverName'],
175
                $messageArray['command'],
176
                $messageArray['params']
177
            );
178
179
            $this->dispatcher->dispatch($message);
180
        }
181
    }
182
}
183