DbalConnectionFactory::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 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