Completed
Push — master ( 6323ac...52893e )
by
unknown
6s
created

Client::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 19
rs 9.4285
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
        socket_connect(
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
169
            $message = RawMessage::fromArray(
170
                $parser->parse($raw . "\r\n")
171
            );
172
            $this->dispatcher->dispatch($message);
173
        }
174
    }
175
}
176