Completed
Push — master ( 882947...a4a53d )
by Mike
13:52 queued 13:46
created

SqlFileWriter::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Doctrine\DBAL\Migrations;
4
5
use Doctrine\DBAL\Exception\InvalidArgumentException;
6
7
/**
8
 * @deprecated
9
 *
10
 * @see \Doctrine\DBAL\Migrations\FileQueryWriter
11
 */
12
class SqlFileWriter
13
{
14
    private $migrationsColumnName;
15
16
    private $migrationsTableName;
17
18
    private $destPath;
19
20
    /** @var null|OutputWriter */
21
    private $outputWriter;
22
23
    /**
24
     * @param string $migrationsColumnName
25
     * @param string $migrationsTableName
26
     * @param string $destPath
27
     * @param \Doctrine\DBAL\Migrations\OutputWriter $outputWriter
28
     */
29 18
    public function __construct(
30
        $migrationsColumnName,
31
        $migrationsTableName,
32
        $destPath,
33
        OutputWriter $outputWriter = null
34
    ) {
35 18
        if (empty($migrationsColumnName)) {
36 1
            $this->throwInvalidArgumentException('Migrations column name cannot be empty.');
37
        }
38
39 17
        if (empty($migrationsTableName)) {
40 2
            $this->throwInvalidArgumentException('Migrations table name cannot be empty.');
41
        }
42
43 15
        if (empty($destPath)) {
44
            $this->throwInvalidArgumentException('Destination file must be specified.');
45
        }
46
47 15
        $this->migrationsColumnName = $migrationsColumnName;
48 15
        $this->migrationsTableName  = $migrationsTableName;
49 15
        $this->destPath             = $destPath;
50 15
        $this->outputWriter         = $outputWriter;
51 15
    }
52
53
    /**
54
     * @param array $queriesByVersion array Keys are versions and values are arrays of SQL queries (they must be castable to string)
55
     * @param string $direction
56
     * @return int|bool
57
     */
58 14
    public function write(array $queriesByVersion, $direction)
59
    {
60 14
        $path   = $this->buildMigrationFilePath();
61 14
        $string = $this->buildMigrationFile($queriesByVersion, $direction);
62
63 14
        if ($this->outputWriter) {
64 8
            $this->outputWriter->write("\n" . sprintf('Writing migration file to "<info>%s</info>"', $path));
65
        }
66
67 14
        return file_put_contents($path, $string);
68
    }
69
70 14
    private function buildMigrationFile(array $queriesByVersion, string $direction) : string
71
    {
72 14
        $string = sprintf("-- Doctrine Migration File Generated on %s\n", date('Y-m-d H:i:s'));
73
74 14
        foreach ($queriesByVersion as $version => $queries) {
75 14
            $string .= "\n-- Version " . $version . "\n";
76
77 14
            foreach ($queries as $query) {
78 14
                $string .= $query . ";\n";
79
            }
80
81
82 14
            $string .= $this->getVersionUpdateQuery($version, $direction);
83
        }
84
85 14
        return $string;
86
    }
87
88 14
    private function getVersionUpdateQuery(string $version, string $direction) : string
89
    {
90 14
        if ($direction === Version::DIRECTION_DOWN) {
91 6
            $query = "DELETE FROM %s WHERE %s = '%s';\n";
92
        } else {
93 8
            $query = "INSERT INTO %s (%s) VALUES ('%s');\n";
94
        }
95
96 14
        return sprintf($query, $this->migrationsTableName, $this->migrationsColumnName, $version);
97
    }
98
99 14
    private function buildMigrationFilePath() : string
100
    {
101 14
        $path = $this->destPath;
102
103 14
        if (is_dir($path)) {
104 10
            $path = realpath($path);
105 10
            $path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
106
        }
107
108 14
        return $path;
109
    }
110
111 3
    protected function throwInvalidArgumentException($message)
112
    {
113 3
        throw new InvalidArgumentException($message);
114
    }
115
}
116