Test Failed
Pull Request — master (#2)
by Alex
02:44
created

ConfigurationManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 28
dl 0
loc 110
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfiguration() 0 3 1
A getConfiguration() 0 12 4
A create() 0 9 2
A addConfigurationConfig() 0 3 1
A __construct() 0 4 1
A hasConfiguration() 0 3 2
A setConfigurations() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Service\Configuration;
6
7
use Arp\LaminasDoctrine\Config\DoctrineConfig;
8
use Arp\LaminasDoctrine\Service\Configuration\Exception\ConfigurationFactoryException;
9
use Arp\LaminasDoctrine\Service\Configuration\Exception\ConfigurationManagerException;
10
use Doctrine\ORM\Configuration;
11
12
/**
13
 * @author  Alex Patterson <[email protected]>
14
 * @package Arp\LaminasDoctrine\Service\Configuration
15
 */
16
final class ConfigurationManager implements ConfigurationManagerInterface
17
{
18
    /**
19
     * @var ConfigurationFactoryInterface
20
     */
21
    private ConfigurationFactoryInterface $configurationFactory;
22
23
    /**
24
     * @var DoctrineConfig
25
     */
26
    private DoctrineConfig $config;
27
28
    /**
29
     * @var Configuration[]
30
     */
31
    private array $configurations = [];
32
33
    /**
34
     * @param ConfigurationFactoryInterface $configurationFactory
35
     * @param DoctrineConfig                $doctrineConfig
36
     */
37
    public function __construct(ConfigurationFactoryInterface $configurationFactory, DoctrineConfig $doctrineConfig)
38
    {
39
        $this->configurationFactory = $configurationFactory;
40
        $this->config = $doctrineConfig;
41
    }
42
43
    /**
44
     * @param string $name
45
     *
46
     * @return bool
47
     */
48
    public function hasConfiguration(string $name): bool
49
    {
50
        return isset($this->configurations[$name]) || $this->config->hasConfigurationConfig($name);
51
    }
52
53
    /**
54
     * @param string $name
55
     *
56
     * @return Configuration
57
     *
58
     * @throws ConfigurationManagerException
59
     */
60
    public function getConfiguration(string $name): Configuration
61
    {
62
        if (!isset($this->configurations[$name]) && $this->config->hasConfigurationConfig($name)) {
63
            $this->configurations[$name] = $this->create($name, $this->config->getConfigurationConfig($name));
64
        }
65
66
        if (isset($this->configurations[$name])) {
67
            return $this->configurations[$name];
68
        }
69
70
        throw new ConfigurationManagerException(
71
            sprintf('Unable to find Doctrine Configuration registered with name \'%s\'', $name)
72
        );
73
    }
74
75
    /**
76
     * @param iterable<string, Configuration|array> $configurations
77
     */
78
    public function setConfigurations(iterable $configurations): void
79
    {
80
        $this->configurations = [];
81
82
        foreach ($configurations as $name => $configuration) {
83
            if (is_array($configuration)) {
84
                $this->addConfigurationConfig($name, $configuration);
85
            } else {
86
                $this->setConfiguration($name, $configuration);
87
            }
88
        }
89
    }
90
91
    /**
92
     * @param string        $name
93
     * @param Configuration $configuration
94
     */
95
    public function setConfiguration(string $name, Configuration $configuration): void
96
    {
97
        $this->configurations[$name] = $configuration;
98
    }
99
100
    /**
101
     * @param string               $name
102
     * @param array<string, mixed> $config
103
     */
104
    public function addConfigurationConfig(string $name, array $config): void
105
    {
106
        $this->config->setConfigurationConfig($name, $config);
107
    }
108
109
    /**
110
     * @param string               $name
111
     * @param array<string, mixed> $config
112
     *
113
     * @return Configuration
114
     *
115
     * @throws ConfigurationManagerException
116
     */
117
    private function create(string $name, array $config): Configuration
118
    {
119
        try {
120
            return $this->configurationFactory->create($config);
121
        } catch (ConfigurationFactoryException $e) {
122
            throw new ConfigurationManagerException(
123
                sprintf('Failed to create doctrine configuration \'%s\': %s', $name, $e->getMessage()),
124
                $e->getCode(),
125
                $e
126
            );
127
        }
128
    }
129
}
130