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

ArrayConnectionConfigurationLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A chosen() 0 17 4
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