Configuration::toArray()   A
last analyzed

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 0
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 3
    public function __construct(array $config)
15
    {
16 3
        $this->config = $config;
17
    }
18
19
    /**
20
     * Get the driver name.
21
     */
22 3
    public function getDriver(): string
23
    {
24 3
        return $this->config['driver'];
25
    }
26
27
    /**
28
     * Get the connection name.
29
     */
30 3
    public function getConnection(): string
31
    {
32 3
        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 3
    public function toArray(): array
55
    {
56 3
        return $this->config;
57
    }
58
59
    /**
60
     * Create the configuration of the connection driver.
61
     */
62 3
    public static function createConfiguration(string $name, array $options): self
63
    {
64
        // pre format driver options
65 3
        $preparedConfig = [];
66
67 3
        if (isset($options['url'])) {
68 3
            $preparedConfig = ResolverConnectionDriverFactory::parseDsn($options['url']);
69
        }
70 3
        if (!empty($options['options'])) {
71 1
            $options = array_merge($options, $options['options']);
72
        }
73
74 3
        unset($options['url'], $options['options'], $options['connection_factory']);
75 3
        $preparedConfig = array_merge($preparedConfig, array_filter($options));
76 3
        $factory = new ResolverConnectionDriverFactory([$name => $preparedConfig]);
77
78 3
        return new self($factory->getConfig($name));
79
    }
80
}
81