Completed
Push — master ( 80fdf3...351fbc )
by Vladimir
02:43
created

RecalculateEloCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/**
3
 * This symfony command will recalculate Elo matches
4
 *
5
 * @package    BZiON
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
namespace BZIon\Command;
10
11
use Match;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class RecalculateEloCommand extends Command
17
{
18
    protected function configure()
19
    {
20
        $this
21
            ->setName('bzion:recalc-elo')
22
            ->setDescription('Recalculate Elos for a given match and all of the matches proceeding it.')
23
            ->addArgument('matchID', InputArgument::REQUIRED, 'The ID of the first match that needs to be recalculated.')
24
        ;
25
    }
26
27
    protected function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        $id = $input->getArgument('matchID');
30
        $match = Match::get($id);
31
32
        if (!$match->isValid()) {
33
            $output->writeln(sprintf('No match found with id: %d', $id));
34
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method execute() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
35
        }
36
37
        $output->writeln(sprintf('Starting Elo recalculation with match #%d...', $id));
38
        $output->writeln(sprintf('  (this may take a while)'));
39
40
        try {
41
            Match::recalculateMatchesSince($match, function ($event) use ($output) {
42
                switch ($event['type']) {
43
                    case 'recalculation.count':
44
                        $output->writeln(sprintf("\nDetected %d matches needing recalculations\n", $event['value']));
45
                        $this->initProgress($output, $event['value']);
46
                        break;
47
48
                    case 'recalculation.progress':
49
                        $this->advance();
50
                        break;
51
52
                    case 'recalculation.complete':
53
                        $this->finishProgress();
54
                        break;
55
56
                    default:
57
                        $output->writeln(sprintf('Received unknown recalculation event: %s', $event['type']));
58
                }
59
            });
60
        }
61
        catch (\Exception $e) {
62
            $errMsg = $e->getMessage();
63
64
            $output->writeln(<<<EXCEPTION
65
            
66
<bg=red;options=bold>
67
 [ERROR] An exception occurred with the following message:
68
   ${$errMsg}
69
</>
70
EXCEPTION
71
);
72
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method execute() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
73
        }
74
75
        $output->writeln(<<<SUCCESS
76
77
<bg=green;options=bold>
78
79
 [OK] Match Elos have been successfully recalculated.
80
</>
81
SUCCESS
82
        );
83
    }
84
}
85