BotCommand::startBot()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 53
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 53
rs 7.5251
cc 7
eloc 35
nc 2
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Xdaysaysay\CoreBundle\Command;
4
5
use Phergie\Irc\Client\React\Client;
6
use Phergie\Irc\Client\React\WriteStream;
7
use Phergie\Irc\Connection;
8
use Phergie\Irc\ConnectionInterface;
9
use Psr\Log\LoggerInterface;
10
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Xdaysaysay\CoreBundle\Entity\IRCServer;
15
use Xdaysaysay\CoreBundle\Entity\Team;
16
17
class BotCommand extends ContainerAwareCommand
18
{
19
    private static $ircServers = [];
20
21
    /**
22
     * @inheritdoc
23
     *
24
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
25
     */
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('xdaysaysay:bot')
30
            ->setDescription('Bot commands')
31
            ->addArgument(
32
                'option',
33
                InputArgument::REQUIRED,
34
                'start|stop|restart|status'
35
            );
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $option = $input->getArgument('option');
44
        switch ($option) {
45
            case 'start':
46
                $this->start($output);
47
                break;
48
            case 'stop':
49
                break;
50
            case 'restart':
51
                break;
52
            case 'status':
53
                break;
54
        }
55
    }
56
57
    private function initializeServers()
58
    {
59
        $ircServers = $this->getContainer()->get('doctrine')->getRepository('XdaysaysayCoreBundle:IRCServer')->findAll();
60
        foreach ($ircServers as $ircServer) {
61
            $teams = [];
62
            if (!$ircServer->getXdccnames()->isEmpty()) {
63
                foreach ($ircServer->getXdccnames() as $xdccName) {
64
                    if ($xdccName->getXdcc()->getVisible()) {
65
                        foreach ($ircServer->getTeams() as $team) {
66
                            $teams[] = $team;
67
                        }
68
                        if (empty(self::$ircServers[$ircServer->getHost()])) {
69
                            self::$ircServers[$ircServer->getHost()] = ['ircServer' => $xdccName->getIrcServer(), 'status' => 'offline'];
70
                        }
71
                    }
72
                }
73
            }
74
            if (!empty(self::$ircServers[$ircServer->getHost()])) {
75
                self::$ircServers[$ircServer->getHost()]['teams'] = $teams;
76
            }
77
        }
78
    }
79
80
    private function start(OutputInterface $output)
81
    {
82
        $this->initializeServers();
83
        // Test
84
        self::$ircServers = [];
85
        $ircServer = $this->getContainer()->get('doctrine')->getRepository('XdaysaysayCoreBundle:IRCServer')->find(7);
86
        $team = $this->getContainer()->get('doctrine')->getRepository('XdaysaysayCoreBundle:Team')->find(26);
87
        self::$ircServers[$ircServer->getHost()] = ['ircServer' => $ircServer, 'status' => 'offline', 'teams' => [$team]]; // Otaku-IRC
88
        // End Test
89
        foreach (self::$ircServers as $ircServer) {
90
            if ($ircServer['status'] === 'offline') {
91
                $this->startBot($output, $ircServer['ircServer']);
92
            }
93
        }
94
    }
95
96
    private function startBot(OutputInterface $output, IRCServer $ircServer)
97
    {
98
        $connection = new Connection();
99
100
        $connection
101
            ->setServerHostname($ircServer->getHost())
102
            ->setNickname('xdaysaysay')
103
            ->setUsername('xdaysaysay')
104
            ->setHostname('xdaysaysay')
105
            ->setRealname('xdaysaysay')
106
            ->setOption('write', new WriteStream());
107
        if ($ircServer->getPortSsl()) {
108
            $connection->setOption('transport', 'ssl');
109
            $connection->setServerPort($ircServer->getPortSsl());
110
        } else {
111
            $connection->setServerPort($ircServer->getPort());
112
        }
113
114
        $output->writeln('Connection to '.$connection->getServerHostname());
115
116
        $client = new Client();
117
118
        $client->on('irc.received', function ($message, WriteStream $write, Connection $connection, LoggerInterface $logger) {
0 ignored issues
show
Unused Code introduced by
The parameter $connection 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...
Unused Code introduced by
The parameter $logger 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...
119
            if ($message['command'] !== 'JOIN') {
120
                return;
121
            }
122
            $channel = $message['params']['channels'];
123
            $nick = $message['nick'];
124
            $write->ircPrivmsg($channel, 'Welcome '.$nick.'!');
125
        });
126
127
        $client->on('connect.after.each', function (Connection $connection, WriteStream $write) use ($output, $client) {
128
            $output->writeln('Connected to '.$connection->getServerHostname());
129
            if ($write) {
130
                /** @var Team $team */
131
                foreach (self::$ircServers[$connection->getServerHostname()]['teams'] as $team) {
132
                    $client->addTimer(15, function() use($write, $team) {
133
                        $join = $write->ircJoin($team->getChanNameStaff(), $team->getChanNameStaffPassword() ? : null);
134
                        dump($join);
135
                        $join = $write->ircJoin($team->getChanName(), $team->getChanNamePassword() ? : null);
136
                        dump($join);
137
                    });
138
                }
139
            }
140
        });
141
142
        $client->on('connect.error', function (\Exception $exception, ConnectionInterface $connection, LoggerInterface $logger) {
143
            dump('connect.error');
144
            $logger->debug('Connection to '.$connection->getServerHostname().' lost: '.$exception->getMessage());
145
        });
146
//        $client->setTickInterval()
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
147
        $client->run($connection);
148
    }
149
}