Completed
Push — master ( 55f34b...006836 )
by Catalin
08:33 queued 05:38
created

ConfigurationBuilder::setOnDiskConfiguration()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.392

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 20
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 35
ccs 16
cts 20
cp 0.8
crap 7.392
rs 8.6666
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald.
7
 *
8
 * (c) 2014 Matthew Fitzgerald
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace AntiMattr\MongoDB\Migrations\Configuration;
15
16
use AntiMattr\MongoDB\Migrations\OutputWriter;
17
use MongoDB\Client;
18
use Symfony\Component\Yaml\Yaml;
19
20
/**
21
 * @author Douglas Reith <[email protected]>
22
 */
23
class ConfigurationBuilder
24
{
25
    /**
26
     * @var \MongoDB\Client
27
     */
28
    private $connection;
29
30
    /**
31
     * @var OutputWriter
32
     */
33
    private $outputWriter;
34
35
    /**
36
     * @var array
37
     */
38
    private $configParams;
39
40
    /**
41
     * @var string
42
     */
43
    private $configFile;
44
45 3
    private function __construct()
46
    {
47 3
        $this->configParams = [
48
            'name' => null,
49
            'database' => null,
50
            'collection_name' => null,
51
            'migrations_namespace' => null,
52
            'migrations_directory' => null,
53
            'migrations_script_directory' => null,
54
            'migrations' => [],
55
        ];
56 3
    }
57
58 3
    public static function create(): ConfigurationBuilder
59
    {
60 3
        return new static();
61
    }
62
63 3
    public function setConnection(Client $connection): ConfigurationBuilder
64
    {
65 3
        $this->connection = $connection;
66
67 3
        return $this;
68
    }
69
70 3
    public function setOutputWriter(OutputWriter $outputWriter): ConfigurationBuilder
71
    {
72 3
        $this->outputWriter = $outputWriter;
73
74 3
        return $this;
75
    }
76
77 3
    public function setOnDiskConfiguration(?string $configFile = null): ConfigurationBuilder
78
    {
79 3
        $this->configFile = $configFile;
80
81 3
        if ($this->configFile) {
82 2
            if (file_exists($path = getcwd() . '/' . $this->configFile)) {
83
                $this->configFile = $path;
84
            }
85
86 2
            if (!file_exists($this->configFile)) {
87
                throw new \InvalidArgumentException('The specified config file is not a valid file.');
88
            }
89
90 2
            $info = pathinfo($this->configFile);
91
92 2
            $fileExt = strtolower($info['extension']);
93
94 2
            switch ($fileExt) {
95 2
                case 'xml':
96 1
                    $diskConfig = $this->loadXmlFile($this->configFile);
97 1
                    break;
98
99 1
                case 'yml':
100
                case 'yaml':
101 1
                    $diskConfig = Yaml::parse(file_get_contents($this->configFile));
102 1
                    break;
103
104
                default:
105
                    throw new \InvalidArgumentException(sprintf('The specified config file should end in .xml or .yml/.yaml. Unrecognized extension [%s]', $fileExt));
106
            }
107
108 2
            $this->configParams = array_merge($this->configParams, $diskConfig);
109
        }
110
111 3
        return $this;
112
    }
113
114 3
    public function build(): Configuration
115
    {
116 3
        $config = new Configuration($this->connection, $this->outputWriter);
117
118 3
        $config->setName($this->configParams['name'])
119 3
            ->setFile($this->configFile)
120 3
            ->setMigrationsDatabaseName($this->configParams['database'])
121 3
            ->setMigrationsCollectionName($this->configParams['collection_name'])
122 3
            ->setMigrationsNamespace($this->configParams['migrations_namespace'])
123
        ;
124
125 3
        if (!empty($this->configParams['migrations_directory'])) {
126 2
            $migrationsDirectory = $this->getDirectoryRelativeToFile(
127 2
                $this->configFile,
128 2
                $this->configParams['migrations_directory']
129
            );
130
131 2
            $config->setMigrationsDirectory($migrationsDirectory)
132 2
                ->registerMigrationsFromDirectory($migrationsDirectory);
133
        }
134
135 3
        if (!empty($this->configParams['migrations_script_directory'])) {
136 2
            $scriptsDirectory = $this->getDirectoryRelativeToFile(
137 2
                $this->configFile,
138 2
                $this->configParams['migrations_script_directory']
139
            );
140
141 2
            $config->setMigrationsScriptDirectory($scriptsDirectory);
142
        }
143
144 3
        foreach ($this->configParams['migrations'] as $migration) {
145
            $config->registerMigration(
146
                $migration['version'],
147
                $migration['class']
148
            );
149
        }
150
151 3
        return $config;
152
    }
153
154 1
    private function loadXmlFile(string $configFile): array
155
    {
156 1
        $xml = simplexml_load_file($configFile);
157 1
        $configArr = [];
158
159 1
        if (isset($xml->name)) {
160 1
            $configArr['name'] = (string) $xml->name;
161
        }
162
163 1
        if (isset($xml->database['name'])) {
164 1
            $configArr['database'] = (string) $xml->database['name'];
165
        }
166
167 1
        if (isset($xml->collection['name'])) {
168 1
            $configArr['collection_name'] = (string) $xml->collection['name'];
169
        }
170
171 1
        if (isset($xml->{'migrations-namespace'})) {
172 1
            $configArr['migrations_namespace'] = (string) $xml->{'migrations-namespace'};
173
        }
174
175 1
        if (isset($xml->{'migrations-directory'})) {
176 1
            $configArr['migrations_directory'] = (string) $xml->{'migrations-directory'};
177
        }
178
179 1
        if (isset($xml->{'migrations-script-directory'})) {
180 1
            $configArr['migrations_script_directory'] = (string) $xml->{'migrations-script-directory'};
181
        }
182
183 1
        if (isset($xml->migrations->migration)) {
184
            foreach ($xml->migrations->migration as $migration) {
185
                $configArr['migrations'][] = [
186
                    'version' => $migration['version'],
187
                    'class' => $migration['class'],
188
                ];
189
            }
190
        }
191
192 1
        return $configArr;
193
    }
194
195
    /**
196
     * Get the path to the directory relative to the config file.
197
     */
198 2
    protected function getDirectoryRelativeToFile(string $configFile, ?string $directory = null): ?string
199
    {
200 2
        if (!$directory) {
201
            return null;
202
        }
203
204 2
        $path = realpath(dirname($configFile) . '/' . $directory);
205
206 2
        if (false !== $path) {
207
            return $path;
208
        }
209
210 2
        return $directory;
211
    }
212
}
213