Completed
Push — master ( 368556...6bf94b )
by Jonathan
11s
created

ConnectionLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getConnection() 0 11 2
A createConnectionConfigurationChainLoader() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\Migrations\Configuration\Configuration;
9
use Doctrine\Migrations\Configuration\Connection\ConnectionLoaderInterface;
10
use Doctrine\Migrations\Configuration\Connection\Loader\ArrayConnectionConfigurationLoader;
11
use Doctrine\Migrations\Configuration\Connection\Loader\ConnectionConfigurationChainLoader;
12
use Doctrine\Migrations\Configuration\Connection\Loader\ConnectionConfigurationLoader;
13
use Doctrine\Migrations\Configuration\Connection\Loader\ConnectionHelperLoader;
14
use InvalidArgumentException;
15
use Symfony\Component\Console\Helper\HelperSet;
16
use Symfony\Component\Console\Input\InputInterface;
17
18
class ConnectionLoader
19
{
20
    /** @var Configuration|null */
21
    private $configuration;
22
23 13
    public function __construct(?Configuration $configuration)
24
    {
25 13
        $this->configuration = $configuration;
26 13
    }
27
28 13
    public function getConnection(InputInterface $input, HelperSet $helperSet) : Connection
29
    {
30 13
        $connection = $this->createConnectionConfigurationChainLoader($input, $helperSet)
31 13
            ->chosen();
32
33 13
        if ($connection !== null) {
34 12
            return $connection;
35
        }
36
37 1
        throw new InvalidArgumentException(
38 1
            'You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.'
39
        );
40
    }
41
42 12
    protected function createConnectionConfigurationChainLoader(
43
        InputInterface $input,
44
        HelperSet $helperSet
45
    ) : ConnectionLoaderInterface {
46 12
        return new ConnectionConfigurationChainLoader([
47 12
            new ArrayConnectionConfigurationLoader($input->getOption('db-configuration')),
48 12
            new ArrayConnectionConfigurationLoader('migrations-db.php'),
49 12
            new ConnectionHelperLoader($helperSet, 'connection'),
50 12
            new ConnectionConfigurationLoader($this->configuration),
51
        ]);
52
    }
53
}
54