Completed
Pull Request — master (#300)
by De Cramer
04:22
created

DelDummyRecordsCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 126
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 23.21%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 5
dl 14
loc 126
ccs 13
cts 56
cp 0.2321
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 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 MapStorage
30
     */
31
    private $mapStorage;
32
    /**
33
     * @var PlayerQueryBuilder
34
     */
35
    private $playerQueryBuilder;
36
    /**
37
     * @var RecordQueryBuilder
38
     */
39
    private $recordQueryBuilder;
40
    /**
41
     * @var Console
42
     */
43
    private $console;
44
    /**
45
     * @var MapQueryBuilder
46
     */
47
    private $mapQuery;
48
49
    /**
50
     * ScriptPanel constructor.
51
     *
52
     * @param MapStorage         $mapStorage
53
     * @param PlayerQueryBuilder $playerQueryBuilder
54
     * @param RecordQueryBuilder $recordQueryBuilder
55
     * @param MapQueryBuilder    $mapQuery
56
     * @param Console            $console
57
     */
58 164 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...
59
        MapStorage $mapStorage,
60
        PlayerQueryBuilder $playerQueryBuilder,
61
        RecordQueryBuilder $recordQueryBuilder,
62
        MapQueryBuilder $mapQuery,
63
        Console $console
64
    ) {
65 164
        parent::__construct();
66 164
        $this->mapStorage = $mapStorage;
67 164
        $this->playerQueryBuilder = $playerQueryBuilder;
68 164
        $this->recordQueryBuilder = $recordQueryBuilder;
69 164
        $this->mapQuery = $mapQuery;
70 164
        $this->console = $console;
71 164
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 164
    protected function configure()
77
    {
78
        $this
79
            // the name of the command (the part after "bin/console")
80 164
            ->setName('eXpansion:testing:deleteTestData')
81
            // the short description shown while running "php bin/console list"
82 164
            ->setDescription('creates defined number of users with records to all maps.')
83
            // the full command description shown when running the command with
84
            // the "--help" option
85 164
            ->setHelp('This command generates defined number of users and records to each map...');
86 164
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function execute(InputInterface $input, OutputInterface $output)
92
    {
93
        $this->console->init($output, null);
94
95
        $this->console->writeln("Removing dummy records... please wait...");
96
        $maps = $this->mapQuery->getAllMaps();
97
98
        $i = 1;
99
        foreach ($maps as $m => $map) {
100
            $records = $this->recordQueryBuilder->getMapRecords($map->getMapuid(), 1, "asc", 2000);
101
102
            if (count($records) > 0) {
103
                $this->console->writeln("Deleting dummy records from map ".$i."/".count($maps)." -> ".$map->getName());
104
                $con = Propel::getWriteConnection(RecordTableMap::DATABASE_NAME);
105
                $con->beginTransaction();
106
                $progress = new ProgressBar($output, count($records));
107
                $progress->start();
108
                foreach ($records as $record) {
109
                    if (strstr($record->getPlayer()->getLogin(), "dummyplayer_")) {
110
                        $record->delete();
111
                        $progress->advance();
112
                    }
113
                }
114
                $con->commit();
115
                $progress->finish();
116
                $this->console->writeln("");
117
            } else {
118
                $this->console->writeln("skipping map ".$i."/".count($maps)." -> ".$map->getName());
119
            }
120
121
            PlayerTableMap::clearInstancePool();
122
            PlayerTableMap::clearRelatedInstancePool();
123
            RecordTableMap::clearInstancePool();
124
            RecordTableMap::clearRelatedInstancePool();
125
            $i++;
126
        }
127
128
        $this->console->writeln("");
129
        $this->console->writeln("Removing dummy players... please wait...");
130
        $con = Propel::getWriteConnection(PlayerTableMap::DATABASE_NAME);
131
        $con->beginTransaction();
132
133
        $players = $this->playerQueryBuilder->findDummy();
134
135
        $progress = new ProgressBar($output, count($players));
136
        $progress->start();
137
        /** @var Player $player */
138
        foreach ($players as $player) {
139
            $player->delete();
140
            $progress->advance();
141
        }
142
        $con->commit();
143
        $progress->finish();
144
145
        PlayerTableMap::clearInstancePool();
146
        PlayerTableMap::clearRelatedInstancePool();
147
        $this->console->writeln("");
148
        $this->console->writeln("all Done.");
149
    }
150
151
}
152