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

MigrationDirectoryHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 59
ccs 21
cts 23
cp 0.913
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A appendDir() 0 4 1
A createDirIfNotExists() 0 6 2
A __construct() 0 4 1
B getMigrationDirectory() 0 21 5
A getName() 0 4 1
1
<?php
2
3
namespace Doctrine\DBAL\Migrations\Tools\Console\Helper;
4
5
use Doctrine\DBAL\Migrations\Configuration\Configuration;
6
use Symfony\Component\Console\Helper\Helper;
7
8
/**
9
 * Class ConfigurationHelper
10
 * @package Doctrine\DBAL\Migrations\Tools\Console\Helper
11
 * @internal
12
 */
13
class MigrationDirectoryHelper extends Helper
14
{
15
16
    /**
17
     * @var Configuration
18
     */
19
    private $configuration;
20
21 15
    public function __construct(Configuration $configuration = null)
22
    {
23 15
        $this->configuration = $configuration;
24 15
    }
25
26 14
    public function getMigrationDirectory()
27
    {
28 14
        $dir = $this->configuration->getMigrationsDirectory();
29 14
        $dir = $dir ? $dir : getcwd();
30 14
        $dir = rtrim($dir, '/');
31
32 14
        if ( ! file_exists($dir)) {
33 1
            throw new \InvalidArgumentException(sprintf('Migrations directory "%s" does not exist.', $dir));
34
        }
35
36 13
        if ($this->configuration->areMigrationsOrganizedByYear()) {
37 3
            $dir .= $this->appendDir(date('Y'));
38
        }
39
40 13
        if ($this->configuration->areMigrationsOrganizedByYearAndMonth()) {
41 2
            $dir .= $this->appendDir(date('m'));
42
        }
43 13
        $this->createDirIfNotExists($dir);
44
45 13
        return $dir;
46
    }
47
48 3
    private function appendDir($dir)
49
    {
50 3
        return DIRECTORY_SEPARATOR . $dir;
51
    }
52
53 13
    private function createDirIfNotExists($dir)
54
    {
55 13
        if ( ! file_exists($dir)) {
56 3
            mkdir($dir, 0755, true);
57
        }
58 13
    }
59
60
    /**
61
     * Returns the canonical name of this helper.
62
     *
63
     * @return string The canonical name
64
     *
65
     * @api
66
     */
67
    public function getName()
68
    {
69
        return 'MigrationDirectory';
70
    }
71
}
72