Pool::getConfigDefaults()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
nc 1
nop 0
1
<?php
2
namespace PHPDaemon\Servers\IRCBouncer;
3
4
use PHPDaemon\Core\Daemon;
5
use PHPDaemon\Network\Server;
6
use PHPDaemon\Structures\ObjectStorage;
7
8
class Pool extends Server
9
{
10
11
    /**
12
     * @var Pool
13
     */
14
    public $client;
15
16
    /**
17
     * @var Connection
18
     */
19
    public $conn;
20
21
    /**
22
     * @var boolean
23
     */
24
    public $protologging = false;
25
26
    /**
27
     * @var Pool
28
     */
29
    public $db;
30
31
    /**
32
     * @var object
33
     */
34
    public $messages;
35
36
    /**
37
     * @var object
38
     */
39
    public $channels;
40
41
    /**
42
     * @TODO DESCR
43
     */
44
    protected function init()
45
    {
46
        $this->client = Pool::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \PHPDaemon\Servers\IRCBouncer\Pool::getInstance() of type object<PHPDaemon\Network\this> is incompatible with the declared type object<PHPDaemon\Servers\IRCBouncer\Pool> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
        $this->client->protologging = $this->protologging;
48
        $this->db = Pool::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \PHPDaemon\Servers\IRCBouncer\Pool::getInstance() of type object<PHPDaemon\Network\this> is incompatible with the declared type object<PHPDaemon\Servers\IRCBouncer\Pool> of property $db.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
        $this->messages = $this->db->{$this->config->dbname->value . '.messages'};
0 ignored issues
show
Bug introduced by
The property dbname does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
50
        $this->channels = $this->db->{$this->config->dbname->value . '.channels'};
51
    }
52
53
    /**
54
     * Setting default config options
55
     * Overriden from ConnectionPool::getConfigDefaults
56
     * @return array|bool
57
     */
58
    protected function getConfigDefaults()
59
    {
60
        return [
61
            /* [string|array] Listen addresses */
62
            'listen' => '0.0.0.0',
63
64
            /* [integer] Listen port */
65
            'port' => 6667,
66
67
            /* [string] URL */
68
            'url' => 'irc://user@host/nickname/realname',
69
70
            /* [string] Server name */
71
            'servername' => 'bnchost.tld',
72
73
            /* [string] Default channels */
74
            'defaultchannels' => '',
75
76
            /* [boolean] Logging? */
77
            'protologging' => 0,
78
79
            /* [string] Database name */
80
            'dbname' => 'bnc',
81
82
            /* [string] Password */
83
            'password' => 'SecretPwd',
84
        ];
85
    }
86
87
    /**
88
     * @TODO DESCR
89
     */
90
    public function applyConfig()
91
    {
92
        parent::applyConfig();
93
        $this->protologging = (bool)$this->config->protologging->value;
0 ignored issues
show
Bug introduced by
The property protologging does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
94
        if (isset($this->client)) {
95
            $this->client->protologging = $this->protologging;
96
        }
97
    }
98
99
    /**
100
     * @TODO DESCR
101
     */
102
    public function onReady()
103
    {
104
        parent::onReady();
105
        $this->client->onReady();
106
        $this->getConnection($this->config->url->value);
0 ignored issues
show
Bug introduced by
The property url does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
107
    }
108
109
    /**
110
     * @TODO DESCR
111
     * @param string $url
112
     */
113
    public function getConnection($url)
114
    {
115
        $this->client->getConnection($url, function ($conn) use ($url) {
0 ignored issues
show
Unused Code introduced by
The call to Pool::getConnection() has too many arguments starting with function ($conn) use($ur... . $url . ')'); } }.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116
            $this->conn = $conn;
117
            $conn->attachedClients = new ObjectStorage;
118
            if ($conn->connected) {
119
                Daemon::log('IRC bot connected at ' . $url);
120
                $conn->join($this->config->defaultchannels->value);
0 ignored issues
show
Bug introduced by
The property defaultchannels does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
121
                $conn->bind('motd', function ($conn) {
0 ignored issues
show
Unused Code introduced by
The parameter $conn 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...
122
                    //Daemon::log($conn->motd);
123
                });
124
                foreach ($this as $bouncerConn) {
125
                    if (!$bouncerConn->attachedServer) {
126
                        $bouncerConn->attachTo($conn);
127
                    }
128
                }
129
                $conn->bind('command', function ($conn, $from, $cmd, $args) {
130
                    if ($cmd === 'PONG') {
131
                        return;
132
                    } elseif ($cmd === 'PING') {
133
                        return;
134
                    }
135
                    if ($from['orig'] === $conn->servername) {
136
                        $from['orig'] = $this->config->servername->value;
0 ignored issues
show
Bug introduced by
The property servername does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
137
                    }
138
                    $conn->attachedClients->each('commandArr', $from['orig'], $cmd, $args);
139
                });
140
                $conn->bind('privateMsg', function ($conn, $msg) {
141
                    Daemon::log('IRCBouncer: got private message \'' . $msg['body'] . '\' from \'' . $msg['from']['orig'] . '\'');
142
                });
143
                $conn->bind('msg', function ($conn, $msg) {
144
                    $msg['ts'] = microtime(true);
145
                    $msg['dir'] = 'i';
146
                    $this->messages->insert($msg);
147
                });
148
                $conn->bind('disconnect', function ($conn) use ($url) {
149
                    foreach ($this as $bouncerConn) {
150
                        if ($bouncerConn->attachedServer === $conn) {
151
                            $bouncerConn->detach();
152
                        }
153
                    }
154
                    $this->getConnection($url);
155
                });
156
            } else {
157
                Daemon::log('IRCBouncer: unable to connect (' . $url . ')');
158
            }
159
        });
160
    }
161
}
162