MigrationDirectoryHelper   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 40
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMigrationDirectory() 0 20 4
A appendDir() 0 4 1
A createDirIfNotExists() 0 8 2
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