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

FileQueryWriter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 87
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A buildMigrationFilePath() 0 8 2
A getVersionUpdateQuery() 0 13 2
A write() 0 15 2
A buildMigrationFile() 0 19 3
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