SqlGenerator::generate()   B
last analyzed

Complexity

Conditions 9
Paths 10

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 22
cts 22
cp 1
rs 7.6924
c 0
b 0
f 0
cc 9
nc 10
nop 4
crap 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Generator;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\Migrations\Configuration\Configuration;
9
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
10
use Doctrine\SqlFormatter\NullHighlighter;
11
use Doctrine\SqlFormatter\SqlFormatter;
12
use function array_unshift;
13
use function count;
14
use function implode;
15
use function sprintf;
16
use function stripos;
17
use function strlen;
18
use function var_export;
19
20
/**
21
 * The SqlGenerator class is responsible for generating the body of the up() and down() methods for a migration
22
 * from an array of SQL queries.
23
 *
24
 * @internal
25
 */
26
class SqlGenerator
27
{
28
    /** @var Configuration */
29
    private $configuration;
30
31
    /** @var AbstractPlatform */
32
    private $platform;
33
34 3
    public function __construct(Configuration $configuration, AbstractPlatform $platform)
35
    {
36 3
        $this->configuration = $configuration;
37 3
        $this->platform      = $platform;
38 3
    }
39
40
    /** @param string[] $sql */
41 3
    public function generate(
42
        array $sql,
43
        bool $formatted = false,
44
        int $lineLength = 120,
45
        bool $checkDbPlatform = true
46
    ) : string {
47 3
        $code = [];
48
49 3
        $storageConfiguration = $this->configuration->getMetadataStorageConfiguration();
50 3
        foreach ($sql as $query) {
51 3
            if ($storageConfiguration instanceof TableMetadataStorageConfiguration
52 3
                && stripos($query, $storageConfiguration->getTableName()) !== false) {
53 3
                continue;
54
            }
55
56 3
            if ($formatted) {
57 3
                $maxLength = $lineLength - 18 - 8; // max - php code length - indentation
58
59 3
                if (strlen($query) > $maxLength) {
60 3
                    $query = (new SqlFormatter(new NullHighlighter()))->format($query);
61
                }
62
            }
63
64 3
            $code[] = sprintf('$this->addSql(%s);', var_export($query, true));
65
        }
66
67 3
        if (count($code) !== 0 && $checkDbPlatform && $this->configuration->isDatabasePlatformChecked()) {
68 1
            $currentPlatform = $this->platform->getName();
69
70 1
            array_unshift(
71 1
                $code,
72 1
                sprintf(
73 1
                    '$this->abortIf($this->connection->getDatabasePlatform()->getName() !== %s, %s);',
74 1
                    var_export($currentPlatform, true),
75 1
                    var_export(sprintf("Migration can only be executed safely on '%s'.", $currentPlatform), true)
76
                ),
77 1
                ''
78
            );
79
        }
80
81 3
        return implode("\n", $code);
82
    }
83
}
84