GenDummyRecordsCommand::execute()   B
last analyzed

Complexity

Conditions 8
Paths 16

Size

Total Lines 85

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 85
ccs 0
cts 61
cp 0
rs 7.0828
c 0
b 0
f 0
cc 8
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\Bundle\LocalRecords\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\Bundle\Maps\Model\MapQueryBuilder;
10
use eXpansion\Framework\Core\Services\Console;
11
use eXpansion\Framework\Core\Storage\MapStorage;
12
use eXpansion\Framework\PlayersBundle\Model\Map\PlayerTableMap;
13
use eXpansion\Framework\PlayersBundle\Model\Player;
14
use eXpansion\Framework\PlayersBundle\Model\PlayerQueryBuilder;
15
use Maniaplanet\DedicatedServer\Connection;
16
use Propel\Runtime\Propel;
17
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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 GenDummyRecordsCommand extends ContainerAwareCommand
30
{
31
    /**
32
     * @var MapStorage
33
     */
34
    private $mapStorage;
35
    /**
36
     * @var PlayerQueryBuilder
37
     */
38
    private $playerQueryBuilder;
39
    /**
40
     * @var RecordQueryBuilder
41
     */
42
    private $recordQueryBuilder;
43
    /**
44
     * @var Console
45
     */
46
    private $console;
47
    /**
48
     * @var MapQueryBuilder
49
     */
50
    private $mapQuery;
51
52
    /**
53
     * ScriptPanel constructor.
54
     *
55
     * @param MapStorage         $mapStorage
56
     * @param PlayerQueryBuilder $playerQueryBuilder
57
     * @param RecordQueryBuilder $recordQueryBuilder
58
     * @param MapQueryBuilder    $mapQuery
59
     * @param Console            $console
60
     */
61 2 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
        MapStorage $mapStorage,
63
        PlayerQueryBuilder $playerQueryBuilder,
64
        RecordQueryBuilder $recordQueryBuilder,
65
        MapQueryBuilder $mapQuery,
66
        Console $console
67
    ) {
68 2
        parent::__construct();
69 2
        $this->mapStorage = $mapStorage;
70 2
        $this->playerQueryBuilder = $playerQueryBuilder;
71 2
        $this->recordQueryBuilder = $recordQueryBuilder;
72 2
        $this->mapQuery = $mapQuery;
73 2
        $this->console = $console;
74 2
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79 2
    protected function configure()
80
    {
81
        $this
82
            // the name of the command (the part after "bin/console")
83 2
            ->setName('eXpansion:testing:generateTestData')
84
            // the short description shown while running "php bin/console list"
85 2
            ->setDescription('creates defined number of users with records to all maps.')
86
            // the full command description shown when running the command with
87
            // the "--help" option
88 2
            ->setHelp('This command generates defined number of users and records to each map...');
89
        $this
90
            // configure an argument
91 2
            ->addArgument('count', InputArgument::REQUIRED, 'The number of users and records to generate.');
92
93 2
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function execute(InputInterface $input, OutputInterface $output)
99
    {
100
        $this->console->init($output, null);
101
102
        $count = $input->getArgument('count');
103
        $this->console->writeln('Checking if there\'s enough players');
104
105
        $preAdd = $this->playerQueryBuilder->findDummy();
106
107
        if (count($preAdd) < $count) {
108
109
            $this->console->writeln("Generating missing players up to $count");
110
            $progress = new ProgressBar($output, $count);
0 ignored issues
show
Documentation introduced by
$count is of type string|array<integer,string>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
            $progress->start();
112
            $progress->advance(count($preAdd));
113
114
            $con = Propel::getWriteConnection(PlayerTableMap::DATABASE_NAME);
115
            $con->beginTransaction();
116
117
            for ($x = count($preAdd); $x < $count; $x++) {
118
                $player = new Player();
119
                $login = "dummyplayer_$x";
120
                $player->setNickname($login);
121
                $player->setNicknameStripped($login);
122
                $player->setLogin($login);
123
                $player->setPath("World|Hello");
124
                $player->save();
125
                $progress->advance();
126
            }
127
            $con->commit();
128
            $progress->finish();
129
        }
130
131
        unset($preAdd);
132
133
        /** @var Player[] $players */
134
        $players = $this->playerQueryBuilder->findDummy();
135
136
        $this->console->writeln("Generating maximum of $count records for all maps on server");
137
        $maps = $this->mapQuery->getAllMaps();
138
139
        $con = Propel::getWriteConnection(RecordTableMap::DATABASE_NAME);
140
        $i = 1;
141
        foreach ($maps as $m => $map) {
142
            $records = $this->recordQueryBuilder->getMapRecords($map->getMapuid(), 1, "asc", 1000);
143
144
145
            $idsUsed = [];
146
            if (count($records) < $count) {
147
                foreach ($records as $record) {
148
                    $idsUsed[] = $record->getPlayerId();
149
                }
150
                $this->console->writeln("Generating records for map ".$i."/".count($maps)." -> ".$map->getName());
151
                $con->beginTransaction();
152
153
                $record = new Record();
154
                $record->setNblaps(1);
155
                $record->setNbFinish(1);
156
                $record->setMapuid($map->getMapuid());
157
                $record->setCreatedAt(new \DateTime());
158
                $record->setCheckpoints([]);
159
                $progress = new ProgressBar($output, $count - count($records));
160
                $progress->start();
161
                for ($x = (count($records) + 1); $x < $count; $x++) {
162
                    if (!in_array($x, $idsUsed)) {
163
                        $rec = clone $record;
164
                        $rec->setScore(mt_rand($map->getGoldtime(), $map->getGoldtime() * 5));
165
                        $rec->setPlayerId($players[$x-1]->getId());
166
                        $rec->save();
167
                        $progress->advance();
168
                    }
169
                }
170
                $con->commit();
171
                $progress->finish();
172
                $i++;
173
                RecordTableMap::clearInstancePool();
174
                RecordTableMap::clearRelatedInstancePool();
175
176
            } else {
177
                $this->console->writeln("skipping map, records alreaady set!");
178
            }
179
        }
180
        PlayerTableMap::clearInstancePool();
181
        PlayerTableMap::clearRelatedInstancePool();
182
    }
183
}
184