MigrationDirectoryHelper::getMigrationDirectory()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.6
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Helper;
6
7
use Doctrine\Migrations\Configuration\Configuration;
8
use Doctrine\Migrations\Tools\Console\Exception\DirectoryDoesNotExist;
9
use function date;
10
use function file_exists;
11
use function mkdir;
12
use function rtrim;
13
use const DIRECTORY_SEPARATOR;
14
15
/**
16
 * The MigrationDirectoryHelper class is responsible for returning the directory that migrations are stored in.
17
 *
18
 * @internal
19
 */
20
class MigrationDirectoryHelper
21
{
22
    /**
23
     * @throws DirectoryDoesNotExist
24
     */
25 6
    public function getMigrationDirectory(Configuration $configuration, string $dir) : string
26
    {
27 6
        $dir = rtrim($dir, '/');
28
29 6
        if (! file_exists($dir)) {
30 1
            throw DirectoryDoesNotExist::new($dir);
31
        }
32
33 5
        if ($configuration->areMigrationsOrganizedByYear()) {
34 2
            $dir .= $this->appendDir(date('Y'));
35
        }
36
37 5
        if ($configuration->areMigrationsOrganizedByYearAndMonth()) {
38 1
            $dir .= $this->appendDir(date('m'));
39
        }
40
41 5
        $this->createDirIfNotExists($dir);
42
43 5
        return $dir;
44
    }
45
46 2
    private function appendDir(string $dir) : string
47
    {
48 2
        return DIRECTORY_SEPARATOR . $dir;
49
    }
50
51 5
    private function createDirIfNotExists(string $dir) : void
52
    {
53 5
        if (file_exists($dir)) {
54 3
            return;
55
        }
56
57 2
        mkdir($dir, 0755, true);
58 2
    }
59
}
60