Completed
Push — master ( 61e01b...82ad84 )
by Maxim
03:45
created

MigrationRegistryMysql   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 19.57 %

Importance

Changes 0
Metric Value
dl 18
loc 92
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 7 7 2
A getInitialSql() 0 3 1
A isRegistered() 0 3 1
A __construct() 0 4 1
A initRegistry() 0 3 1
A unregister() 7 7 2
A getRegistered() 0 14 4

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 WebComplete\core\utils\migration;
4
5
use Doctrine\DBAL\Connection;
6
use WebComplete\core\utils\container\ContainerInterface;
7
8
class MigrationRegistryMysql extends AbstractMigrationRegistry
9
{
10
    /**
11
     * @var Connection
12
     */
13
    protected $db;
14
    protected $migrationTable = '_migrations';
15
16
    /**
17
     * @param ContainerInterface $container
18
     * @param Connection $db
19
     */
20
    public function __construct(ContainerInterface $container, Connection $db)
21
    {
22
        parent::__construct($container);
23
        $this->db = $db;
24
    }
25
26
    /**
27
     * check or create initial registry
28
     *
29
     * @throws \Doctrine\DBAL\DBALException
30
     */
31
    public function initRegistry()
32
    {
33
        $this->db->executeQuery($this->getInitialSql());
34
    }
35
36
    /**
37
     * @return string[] migration classes
38
     * @throws \Exception
39
     */
40
    public function getRegistered(): array
41
    {
42
        $this->initRegistry();
43
        $result = [];
44
        $stmt = $this->db->createQueryBuilder()
45
            ->select('*')->from($this->migrationTable)->orderBy('id', 'asc')->execute();
46
        if ($rows = $stmt->fetchAll(\PDO::FETCH_ASSOC)) {
47
            foreach ($rows as $row) {
48
                if (isset($row['class'])) {
49
                    $result[] = $row['class'];
50
                }
51
            }
52
        }
53
        return $result;
54
    }
55
56
    /**
57
     * @param string $class migration class
58
     *
59
     * @return bool
60
     */
61
    public function isRegistered(string $class): bool
62
    {
63
        return \in_array($class, $this->getRegistered(), true);
64
    }
65
66
    /**
67
     * @param string $class migration class
68
     * @throws \Exception
69
     */
70 View Code Duplication
    public function register(string $class)
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...
71
    {
72
        $this->initRegistry();
73
        if (!$this->isRegistered($class)) {
74
            $migration = $this->getMigration($class);
75
            $migration->up();
76
            $this->db->insert($this->migrationTable, ['class' => $class]);
77
        }
78
    }
79
80
    /**
81
     * @param string $class migration class
82
     * @throws \Exception
83
     */
84 View Code Duplication
    public function unregister(string $class)
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...
85
    {
86
        $this->initRegistry();
87
        if ($this->isRegistered($class)) {
88
            $migration = $this->getMigration($class);
89
            $migration->down();
90
            $this->db->delete($this->migrationTable, ['class' => $class]);
91
        }
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    protected function getInitialSql(): string
98
    {
99
        return "CREATE TABLE IF NOT EXISTS `{$this->migrationTable}` (
100
          `id` INT(11) NOT NULL AUTO_INCREMENT,
101
          `class` VARCHAR(300) NOT NULL,
102
          `created_on` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
103
          PRIMARY KEY(`id`),
104
          UNIQUE(`class`)
105
        )";
106
    }
107
}
108