Passed
Push — master ( 229462...d74785 )
by Andreas
02:20 queued 14s
created

MigrationsConfigurationHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 67
ccs 19
cts 21
cp 0.9048
rs 10
c 1
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfig() 0 8 2
A getConfiguration() 0 30 5
A getName() 0 3 1
A __construct() 0 3 2
A configExists() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Helper;
6
7
use Doctrine\Migrations\Configuration\Configuration;
8
use Doctrine\Migrations\Configuration\ConfigurationLoader;
9
use Doctrine\Migrations\Configuration\Exception\UnknownLoader;
10
use Doctrine\Migrations\Tools\Console\Exception\FileTypeNotSupported;
11
use Symfony\Component\Console\Helper\Helper;
12
use Symfony\Component\Console\Input\InputInterface;
13
use const PATHINFO_EXTENSION;
14
use function file_exists;
15
use function is_string;
16
use function pathinfo;
17
18
/**
19
 * The MigrationsConfigurationHelper class is responsible for getting the Configuration instance from one of the supported methods
20
 * for defining the configuration for your migrations.
21
 */
22
final class MigrationsConfigurationHelper extends Helper implements ConfigurationHelper
23
{
24
    /** @var ConfigurationLoader */
25
    private $loader;
26
27 4
    public function __construct(?ConfigurationLoader $loader = null)
28
    {
29 4
        $this->loader = $loader ?: new ConfigurationLoader();
30 4
    }
31
32 4
    public function getConfiguration(InputInterface $input) : Configuration
33
    {
34
        /**
35
         * If a configuration option is passed to the command line, use that configuration
36
         * instead of any other one.
37
         */
38 4
        $configurationFile = $input->getOption('configuration');
39
40 4
        if ($configurationFile !== null && is_string($configurationFile)) {
41 2
            return $this->loadConfig($configurationFile);
42
        }
43
44
        /**
45
         * If no any other config has been found, look for default config file in the path.
46
         */
47
        $defaultConfig = [
48 2
            'migrations.xml',
49
            'migrations.yml',
50
            'migrations.yaml',
51
            'migrations.json',
52
            'migrations.php',
53
        ];
54
55 2
        foreach ($defaultConfig as $config) {
56 2
            if ($this->configExists($config)) {
57 1
                return $this->loadConfig($config);
58
            }
59
        }
60
61 1
        return $this->loader->getLoader('array')->load([]);
62
    }
63
64 2
    private function configExists(string $config) : bool
65
    {
66 2
        return file_exists($config);
67
    }
68
69
    /**
70
     * @throws FileTypeNotSupported
71
     */
72 3
    private function loadConfig(string $configFile) : Configuration
73
    {
74 3
        $extension = pathinfo($configFile, PATHINFO_EXTENSION);
75
76
        try {
77 3
            return $this->loader->getLoader($extension)->load($configFile);
78 1
        } catch (UnknownLoader $e) {
79 1
            throw FileTypeNotSupported::new();
80
        }
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getName() : string
87
    {
88
        return 'configuration';
89
    }
90
}
91