ConfigurationFile::getConnection()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 10
cts 11
cp 0.9091
rs 9.2568
c 0
b 0
f 0
cc 5
nc 5
nop 0
crap 5.0187
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration\Connection;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\DriverManager;
9
use Doctrine\Migrations\Configuration\Connection\Exception\FileNotFound;
10
use Doctrine\Migrations\Configuration\Connection\Exception\InvalidConfiguration;
11
use function file_exists;
12
use function is_array;
13
14
/**
15
 * This class will return a Connection instance, loaded from a configuration file provided as argument.
16
 */
17
final class ConfigurationFile implements ConnectionLoader
18
{
19
    /** @var string */
20
    private $filename;
21
22 4
    public function __construct(string $filename)
23
    {
24 4
        $this->filename = $filename;
25 4
    }
26
27 4
    public function getConnection() : Connection
28
    {
29 4
        if (! file_exists($this->filename)) {
30 1
            throw FileNotFound::new($this->filename);
31
        }
32
33 3
        $params = include $this->filename;
34
35 3
        if ($params instanceof Connection) {
36 1
            return $params;
37
        }
38
39 2
        if ($params instanceof ConnectionLoader) {
40
            return $params->getConnection();
41
        }
42
43 2
        if (is_array($params)) {
44 1
            return DriverManager::getConnection($params);
45
        }
46
47 1
        throw InvalidConfiguration::invalidArrayConfiguration();
48
    }
49
}
50