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

ConnectionLoader::__construct()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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