Failed Conditions
Pull Request — master (#725)
by Michael
02:19
created

ArrayConnectionConfigurationLoader::chosen()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 0
dl 0
loc 17
ccs 8
cts 9
cp 0.8889
crap 4.0218
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration\Connection\Loader;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\DriverManager;
9
use Doctrine\Migrations\Configuration\Connection\ConnectionLoaderInterface;
10
use Doctrine\Migrations\Configuration\Connection\Loader\Exception\InvalidConfiguration;
11
use function file_exists;
12
use function is_array;
13
14
/**
15
 * The ArrayConnectionConfigurationLoader class is responsible for loading a Doctrine\DBAL\Connection from a PHP file
16
 * that returns an array of connection information which is used to instantiate a connection with DriverManager::getConnection()
17
 *
18
 * @internal
19
 */
20
class ArrayConnectionConfigurationLoader implements ConnectionLoaderInterface
21
{
22
    /** @var null|string */
23
    private $filename;
24
25 14
    public function __construct(?string $filename)
26
    {
27 14
        $this->filename = $filename;
28 14
    }
29
30
    /**
31
     * Read the input and return a Configuration, returns null if the config
32
     * is not supported.
33
     *
34
     * @throws InvalidConfiguration
35
     */
36 14
    public function chosen() : ?Connection
37
    {
38 14
        if ($this->filename === null) {
39 13
            return null;
40
        }
41
42 14
        if (! file_exists($this->filename)) {
43 13
            return null;
44
        }
45
46 1
        $params = include $this->filename;
47
48 1
        if (! is_array($params)) {
49
            throw InvalidConfiguration::invalidArrayConfiguration();
50
        }
51
52 1
        return DriverManager::getConnection($params);
53
    }
54
}
55