Failed Conditions
Push — master ( 0469de...12f4fc )
by Jonathan
02:19
created

MigrationFileBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations;
6
7
use DateTimeInterface;
8
use function sprintf;
9
10
/**
11
 * @var internal
12
 */
13
final class MigrationFileBuilder
14
{
15
    /** @var string */
16
    private $tableName;
17
18
    /** @var string */
19
    private $columnName;
20
21 10
    public function __construct(
22
        string $tableName,
23
        string $columnName
24
    ) {
25 10
        $this->columnName = $columnName;
26 10
        $this->tableName  = $tableName;
27 10
    }
28
29
    /** @param string[][] $queriesByVersion */
30 9
    public function buildMigrationFile(
31
        array $queriesByVersion,
32
        string $direction,
33
        DateTimeInterface $now
34
    ) : string {
35 9
        $string = sprintf("-- Doctrine Migration File Generated on %s\n", $now->format('Y-m-d H:i:s'));
36
37 9
        foreach ($queriesByVersion as $version => $queries) {
38 9
            $version = (string) $version;
39
40 9
            $string .= "\n-- Version " . $version . "\n";
41
42 9
            foreach ($queries as $query) {
43 9
                $string .= $query . ";\n";
44
            }
45
46 9
            $string .= $this->getVersionUpdateQuery($version, $direction);
47
        }
48
49 9
        return $string;
50
    }
51
52 9
    private function getVersionUpdateQuery(string $version, string $direction) : string
53
    {
54 9
        if ($direction === Version::DIRECTION_DOWN) {
55 3
            $query = "DELETE FROM %s WHERE %s = '%s';\n";
56
        } else {
57 6
            $query = "INSERT INTO %s (%s) VALUES ('%s');\n";
58
        }
59
60 9
        return sprintf(
61 9
            $query,
62 9
            $this->tableName,
63 9
            $this->columnName,
64 9
            $version
65
        );
66
    }
67
}
68