Failed Conditions
Push — master ( d8d7f6...ee5f34 )
by Jonathan
11s
created

ConfigurationHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 105
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfig() 0 22 2
A getName() 0 3 1
B getMigrationConfig() 0 50 5
A configExists() 0 3 1
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Helper;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\Migrations\Configuration\ArrayConfiguration;
9
use Doctrine\Migrations\Configuration\Configuration;
10
use Doctrine\Migrations\Configuration\JsonConfiguration;
11
use Doctrine\Migrations\Configuration\XmlConfiguration;
12
use Doctrine\Migrations\Configuration\YamlConfiguration;
13
use Doctrine\Migrations\OutputWriter;
14
use InvalidArgumentException;
15
use Symfony\Component\Console\Helper\Helper;
16
use Symfony\Component\Console\Input\InputInterface;
17
use function file_exists;
18
use function pathinfo;
19
use function sprintf;
20
21
class ConfigurationHelper extends Helper implements ConfigurationHelperInterface
22
{
23
    /** @var Connection */
24
    private $connection;
25
26
    /** @var null|Configuration */
27
    private $configuration;
28
29 50
    public function __construct(
30
        Connection $connection,
31
        ?Configuration $configuration = null
32
    ) {
33 50
        $this->connection    = $connection;
34 50
        $this->configuration = $configuration;
35 50
    }
36
37 49
    public function getMigrationConfig(InputInterface $input, OutputWriter $outputWriter) : Configuration
38
    {
39
        /**
40
         * If a configuration option is passed to the command line, use that configuration
41
         * instead of any other one.
42
         */
43 49
        if ($input->getOption('configuration')) {
44 14
            $outputWriter->write(
45 14
                sprintf(
46 14
                    'Loading configuration from command option: %s',
47 14
                    $input->getOption('configuration')
48
                )
49
            );
50
51 14
            return $this->loadConfig($input->getOption('configuration'), $outputWriter);
52
        }
53
54
        /**
55
         * If a configuration has already been set using DI or a Setter use it.
56
         */
57 35
        if ($this->configuration) {
58 27
            $outputWriter->write(
59 27
                'Loading configuration from the integration code of your framework (setter).'
60
            );
61
62 27
            $this->configuration->setOutputWriter($outputWriter);
63
64 27
            return $this->configuration;
65
        }
66
67
        /**
68
         * If no any other config has been found, look for default config file in the path.
69
         */
70
        $defaultConfig = [
71 8
            'migrations.xml',
72
            'migrations.yml',
73
            'migrations.yaml',
74
            'migrations.json',
75
            'migrations.php',
76
        ];
77
78 8
        foreach ($defaultConfig as $config) {
79 8
            if ($this->configExists($config)) {
80 5
                $outputWriter->write(sprintf('Loading configuration from file: %s', $config));
81
82 8
                return $this->loadConfig($config, $outputWriter);
83
            }
84
        }
85
86 3
        return new Configuration($this->connection, $outputWriter);
87
    }
88
89
90 8
    private function configExists(string $config) : bool
91
    {
92 8
        return file_exists($config);
93
    }
94
95
    /** @throws InvalidArgumentException */
96 19
    private function loadConfig(string $config, OutputWriter $outputWriter) : Configuration
97
    {
98
        $map = [
99 19
            'xml'   => XmlConfiguration::class,
100
            'yaml'  => YamlConfiguration::class,
101
            'yml'   => YamlConfiguration::class,
102
            'php'   => ArrayConfiguration::class,
103
            'json'  => JsonConfiguration::class,
104
        ];
105
106 19
        $info = pathinfo($config);
107
108
        // check we can support this file type
109 19
        if (empty($map[$info['extension']])) {
110 1
            throw new InvalidArgumentException('Given config file type is not supported');
111
        }
112
113 18
        $class         = $map[$info['extension']];
114 18
        $configuration = new $class($this->connection, $outputWriter);
115 18
        $configuration->load($config);
116
117 18
        return $configuration;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 1
    public function getName() : string
124
    {
125 1
        return 'configuration';
126
    }
127
}
128