DbalConnectionFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 38
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 18 3
1
<?php
2
namespace PSB\Persistence\Doctrine2;
3
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\DBALException;
7
use Doctrine\DBAL\DriverManager;
8
use PSB\Core\Exception\UnexpectedValueException;
9
use PSB\Core\Util\Settings;
10
11
class DbalConnectionFactory
12
{
13
    /**
14
     * @var Settings
15
     */
16
    private $settings;
17
18
    /**
19
     * @param Settings $settings
20
     */
21 5
    public function __construct(Settings $settings)
22
    {
23 5
        $this->settings = $settings;
24 5
    }
25
26
    /**
27
     * @return Connection
28
     * @throws DBALException
29
     */
30 3
    public function __invoke()
31
    {
32 3
        $connection = $this->settings->tryGet(Doctrine2KnownSettingsEnum::CONNECTION);
33
34 3
        if ($connection) {
35 1
            return $connection;
36
        }
37
38 2
        $connectionParameters = $this->settings->tryGet(Doctrine2KnownSettingsEnum::CONNECTION_PARAMETERS);
39 2
        if (!$connectionParameters) {
40 1
            throw new UnexpectedValueException(
41
                "The Doctrine 2 persistence requires either a connection instance or connection parameters. " .
42 1
                "You can provide them through the persistence configurator."
43
            );
44
        }
45
46 1
        return DriverManager::getConnection($connectionParameters);
47
    }
48
}
49