Test Failed
Push — master ( 2530ac...3f2955 )
by Vincent
10:10 queued 07:25
created

Configuration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Bdf\QueueBundle\ConnectionFactory;
4
5
use Bdf\Queue\Connection\Factory\ResolverConnectionDriverFactory;
6
7
/**
8
 * Configuration
9
 */
10
final class Configuration
11
{
12
    private $config;
13
14 1
    public function __construct(array $config)
15
    {
16 1
        $this->config = $config;
17 1
    }
18
19
    /**
20
     * Get the driver name
21
     */
22 1
    public function getDriver(): string
23
    {
24 1
        return $this->config['driver'];
25
    }
26
27
    /**
28
     * Get the connection name
29
     */
30 1
    public function getConnection(): string
31
    {
32 1
        return $this->config['connection'];
33
    }
34
35
    /**
36
     * Get a configuration key
37
     */
38
    public function get(string $key, $default = null)
39
    {
40
        return $this->config[$key] ?? $default;
41
    }
42
43
    /**
44
     * Check whether a configuration key exists
45
     */
46
    public function has(string $key): bool
47
    {
48
        return isset($this->config[$key]);
49
    }
50
51
    /**
52
     * Get the array config
53
     */
54 1
    public function toArray(): array
55
    {
56 1
        return $this->config;
57
    }
58
59
    /**
60
     * Create the configuration of the connection driver
61
     */
62 1
    public static function createConfiguration(string $name, array $options): self
63
    {
64
        // pre format driver options
65 1
        $preparedConfig = [];
66
67 1
        if (isset($options['url'])) {
68 1
            $preparedConfig = ResolverConnectionDriverFactory::parseDsn($options['url']);
69
        }
70 1
        if (!empty($options['options'])) {
71 1
            $options = array_merge($options, $options['options']);
72
        }
73
74 1
        unset($options['url'], $options['options'], $options['connection_factory']);
75 1
        $preparedConfig = array_merge($preparedConfig, array_filter($options));
76 1
        $factory = new ResolverConnectionDriverFactory([$name => $preparedConfig]);
77
78 1
        return new self($factory->getConfig($name));
79
    }
80
}