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

SQLExecutor::setReferences()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 15
Ratio 68.18 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 15
loc 22
c 0
b 0
f 0
rs 8.6737
ccs 0
cts 0
cp 0
cc 5
eloc 14
nc 5
nop 2
crap 30
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