Completed
Pull Request — master (#665)
by Jonathan
02:09
created

ConnectionLoader::getConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
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