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

FileQueryWriter::buildMigrationFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 19
ccs 9
cts 9
cp 1
crap 3
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 DateTime;
8
use DateTimeInterface;
9
use function file_put_contents;
10
use function is_dir;
11
use function sprintf;
12
13
final class FileQueryWriter implements QueryWriter
14
{
15
    /** @var null|OutputWriter */
16
    private $outputWriter;
17
18
    /** @var MigrationFileBuilder */
19
    private $migrationFileBuilder;
20
21 9
    public function __construct(
22
        OutputWriter $outputWriter,
23
        MigrationFileBuilder $migrationFileBuilder
24
    ) {
25 9
        $this->outputWriter         = $outputWriter;
26 9
        $this->migrationFileBuilder = $migrationFileBuilder;
27 9
    }
28
29
    /**
30
     * @param mixed[] $queriesByVersion
31
     */
32 8
    public function write(
33
        string $path,
34
        string $direction,
35
        array $queriesByVersion,
36
        ?DateTimeInterface $now = null
37
    ) : bool {
38 8
        $now = $now ?? new DateTime();
39
40 8
        $string = $this->migrationFileBuilder
41 8
            ->buildMigrationFile($queriesByVersion, $direction, $now);
42
43 8
        $path = $this->buildMigrationFilePath($path, $now);
44
45 8
        if ($this->outputWriter !== null) {
46 8
            $this->outputWriter->write(
47 8
                "\n" . sprintf('Writing migration file to "<info>%s</info>"', $path)
48
            );
49
        }
50
51 8
        return file_put_contents($path, $string) !== false;
52
    }
53
54 8
    private function buildMigrationFilePath(string $path, DateTimeInterface $now) : string
55
    {
56 8
        if (is_dir($path)) {
57 4
            $path = realpath($path);
58 4
            $path = $path . '/doctrine_migration_' . $now->format('YmdHis') . '.sql';
59
        }
60
61 8
        return $path;
62
    }
63
}
64