Completed
Push — master ( ac3b8b...3c857d )
by Gaetano
18:21
created

SQLExecutor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 20.55 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
dl 15
loc 73
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4
rs 10
ccs 11
cts 12
cp 0.9167

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 22 2
B setReferences() 15 22 5

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
4
namespace Kaliop\eZMigrationBundle\Core\Executor;
5
6
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
7
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
8
use Kaliop\eZMigrationBundle\API\ReferenceBagInterface;
9
10
class SQLExecutor extends AbstractExecutor
11
{
12
    /**
13
     * @var DatabaseHandler $connection
14
     */
15
    protected $dbHandler;
16
17
    protected $supportedStepTypes = array('sql');
18
19
    /** @var ReferenceBagInterface $referenceResolver */
20
    protected $referenceResolver;
21 20
22
    /**
23 20
     * @param DatabaseHandler $dbHandler
24 20
     * @param ReferenceBagInterface $referenceResolver
25
     */
26
    public function __construct(DatabaseHandler $dbHandler, ReferenceBagInterface $referenceResolver)
27
    {
28
        $this->dbHandler = $dbHandler;
29
        $this->referenceResolver = $referenceResolver;
30
    }
31 2
32
    /**
33 2
     * @param MigrationStep $step
34
     * @return integer
35 2
     * @throws \Exception if migration step is not for this type of db
36
     */
37 2
    public function execute(MigrationStep $step)
38
    {
39 2
        parent::execute($step);
40
41 2
        $conn = $this->dbHandler->getConnection();
42
        // @see http://doctrine-orm.readthedocs.io/projects/doctrine-dbal/en/latest/reference/platforms.html
43
        $dbType = strtolower(preg_replace('/([0-9]+|Platform)/', '', $conn->getDatabasePlatform()->getName()));
44 2
45
        $dsl = $step->dsl;
46 2
47
        if (!isset($dsl[$dbType])) {
48
            throw new \Exception("Current database type '$dbType' is not supported by the SQL migration");
49
        }
50
        $sql = $dsl[$dbType];
51
52
        // returns the number of affected rows
53
        $result = $conn->exec($sql);
54
55
        $this->setReferences($result, $dsl);
56
57
        return $result;
58
    }
59
60
    protected function setReferences($result, $dsl)
61
    {
62
        if (!array_key_exists('references', $dsl)) {
63
            return false;
64
        }
65
66 View Code Duplication
        foreach ($dsl['references'] as $reference) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
            switch ($reference['attribute']) {
68
                case 'affected_rows':
69
                    $value = $result;
70
                    break;
71
                default:
72
                    throw new \InvalidArgumentException('Sql Executor does not support setting references for attribute ' . $reference['attribute']);
73
            }
74
75
            $overwrite = false;
76
            if (isset($reference['overwrite'])) {
77
                $overwrite = $reference['overwrite'];
78
            }
79
            $this->referenceResolver->addReference($reference['identifier'], $value, $overwrite);
80
        }
81
    }
82
}
83