ParametrizedSqlMigrationQuery   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 66
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A addSql() 0 4 1
A getDescription() 0 7 1
A execute() 0 4 1
A processQueries() 0 9 3
1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Migration;
4
5
use Psr\Log\LoggerInterface;
6
7
class ParametrizedSqlMigrationQuery extends ParametrizedMigrationQuery
8
{
9
    /**
10
     * @var array value = [sql, parameters, types]
11
     */
12
    protected $queries = [];
13
14
    /**
15
     * Adds SQL query
16
     *
17
     * @param string $query  The SQL query
18
     * @param array  $params The parameters to bind to the query, if any.
19
     * @param array  $types  The types the previous parameters are in.
20
     */
21
    public function __construct($query = null, array $params = [], array $types = [])
22
    {
23
        if (!empty($query)) {
24
            $this->queries[] = [$query, $params, $types];
25
        }
26
    }
27
28
    /**
29
     * Adds SQL query
30
     *
31
     * @param string $query  The SQL query
32
     * @param array  $params The parameters to bind to the query, if any.
33
     * @param array  $types  The types the previous parameters are in.
34
     */
35
    public function addSql($query, array $params = [], array $types = [])
36
    {
37
        $this->queries[] = [$query, $params, $types];
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getDescription()
44
    {
45
        $logger = new ArrayLogger();
46
        $this->processQueries($logger, true);
47
48
        return $logger->getMessages();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function execute(LoggerInterface $logger)
55
    {
56
        $this->processQueries($logger);
57
    }
58
59
    /**
60
     * @param LoggerInterface $logger
61
     * @param bool            $dryRun
62
     */
63
    protected function processQueries(LoggerInterface $logger, $dryRun = false)
64
    {
65
        foreach ($this->queries as $query) {
66
            $this->logQuery($logger, $query[0], $query[1], $query[2]);
67
            if (!$dryRun) {
68
                $this->connection->executeUpdate($query[0], $query[1], $query[2]);
69
            }
70
        }
71
    }
72
}
73