Failed Conditions
Pull Request — master (#632)
by Michael
02:44
created

FileQueryWriter::buildMigrationFilePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
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 function date;
8
use function file_put_contents;
9
use function is_dir;
10
use function sprintf;
11
12
final class FileQueryWriter implements QueryWriter
13
{
14
    /** @var string */
15
    private $columnName;
16
17
    /** @var string */
18
    private $tableName;
19
20
    /** @var null|OutputWriter */
21
    private $outputWriter;
22
23 11
    public function __construct(
24
        string $columnName,
25
        string $tableName,
26
        ?OutputWriter $outputWriter
27
    ) {
28 11
        $this->columnName   = $columnName;
29 11
        $this->tableName    = $tableName;
30 11
        $this->outputWriter = $outputWriter;
31 11
    }
32
33
    /**
34
     * @param mixed[] $queriesByVersion
35
     */
36 10
    public function write(
37
        string $path,
38
        string $direction,
39
        array $queriesByVersion
40
    ) : bool {
41 10
        $path   = $this->buildMigrationFilePath($path);
42 10
        $string = $this->buildMigrationFile($queriesByVersion, $direction);
43
44 10
        if ($this->outputWriter !== null) {
45 6
            $this->outputWriter->write(
46 6
                "\n" . sprintf('Writing migration file to "<info>%s</info>"', $path)
47
            );
48
        }
49
50 10
        return file_put_contents($path, $string) !== false;
51
    }
52
53
    /** @param string[][] $queriesByVersion */
54 10
    private function buildMigrationFile(
55
        array $queriesByVersion,
56
        string $direction
57
    ) : string {
58 10
        $string = sprintf("-- Doctrine Migration File Generated on %s\n", date('Y-m-d H:i:s'));
59
60 10
        foreach ($queriesByVersion as $version => $queries) {
61 10
            $version = (string) $version;
62
63 10
            $string .= "\n-- Version " . $version . "\n";
64
65 10
            foreach ($queries as $query) {
66 10
                $string .= $query . ";\n";
67
            }
68
69 10
            $string .= $this->getVersionUpdateQuery($version, $direction);
70
        }
71
72 10
        return $string;
73
    }
74
75 10
    private function getVersionUpdateQuery(string $version, string $direction) : string
76
    {
77 10
        if ($direction === Version::DIRECTION_DOWN) {
78 4
            $query = "DELETE FROM %s WHERE %s = '%s';\n";
79
        } else {
80 6
            $query = "INSERT INTO %s (%s) VALUES ('%s');\n";
81
        }
82
83 10
        return sprintf(
84 10
            $query,
85 10
            $this->tableName,
86 10
            $this->columnName,
87 10
            $version
88
        );
89
    }
90
91 10
    private function buildMigrationFilePath(string $path) : string
92
    {
93 10
        if (is_dir($path)) {
94 6
            $path = realpath($path);
95 6
            $path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
96
        }
97
98 10
        return $path;
99
    }
100
}
101