Completed
Push — master ( ab7ebf...740609 )
by Grégoire
02:23
created

ConfigurationFileWithFallback::loadConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration\Configuration;
6
7
use Doctrine\Migrations\Configuration\Configuration;
8
use Doctrine\Migrations\Configuration\Configuration\Exception\MissingConfigurationFile;
9
use Doctrine\Migrations\Tools\Console\Exception\FileTypeNotSupported;
10
use function file_exists;
11
12
/**
13
 * This class creates a configuration instance from a configuration file passed as argument.
14
 * If no arguments are provided, will try to load one of migrations.{xml, yml, yaml, json, php} files.
15
 *
16
 * @internal
17
 */
18
final class ConfigurationFileWithFallback implements ConfigurationLoader
19
{
20
    /** @var string|null */
21
    private $file;
22
23 3
    public function __construct(?string $file = null)
24
    {
25 3
        $this->file = $file;
26 3
    }
27
28 3
    public function getConfiguration() : Configuration
29
    {
30 3
        if ($this->file !== null) {
31 1
            return $this->loadConfiguration($this->file);
32
        }
33
34
        /**
35
         * If no config has been provided, look for default config file in the path.
36
         */
37
        $defaultFiles = [
38 2
            'migrations.xml',
39
            'migrations.yml',
40
            'migrations.yaml',
41
            'migrations.json',
42
            'migrations.php',
43
        ];
44
45 2
        foreach ($defaultFiles as $file) {
46 2
            if ($this->configurationFileExists($file)) {
47 1
                return $this->loadConfiguration($file);
48
            }
49
        }
50
51 1
        throw MissingConfigurationFile::new();
52
    }
53
54 2
    private function configurationFileExists(string $config) : bool
55
    {
56 2
        return file_exists($config);
57
    }
58
59
    /**
60
     * @throws FileTypeNotSupported
61
     */
62 2
    private function loadConfiguration(string $file) : Configuration
63
    {
64 2
        return (new FormattedFile($file))->getConfiguration();
65
    }
66
}
67