Completed
Push — master ( 5bf4c0...313184 )
by Kirill
02:02
created

DeployCommand::execute()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 36
rs 8.439
cc 5
eloc 23
nc 9
nop 2
1
<?php
2
3
namespace Chrl\AppBundle\Command;
4
5
use Chrl\AppBundle\Entity\Game;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputDefinition;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Input\InputInterface;
11
12
class DeployCommand extends ContainerAwareCommand
13
{
14
    // This is just a normal Command::configure() method
15
    protected function configure()
16
    {
17
        $this->setName('buktopuha:deploy')
18
                ->setDescription('Tell all active games about deploy')
19
                ->setDefinition(
20
                    new InputDefinition(
21
                        [
22
                            new InputOption('success')
23
                        ]
24
                    )
25
                );
26
    }
27
28
    // Execute will be called in a endless loop
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
        $output->writeln('Executing...');
32
33
        $gs = $this->getContainer()->get('app.gameservice');
34
        $tgApi = $this->getContainer()->get('buktopuha.telegram_bot_api');
35
        $games = $gs->getActiveGames();
36
37
        if (false == $input->getOption('success')) {
38
            if (file_exists('var/logs/deploy.version')) {
39
                $version = file_get_contents('var/logs/deploy.version');
40
            } else {
41
                $version = 0;
42
                file_put_contents('var/logs/deploy.version', $version);
43
            }
44
45
            $version++;
46
            file_put_contents('var/logs/deploy.version', $version);
47
        }
48
        
49
        /** @var Game $game */
50
        foreach ($games as $game) {
51
            if ($input->getOption('success')) {
52
                $tgApi->sendMessage(
53
                    $game->chatId,
54
                    'Bot is back online (updated to build '.$version.')! The game continues...'
0 ignored issues
show
Bug introduced by
The variable $version does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
55
                );
56
            } else {
57
                $tgApi->sendMessage(
58
                    $game->chatId,
59
                    'Bot rebooting due to deploy (build '.$version
60
                    .'), sorry. The game will continue once bot boots again.'
61
                );
62
            }
63
        }
64
    }
65
}
66