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

DelDummyRecordsCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 132
Duplicated Lines 12.12 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 25.92%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 5
dl 16
loc 132
ccs 14
cts 54
cp 0.2592
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 16 1
A configure() 0 11 1
B execute() 0 58 6

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\RecordQueryBuilder;
7
use eXpansion\Bundle\Maps\Model\MapQueryBuilder;
8
use eXpansion\Framework\Core\Services\Console;
9
use eXpansion\Framework\Core\Storage\MapStorage;
10
use eXpansion\Framework\PlayersBundle\Model\Map\PlayerTableMap;
11
use eXpansion\Framework\PlayersBundle\Model\Player;
12
use eXpansion\Framework\PlayersBundle\Model\PlayerQueryBuilder;
13
use Maniaplanet\DedicatedServer\Connection;
14
use Propel\Runtime\Propel;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Helper\ProgressBar;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
21
/**
22
 * Class Records
23
 *
24
 * @author  reaby
25
 */
26
class DelDummyRecordsCommand extends ContainerAwareCommand
27
{
28
    /**
29
     * @var Connection
30
     */
31
    private $connection;
32
    /**
33
     * @var MapStorage
34
     */
35
    private $mapStorage;
36
    /**
37
     * @var PlayerQueryBuilder
38
     */
39
    private $playerQueryBuilder;
40
    /**
41
     * @var RecordQueryBuilder
42
     */
43
    private $recordQueryBuilder;
44
    /**
45
     * @var Console
46
     */
47
    private $console;
48
    /**
49
     * @var MapQueryBuilder
50
     */
51
    private $mapQuery;
52
53
    /**
54
     * ScriptPanel constructor.
55
     *
56
     * @param Connection         $connection
57
     * @param MapStorage         $mapStorage
58
     * @param PlayerQueryBuilder $playerQueryBuilder
59
     * @param RecordQueryBuilder $recordQueryBuilder
60
     * @param MapQueryBuilder    $mapQuery
61
     * @param Console            $console
62
     */
63 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...
64
        Connection $connection,
65
        MapStorage $mapStorage,
66
        PlayerQueryBuilder $playerQueryBuilder,
67
        RecordQueryBuilder $recordQueryBuilder,
68
        MapQueryBuilder $mapQuery,
69
        Console $console
70
    ) {
71 131
        parent::__construct();
72 131
        $this->mapStorage = $mapStorage;
73 131
        $this->playerQueryBuilder = $playerQueryBuilder;
74 131
        $this->recordQueryBuilder = $recordQueryBuilder;
75 131
        $this->mapQuery = $mapQuery;
76 131
        $this->console = $console;
77 131
        $this->connection = $connection;
78 131
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 131
    protected function configure()
84
    {
85
        $this
86
            // the name of the command (the part after "bin/console")
87 131
            ->setName('eXpansion:testing:deleteTestData')
88
            // the short description shown while running "php bin/console list"
89 131
            ->setDescription('creates defined number of users with records to all maps.')
90
            // the full command description shown when running the command with
91
            // the "--help" option
92 131
            ->setHelp('This command generates defined number of users and records to each map...');
93 131
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function execute(InputInterface $input, OutputInterface $output)
99
    {
100
        $this->console->init($output, null);
101
102
103
        $i = 1;
0 ignored issues
show
Unused Code introduced by
$i 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...
104
105
        $con = Propel::getWriteConnection(PlayerTableMap::DATABASE_NAME);
106
        $con->beginTransaction();
107
108
        $players = $this->playerQueryBuilder->findDummy();
109
110
        $progress = new ProgressBar($output, count($players));
111
        $progress->start();
112
        /** @var Player $player */
113
        foreach ($players as $player) {
114
            $player->delete();
115
            $progress->advance();
116
        }
117
        $con->commit();
118
        $progress->finish();
119
120
121
        $this->console->writeln("");
122
        $this->console->writeln("Removing dummy records... please wait...");
123
        $maps = $this->mapQuery->getAllMaps();
124
        $count = count($maps);
0 ignored issues
show
Unused Code introduced by
$count 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...
125
126
        $i = 1;
127
        foreach ($maps as $m => $map) {
128
            $records = $this->recordQueryBuilder->getMapRecords($map->getMapuid(), 1, "asc", 2000);
129
130
            if (count($records) > 0) {
131
                $this->console->writeln("Deleting dummy records from map ".$i."/".count($maps)." -> ".$map->getName());
132
                $con = Propel::getWriteConnection(RecordTableMap::DATABASE_NAME);
133
                $con->beginTransaction();
134
                $progress = new ProgressBar($output, count($records));
135
                $progress->start();
136
                foreach ($records as $record) {
137
                    if (strstr($record->getPlayer()->getLogin(), "dummyplayer_")) {
138
                        $record->delete();
139
                        $progress->advance();
140
                    }
141
                }
142
                $con->commit();
143
                $progress->finish();
144
                $this->console->writeln("");
145
            } else {
146
                $this->console->writeln("skipping map ".$i."/".count($maps)." -> ".$map->getName());
147
            }
148
149
            PlayerTableMap::clearInstancePool();
150
            PlayerTableMap::clearRelatedInstancePool();
151
            RecordTableMap::clearInstancePool();
152
            RecordTableMap::clearRelatedInstancePool();
153
            $i++;
154
        }
155
    }
156
157
}
158