DbalProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 59
ccs 17
cts 20
cp 0.85
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setContext() 0 4 1
A init() 0 18 3
A get() 0 6 1
1
<?php
2
/**
3
 * This file is part of the Ray.DbalModule package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\DbalModule;
8
9
use Doctrine\DBAL\DriverManager;
10
use Ray\DbalModule\Annotation\DbalConfig;
11
use Ray\Di\Di\PostConstruct;
12
use Ray\Di\InjectorInterface;
13
use Ray\Di\ProviderInterface;
14
use Ray\Di\SetContextInterface;
15
16
class DbalProvider implements ProviderInterface, SetContextInterface
17
{
18
    /**
19
     * @var InjectorInterface
20
     */
21
    private $injector;
22
23
    /**
24
     * @var string
25
     */
26
    private $context;
27
28
    /**
29
     * @var array
30
     */
31
    private $config;
32
33 2
    public function __construct(InjectorInterface $injector)
34
    {
35 2
        $this->injector = $injector;
36 2
    }
37
38 2
    public function setContext($context)
39
    {
40 2
        $this->context = $context;
41 2
    }
42
43
    /**
44
     * @PostConstruct
45
     */
46 2
    public function init()
47
    {
48 2
        $config = $this->injector->getInstance('', DbalConfig::class . $this->context);
49 2
        if (is_array($config)) {
50
            $this->config = $config;
51
52
            return;
53
        }
54 2
        if (is_string($config)) {
55 2
            $parsedConfig = [];
56 2
            parse_str($config, $parsedConfig);
57 2
            $this->config = $parsedConfig;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parsedConfig can be null. However, the property $config is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
58
59 2
            return;
60
        }
61
62
        throw new \InvalidArgumentException('@DbalConfig');
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 2
    public function get()
69
    {
70 2
        $conn = DriverManager::getConnection($this->config);
71
72 2
        return $conn;
73
    }
74
}
75