SqlMigrationQuery   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 71
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A setConnection() 0 4 1
A addSql() 0 4 1
A getDescription() 0 10 3
A execute() 0 7 2
1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Migration;
4
5
use Doctrine\DBAL\Connection;
6
use Psr\Log\LoggerInterface;
7
8
class SqlMigrationQuery implements MigrationQuery, ConnectionAwareInterface
9
{
10
    /**
11
     * @var string[]
12
     */
13
    protected $queries;
14
15
    /**
16
     * @var Connection
17
     */
18
    protected $connection;
19
20
    /**
21
     * @param string|string[] $sql
22
     * @throws \InvalidArgumentException if $sql is empty
23
     */
24
    public function __construct($sql = null)
25
    {
26
        if (empty($sql)) {
27
            $this->queries = [];
28
        } elseif (is_array($sql)) {
29
            $this->queries = $sql;
30
        } else {
31
            $this->queries   = [];
32
            $this->queries[] = $sql;
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function setConnection(Connection $connection)
40
    {
41
        $this->connection = $connection;
42
    }
43
44
    /**
45
     * Adds SQL query
46
     *
47
     * @param string $query The SQL query
48
     */
49
    public function addSql($query)
50
    {
51
        $this->queries[] = $query;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getDescription()
58
    {
59
        if (empty($this->queries)) {
60
            return '';
61
        } elseif (count($this->queries) === 1) {
62
            return $this->queries[0];
63
        }
64
65
        return $this->queries;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function execute(LoggerInterface $logger)
72
    {
73
        foreach ($this->queries as $query) {
74
            $logger->notice($query);
75
            $this->connection->executeUpdate($query);
76
        }
77
    }
78
}
79