Completed
Pull Request — master (#172)
by De Cramer
03:09
created

DelDummyRecordsCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 133
Duplicated Lines 12.03 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 25.92%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 16 1
A configure() 0 11 1
B execute() 0 59 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
        $this->console->writeln("Removing dummy records... please wait...");
103
        $maps = $this->mapQuery->getAllMaps();
104
105
        $i = 1;
106
        foreach ($maps as $m => $map) {
107
            $records = $this->recordQueryBuilder->getMapRecords($map->getMapuid(), 1, "asc", 2000);
108
109
            if (count($records) > 0) {
110
                $this->console->writeln("Deleting dummy records from map ".$i."/".count($maps)." -> ".$map->getName());
111
                $con = Propel::getWriteConnection(RecordTableMap::DATABASE_NAME);
112
                $con->beginTransaction();
113
                $progress = new ProgressBar($output, count($records));
114
                $progress->start();
115
                foreach ($records as $record) {
116
                    if (strstr($record->getPlayer()->getLogin(), "dummyplayer_")) {
117
                        $record->delete();
118
                        $progress->advance();
119
                    }
120
                }
121
                $con->commit();
122
                $progress->finish();
123
                $this->console->writeln("");
124
            } else {
125
                $this->console->writeln("skipping map ".$i."/".count($maps)." -> ".$map->getName());
126
            }
127
128
            PlayerTableMap::clearInstancePool();
129
            PlayerTableMap::clearRelatedInstancePool();
130
            RecordTableMap::clearInstancePool();
131
            RecordTableMap::clearRelatedInstancePool();
132
            $i++;
133
        }
134
135
        $this->console->writeln("");
136
        $this->console->writeln("Removing dummy players... please wait...");
137
        $con = Propel::getWriteConnection(PlayerTableMap::DATABASE_NAME);
138
        $con->beginTransaction();
139
140
        $players = $this->playerQueryBuilder->findDummy();
141
142
        $progress = new ProgressBar($output, count($players));
143
        $progress->start();
144
        /** @var Player $player */
145
        foreach ($players as $player) {
146
            $player->delete();
147
            $progress->advance();
148
        }
149
        $con->commit();
150
        $progress->finish();
151
152
        PlayerTableMap::clearInstancePool();
153
        PlayerTableMap::clearRelatedInstancePool();
154
        $this->console->writeln("");
155
        $this->console->writeln("all Done.");
156
    }
157
158
}
159