Completed
Pull Request — master (#161)
by De Cramer
02:59
created

GenRecordsCommand::execute()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 77
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 6.1476
c 0
b 0
f 0
ccs 0
cts 57
cp 0
cc 8
eloc 57
nc 16
nop 2
crap 72

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 eXpansion\Framework\Core\Command;
4
5
use eXpansion\Bundle\LocalRecords\Model\Map\RecordTableMap;
6
use eXpansion\Bundle\LocalRecords\Model\Record;
7
use eXpansion\Bundle\LocalRecords\Model\RecordQueryBuilder;
8
use eXpansion\Bundle\Maps\Model\Base\MapQuery;
9
use eXpansion\Framework\Core\Services\Console;
10
use eXpansion\Framework\Core\Storage\MapStorage;
11
use eXpansion\Framework\PlayersBundle\Model\Map\PlayerTableMap;
12
use eXpansion\Framework\PlayersBundle\Model\Player;
13
use eXpansion\Framework\PlayersBundle\Model\PlayerQueryBuilder;
14
use Maniaplanet\DedicatedServer\Connection;
15
use Propel\Runtime\Propel;
16
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Helper\ProgressBar;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
24
/**
25
 * Class Records
26
 *
27
 * @author  reaby
28
 */
29
class GenRecordsCommand extends Command
30
{
31
    /**
32
     * @var Connection
33
     */
34
    private $connection;
35
    /**
36
     * @var MapStorage
37
     */
38
    private $mapStorage;
39
    /**
40
     * @var PlayerQueryBuilder
41
     */
42
    private $playerQueryBuilder;
43
    /**
44
     * @var RecordQueryBuilder
45
     */
46
    private $recordQueryBuilder;
47
    /**
48
     * @var Console
49
     */
50
    private $console;
51
    /**
52
     * @var MapQuery
53
     */
54
    private $mapQuery;
55
56
    /**
57
     * ScriptPanel constructor.
58
     *
59
     * @param Connection $connection
60
     * @param MapStorage $mapStorage
61
     * @param PlayerQueryBuilder $playerQueryBuilder
62
     * @param RecordQueryBuilder $recordQueryBuilder
63
     * @param MapQuery $mapQuery
64
     * @param Console $console
65
     */
66
    public function __construct(
67
        Connection $connection,
68
        MapStorage $mapStorage,
69
        PlayerQueryBuilder $playerQueryBuilder,
70
        RecordQueryBuilder $recordQueryBuilder,
71
        MapQuery $mapQuery,
72
        Console $console
73
    ) {
74
        parent::__construct();
75
        $this->mapStorage = $mapStorage;
76
        $this->playerQueryBuilder = $playerQueryBuilder;
77
        $this->recordQueryBuilder = $recordQueryBuilder;
78
        $this->mapQuery = $mapQuery;
79
        $this->console = $console;
80
        $this->connection = $connection;
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    protected function configure()
87
    {
88
        $this
89
            // the name of the command (the part after "bin/console")
90
            ->setName('exp:records:generate')
91
            // the short description shown while running "php bin/console list"
92
            ->setDescription('creates defined number of users with records to all maps.')
93
            // the full command description shown when running the command with
94
            // the "--help" option
95
            ->setHelp('This command generates defined number of users and records to each map...');
96
        $this
97
            // configure an argument
98
            ->addArgument('count', InputArgument::REQUIRED, 'The number of users and records to generate.');
99
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public function execute(InputInterface $input, OutputInterface $output)
106
    {
107
        $count = $input->getArgument('count');
108
109
        $output = $this->console->getConsoleOutput();
110
        $preAdd = $this->playerQueryBuilder->findAll();
111
        if (count($preAdd) <= $count) {
112
113
            $this->console->writeln("Generating missing players up to 1000");
114
            $progress = new ProgressBar($output, $count);
115
            $progress->start();
116
            $progress->advance(count($preAdd));
117
118
            $con = Propel::getWriteConnection(PlayerTableMap::DATABASE_NAME);
119
            $con->beginTransaction();
120
121
            for ($x = count($preAdd); $x < $count; $x++) {
122
                $player = new Player();
123
                $login = "dummyplayer_$x";
124
                $player->setNickname($login);
125
                $player->setNicknameStripped($login);
126
                $player->setLogin($login);
127
                $player->setPath("World|Hello");
128
                $player->save();
129
                $progress->advance();
130
            }
131
            $con->commit();
132
            $progress->finish();
133
134
        }
135
        unset($preAdd);
136
137
        $players = $this->playerQueryBuilder->findAll();
0 ignored issues
show
Unused Code introduced by
$players 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...
138
139
        $this->console->writeln("Generating maximum of 1000 records for all maps on server");
140
        $maps = $this->mapStorage->getMaps();
141
        $count = count($maps);
142
143
144
        $con = Propel::getWriteConnection(RecordTableMap::DATABASE_NAME);
145
        $i = 1;
146
        foreach ($maps as $m => $map) {
147
            $records = $this->recordQueryBuilder->getMapRecords($map->uId, $map->nbLaps, "asc", 1000);
148
            $idsUsed = [];
149
            if (count($records) <= $count - 1) {
150
                foreach ($records as $record) {
151
                    $idsUsed[] = $record->getPlayerId();
152
                }
153
                $this->console->writeln("Generating records for map ".$i."/".count($maps)." -> ".$map->name);
154
                $con->beginTransaction();
155
156
                $record = new Record();
157
                $record->setNblaps(1);
158
                $record->setNbFinish(1);
159
                $record->setMapuid($map->uId);
160
                $record->setCreatedAt(new \DateTime());
161
                $record->setCheckpoints([]);
162
                $progress = new ProgressBar($output, $count);
163
                $progress->start();
164
                for ($x = (count($records) + 1); $x < $count; $x++) {
165
                    if (!in_array($x, $idsUsed)) {
166
                        $rec = clone $record;
167
                        $rec->setScore(mt_rand($map->goldTime, $map->goldTime * 5));
168
                        $rec->setPlayerId($x);
169
                        $rec->save();
170
                        $progress->advance();
171
                    }
172
                }
173
                $con->commit();
174
                $progress->finish();
175
                $i++;
176
            } else {
177
                $this->console->writeln("skipping map, records alreaady set!");
178
            }
179
        }
180
181
    }
182
}
183