Failed Conditions
Pull Request — master (#632)
by Michael
02:44
created

ArrayConnectionConfigurationLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A chosen() 0 19 4
A __construct() 0 3 1
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 InvalidArgumentException;
11
use function file_exists;
12
use function is_array;
13
14
class ArrayConnectionConfigurationLoader implements ConnectionLoaderInterface
15
{
16
    /** @var null|string */
17
    private $filename;
18
19 38
    public function __construct(?string $filename)
20
    {
21 38
        $this->filename = $filename;
22 38
    }
23
24
    /**
25
     * Read the input and return a Configuration, returns null if the config
26
     * is not supported.
27
     *
28
     * @throws InvalidArgumentException
29
     */
30 38
    public function chosen() : ?Connection
31
    {
32 38
        if (empty($this->filename)) {
33 37
            return null;
34
        }
35
36 38
        if (! file_exists($this->filename)) {
37 37
            return null;
38
        }
39
40 1
        $params = include $this->filename;
41
42 1
        if (! is_array($params)) {
43
            throw new InvalidArgumentException(
44
                'The connection file has to return an array with database configuration parameters.'
45
            );
46
        }
47
48 1
        return DriverManager::getConnection($params);
49
    }
50
}
51