Completed
Push — master ( 2f6667...a79c5c )
by Mike
11s
created

MigrationDirectoryHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A appendDir() 0 4 1
B getMigrationDirectory() 0 21 5
A createDirIfNotExists() 0 6 2
A getName() 0 4 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Migrations\Tools\Console\Helper;
21
22
use Doctrine\DBAL\Migrations\Configuration\Configuration;
23
use Symfony\Component\Console\Helper\Helper;
24
25
/**
26
 * Class ConfigurationHelper
27
 * @package Doctrine\DBAL\Migrations\Tools\Console\Helper
28
 * @internal
29
 */
30
class MigrationDirectoryHelper extends Helper
31
{
32
33
    /**
34
     * @var Configuration
35
     */
36
    private $configuration;
37
38 14
    public function __construct(Configuration $configuration = null)
39
    {
40 14
        $this->configuration = $configuration;
41 14
    }
42
43 13
    public function getMigrationDirectory()
44
    {
45 13
        $dir = $this->configuration->getMigrationsDirectory();
46 13
        $dir = $dir ? $dir : getcwd();
47 13
        $dir = rtrim($dir, '/');
48
49 13
        if (!file_exists($dir)) {
50 1
            throw new \InvalidArgumentException(sprintf('Migrations directory "%s" does not exist.', $dir));
51
        }
52
53 12
        if ($this->configuration->areMigrationsOrganizedByYear()) {
54 3
            $dir .= $this->appendDir(date('Y'));
55
        }
56
57 12
        if ($this->configuration->areMigrationsOrganizedByYearAndMonth()) {
58 2
            $dir .= $this->appendDir(date('m'));
59
        }
60 12
        $this->createDirIfNotExists($dir);
61
62 12
        return $dir;
63
    }
64
65 3
    private function appendDir($dir)
66
    {
67 3
        return DIRECTORY_SEPARATOR . $dir;
68
    }
69
70 12
    private function createDirIfNotExists($dir)
71
    {
72 12
        if (!file_exists($dir)) {
73 3
            mkdir($dir, 0755, true);
74
        }
75 12
    }
76
77
    /**
78
     * Returns the canonical name of this helper.
79
     *
80
     * @return string The canonical name
81
     *
82
     * @api
83
     */
84
    public function getName()
85
    {
86
        return 'MigrationDirectory';
87
    }
88
89
90
}
91