MigrationQueryExecutor::execute()   C
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 6.7272
cc 7
eloc 15
nc 10
nop 2
1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Migration;
4
5
use Doctrine\DBAL\Connection;
6
use Psr\Log\LoggerInterface;
7
8
class MigrationQueryExecutor
9
{
10
    /**
11
     * @var Connection
12
     */
13
    protected $connection;
14
15
    /**
16
     * @var LoggerInterface
17
     */
18
    protected $logger;
19
20
    /**
21
     * @param Connection $connection
22
     */
23
    public function __construct(Connection $connection)
24
    {
25
        $this->connection = $connection;
26
    }
27
28
    /**
29
     * Sets a logger
30
     *
31
     * @param LoggerInterface $logger
32
     */
33
    public function setLogger(LoggerInterface $logger)
34
    {
35
        $this->logger = $logger;
36
    }
37
38
    /**
39
     * Gets a connection object this migration query executor works with
40
     *
41
     * @return Connection
42
     */
43
    public function getConnection()
44
    {
45
        return $this->connection;
46
    }
47
48
    /**
49
     * Executes the given query
50
     *
51
     * @param string|MigrationQuery $query
52
     * @param bool                  $dryRun
53
     */
54
    public function execute($query, $dryRun)
55
    {
56
        if ($query instanceof MigrationQuery) {
57
            if ($query instanceof ConnectionAwareInterface) {
58
                $query->setConnection($this->connection);
59
            }
60
            if ($dryRun) {
61
                $descriptions = $query->getDescription();
62
                if (!empty($descriptions)) {
63
                    foreach ((array)$descriptions as $description) {
64
                        $this->logger->notice($description);
65
                    }
66
                }
67
            } else {
68
                $query->execute($this->logger);
69
            }
70
        } else {
71
            $this->logger->notice($query);
72
            if (!$dryRun) {
73
                $this->connection->executeQuery($query);
74
            }
75
        }
76
    }
77
}
78