Passed
Push — master ( 229462...d74785 )
by Andreas
02:20 queued 14s
created

ConnectionHelperLoader::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
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\Tools\Console\Helper\ConnectionHelper;
9
use Doctrine\Migrations\Configuration\Connection\ConnectionLoader;
10
use Symfony\Component\Console\Helper\HelperSet;
11
12
/**
13
 * The ConnectionHelperLoader is responsible for loading a Doctrine\DBAL\Connection from a Symfony Console HelperSet.
14
 *
15
 * @internal
16
 */
17
final class ConnectionHelperLoader implements ConnectionLoader
18
{
19
    /** @var string */
20
    private $helperName;
21
22
    /** @var  HelperSet */
23
    private $helperSet;
24
25
    /** @var ConnectionLoader */
26
    private $fallback;
27
28 6
    public function __construct(string $helperName, ConnectionLoader $fallback, ?HelperSet $helperSet = null)
29
    {
30 6
        $this->helperSet  = $helperSet ?: new HelperSet();
31 6
        $this->helperName = $helperName;
32 6
        $this->fallback   = $fallback;
33 6
    }
34
35
    /**
36
     * Read the input and return a Configuration, returns null if the config
37
     * is not supported.
38
     */
39 4
    public function getConnection() : Connection
40
    {
41 4
        if ($this->helperSet->has($this->helperName)) {
42 2
            $connectionHelper = $this->helperSet->get($this->helperName);
43
44 2
            if ($connectionHelper instanceof ConnectionHelper) {
45 2
                return $connectionHelper->getConnection();
46
            }
47
        }
48
49 2
        return $this->fallback->getConnection();
50
    }
51
}
52