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

ArrayConnectionConfigurationLoader::__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\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