ConfigurationFile   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 33
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConnection() 0 22 5
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