ExampleIRCBot::connect()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
nc 1
nop 0
1
<?php
2
namespace PHPDaemon\Examples;
3
4
/**
5
 * @package    Examples
6
 * @subpackage Base
7
 *
8
 * @author     Vasily Zorin <[email protected]>
9
 */
10
11
class ExampleIRCBot extends \PHPDaemon\Core\AppInstance
12
{
13
    public $client;
14
    public $conn;
15
16
    /**
17
     * Constructor.
18
     * @return void
19
     */
20
    public function init()
21
    {
22
        if ($this->isEnabled()) {
23
            $this->client = \PHPDaemon\Clients\IRC\Pool::getInstance();
24
        }
25
    }
26
27
    /**
28
     * Called when the worker is ready to go.
29
     * @return void
30
     */
31
    public function onReady()
32
    {
33
        if ($this->client) {
34
            $this->client->onReady();
35
            $this->connect();
36
        }
37
    }
38
39
    public function connect()
40
    {
41
        $app = $this;
42
        $r = $this->client->getConnection($this->config->url->value, function ($conn) use ($app) {
0 ignored issues
show
Unused Code introduced by
$r 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...
43
            $app->conn = $conn;
44
            if ($conn->connected) {
45
                \PHPDaemon\Core\Daemon::log('IRC bot connected at ' . $this->config->url->value);
46
                $conn->join('#botwar_phpdaemon');
47
                $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...
48
                    //\PHPDaemon\Daemon::log($conn->motd);
49
                });
50
                $conn->bind('privateMsg', function ($conn, $msg) {
51
                    \PHPDaemon\Core\Daemon::log('IRCBot: got private message \'' . $msg['body'] . '\' from \'' . $msg['from']['orig'] . '\'');
52
                    $conn->message($msg['from']['nick'], 'You just wrote: ' . $msg['body']); // send the message back
53
                });
54
                $conn->bind('disconnect', function () use ($app) {
55
                    $app->connect();
56
                });
57
            } else {
58
                \PHPDaemon\Core\Daemon::log('IRCBot: unable to connect (' . $this->config->url->value . ')');
59
            }
60
        });
61
    }
62
63
    /**
64
     * Called when worker is going to update configuration.
65
     * @return void
66
     */
67
    public function onConfigUpdated()
68
    {
69
        if ($this->client) {
70
            $this->client->config = $this->config;
71
            $this->client->onConfigUpdated();
72
        }
73
    }
74
75
    /**
76
     * Called when application instance is going to shutdown.
77
     * @return boolean Ready to shutdown?
78
     */
79
    public function onShutdown()
80
    {
81
        if ($this->client) {
82
            return $this->client->onShutdown();
83
        }
84
        return true;
85
    }
86
87
    /**
88
     * Setting default config options
89
     * Overriden from AppInstance::getConfigDefaults
90
     * Uncomment and return array with your default options
91
     * @return array|false
92
     */
93
    protected function getConfigDefaults()
94
    {
95
        $random = sprintf('%x', crc32(posix_getpid() . "\x00" . microtime(true)));
96
        return [
97
            'url' => 'irc://guest_' . $random . ':[email protected]/Bot_phpDaemon'
98
        ];
99
    }
100
}
101