Completed
Pull Request — master (#165)
by
unknown
02:58
created

GenDummyRecordsCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 161
Duplicated Lines 9.94 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 19.48%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 8
dl 16
loc 161
rs 10
c 0
b 0
f 0
ccs 15
cts 77
cp 0.1948

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 16 1
A configure() 0 15 1
C execute() 0 84 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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 MapQueryBuilder    $mapQuery
64
     * @param Console            $console
65
     */
66 131 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...
67
        Connection $connection,
68
        MapStorage $mapStorage,
69
        PlayerQueryBuilder $playerQueryBuilder,
70
        RecordQueryBuilder $recordQueryBuilder,
71
        MapQueryBuilder $mapQuery,
72
        Console $console
73
    ) {
74 131
        parent::__construct();
75 131
        $this->mapStorage = $mapStorage;
76 131
        $this->playerQueryBuilder = $playerQueryBuilder;
77 131
        $this->recordQueryBuilder = $recordQueryBuilder;
78 131
        $this->mapQuery = $mapQuery;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mapQuery of type object<eXpansion\Bundle\...\Model\MapQueryBuilder> is incompatible with the declared type object<eXpansion\Bundle\Maps\Model\Base\MapQuery> of property $mapQuery.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79 131
        $this->console = $console;
80 131
        $this->connection = $connection;
81 131
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 131
    protected function configure()
87
    {
88
        $this
89
            // the name of the command (the part after "bin/console")
90 131
            ->setName('eXpansion:testing:generateTestData')
91
            // the short description shown while running "php bin/console list"
92 131
            ->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 131
            ->setHelp('This command generates defined number of users and records to each map...');
96
        $this
97
            // configure an argument
98 131
            ->addArgument('count', InputArgument::REQUIRED, 'The number of users and records to generate.');
99
100 131
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public function execute(InputInterface $input, OutputInterface $output)
106
    {
107
        $this->console->init($output, null);
108
109
        $count = $input->getArgument('count');
110
        $this->console->writeln('Checking if there\'s enough players');
111
112
        $preAdd = $this->playerQueryBuilder->findDummy();
113
        if (count($preAdd) <= $count) {
114
115
            $this->console->writeln("Generating missing players up to $count");
116
            $progress = new ProgressBar($output, $count);
117
            $progress->start();
118
            $progress->advance(count($preAdd));
119
120
            $con = Propel::getWriteConnection(PlayerTableMap::DATABASE_NAME);
121
            $con->beginTransaction();
122
123
            for ($x = count($preAdd); $x < $count; $x++) {
124
                $player = new Player();
125
                $login = "dummyplayer_$x";
126
                $player->setNickname($login);
127
                $player->setNicknameStripped($login);
128
                $player->setLogin($login);
129
                $player->setPath("World|Hello");
130
                $player->save();
131
                $progress->advance();
132
            }
133
            $con->commit();
134
            $progress->finish();
135
136
        }
137
        unset($preAdd);
138
139
        $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...
140
141
        $this->console->writeln("Generating maximum of $count records for all maps on server");
142
        $maps = $this->mapStorage->getMaps();
143
        $count = count($maps);
144
145
146
        $con = Propel::getWriteConnection(RecordTableMap::DATABASE_NAME);
147
        $i = 1;
148
        foreach ($maps as $m => $map) {
149
            $records = $this->recordQueryBuilder->getMapRecords($map->uId, $map->nbLaps, "asc", 1000);
150
            $idsUsed = [];
151
            if (count($records) <= $count - 1) {
152
                foreach ($records as $record) {
153
                    $idsUsed[] = $record->getPlayerId();
154
                }
155
                $this->console->writeln("Generating records for map ".$i."/".count($maps)." -> ".$map->name);
156
                $con->beginTransaction();
157
158
                $record = new Record();
159
                $record->setNblaps(1);
160
                $record->setNbFinish(1);
161
                $record->setMapuid($map->uId);
162
                $record->setCreatedAt(new \DateTime());
163
                $record->setCheckpoints([]);
164
                $progress = new ProgressBar($output, $count);
165
                $progress->start();
166
                for ($x = (count($records) + 1); $x < $count; $x++) {
167
                    if (!in_array($x, $idsUsed)) {
168
                        $rec = clone $record;
169
                        $rec->setScore(mt_rand($map->goldTime, $map->goldTime * 5));
170
                        $rec->setPlayerId($x);
171
                        $rec->save();
172
                        $progress->advance();
173
                    }
174
                }
175
                $con->commit();
176
                $progress->finish();
177
                $i++;
178
                PlayerTableMap::clearInstancePool();
179
                PlayerTableMap::clearRelatedInstancePool();
180
                RecordTableMap::clearInstancePool();
181
                RecordTableMap::clearRelatedInstancePool();
182
183
            } else {
184
                $this->console->writeln("skipping map, records alreaady set!");
185
            }
186
        }
187
188
    }
189
}
190