Completed
Push — master ( 9b2b45...60ae91 )
by Mike
03:58
created

AbstractFileConfiguration::setConfiguration()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 12
cts 12
cp 1
rs 8.8571
cc 5
eloc 8
nc 7
nop 1
crap 5
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\Configuration;
21
22
use Doctrine\DBAL\Migrations\MigrationException;
23
24
/**
25
 * Abstract Migration Configuration class for loading configuration information
26
 * from a configuration file (xml or yml).
27
 *
28
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
29
 * @link        www.doctrine-project.org
30
 * @since       2.0
31
 * @author      Jonathan H. Wage <[email protected]>
32
 */
33
abstract class AbstractFileConfiguration extends Configuration
34
{
35
    /**
36
     * The configuration file used to load configuration information
37
     *
38
     * @var string
39
     */
40
    private $file;
41
42
    /**
43
     * Whether or not the configuration file has been loaded yet or not
44
     *
45
     * @var boolean
46
     */
47
    private $loaded = false;
48
49
    /**
50
     * @var array of possible configuration properties in migrations configuration.
51
     */
52
    private $configurationProperties = [
53
        'migrations_namespace' => 'setMigrationsNamespace',
54
        'table_name' => 'setMigrationsTableName',
55
        'organize_migrations' => 'setMigrationOrganisation',
56
        'name' => 'setName','name' => 'setName',
57
        'migrations_directory' => 'loadMigrationsFromDirectory',
58
        'migrations' => 'loadMigrations',
59
    ];
60
61 79
    protected function setConfiguration(Array $config)
62
    {
63 79
        foreach($config as $configurationKey => $configurationValue) {
64 79
            if (!isset($this->configurationProperties[$configurationKey])) {
65 3
                $msg = sprintf('Migrations configuration key "%s" does not exists.', $configurationKey);
66 3
                throw MigrationException::configurationNotValid($msg);
67
            }
68 76
        }
69 76
        foreach($this->configurationProperties as $configurationKey => $configurationSetter) {
70 76
            if (isset($config[$configurationKey])) {
71 76
                $this->{$configurationSetter}($config[$configurationKey]);
72 69
            }
73 76
        }
74 69
    }
75
76 50
    private function loadMigrationsFromDirectory($migrationsDirectory)
77
    {
78 50
        $this->setMigrationsDirectory($migrationsDirectory);
79 50
        $this->registerMigrationsFromDirectory($migrationsDirectory);
80 50
    }
81
82 29
    private function loadMigrations($migrations)
83
    {
84 29
        if (is_array($migrations)) {
85 28
            foreach ($migrations as $migration) {
86 10
                $this->registerMigration($migration['version'], $migration['class']);
87 28
            }
88 28
        }
89 29
    }
90
91 20
    private function setMigrationOrganisation($migrationOrganisation)
92
    {
93 20
        if (strcasecmp($migrationOrganisation, static::VERSIONS_ORGANIZATION_BY_YEAR) == 0) {
94 8
            $this->setMigrationsAreOrganizedByYear();
95 16
        } else if (strcasecmp($migrationOrganisation, static::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) == 0) {
96 9
            $this->setMigrationsAreOrganizedByYearAndMonth();
97 9
        } else {
98 3
            $msg = 'Unknown ' . var_export($migrationOrganisation, true) . ' for configuration "organize_migrations".';
99 3
            throw MigrationException::configurationNotValid($msg);
100
        }
101 13
    }
102
103
    /**
104
     * Load the information from the passed configuration file
105
     *
106
     * @param string $file The path to the configuration file
107
     *
108
     * @throws MigrationException Throws exception if configuration file was already loaded
109
     */
110 87
    public function load($file)
111
    {
112 87
        if ($this->loaded) {
113 4
            throw MigrationException::configurationFileAlreadyLoaded();
114
        }
115 87
        if (file_exists($path = getcwd() . '/' . $file)) {
116 4
            $file = $path;
117 4
        }
118 87
        $this->file = $file;
119
120 87
        if (!file_exists($file)) {
121 6
            throw new \InvalidArgumentException('Given config file does not exist');
122
        }
123
124 81
        $this->doLoad($file);
125 69
        $this->loaded = true;
126 69
    }
127
128 50
    protected function getDirectoryRelativeToFile($file, $input)
129
    {
130 50
        $path = realpath(dirname($file) . '/' . $input);
131
132 50
        return ($path !== false) ? $path : $input;
133
    }
134
135 10
    public function getFile()
136
    {
137 10
        return $this->file;
138
    }
139
140
    /**
141
     * Abstract method that each file configuration driver must implement to
142
     * load the given configuration file whether it be xml, yaml, etc. or something
143
     * else.
144
     *
145
     * @param string $file The path to a configuration file.
146
     */
147
    abstract protected function doLoad($file);
148
}
149