ConfigurationManager   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 83
rs 10
wmc 14

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\ConfigurationConfigs;
8
use Arp\LaminasDoctrine\Service\Configuration\Exception\ConfigurationFactoryException;
9
use Arp\LaminasDoctrine\Service\Configuration\Exception\ConfigurationManagerException;
10
use Doctrine\ORM\Configuration;
11
12
final class ConfigurationManager implements ConfigurationManagerInterface
13
{
14
    /**
15
     * @var array<string, Configuration>
16
     */
17
    private array $configurations = [];
18
19
    /**
20
     * @param ConfigurationFactoryInterface $configurationFactory
21
     * @param ConfigurationConfigs          $configs
22
     */
23
    public function __construct(
24
        private readonly ConfigurationFactoryInterface $configurationFactory,
25
        private readonly ConfigurationConfigs $configs
26
    ) {
27
    }
28
29
    public function hasConfiguration(string $name): bool
30
    {
31
        return isset($this->configurations[$name]) || $this->configs->hasConfigurationConfig($name);
32
    }
33
34
    /**
35
     * @throws ConfigurationManagerException
36
     */
37
    public function getConfiguration(string $name): Configuration
38
    {
39
        if (!isset($this->configurations[$name]) && $this->configs->hasConfigurationConfig($name)) {
40
            $this->configurations[$name] = $this->create($name, $this->configs->getConfigurationConfig($name));
41
        }
42
43
        if (isset($this->configurations[$name])) {
44
            return $this->configurations[$name];
45
        }
46
47
        throw new ConfigurationManagerException(
48
            sprintf('Unable to find Doctrine Configuration registered with name \'%s\'', $name)
49
        );
50
    }
51
52
    /**
53
     * @param iterable<string, Configuration|array<mixed>> $configurations
54
     */
55
    public function setConfigurations(iterable $configurations): void
56
    {
57
        $this->configurations = [];
58
59
        foreach ($configurations as $name => $configuration) {
60
            if (is_array($configuration)) {
61
                $this->addConfigurationConfig($name, $configuration);
62
            } else {
63
                $this->setConfiguration($name, $configuration);
64
            }
65
        }
66
    }
67
68
    public function setConfiguration(string $name, Configuration $configuration): void
69
    {
70
        $this->configurations[$name] = $configuration;
71
    }
72
73
    /**
74
     * @param array<string, mixed> $config
75
     */
76
    public function addConfigurationConfig(string $name, array $config): void
77
    {
78
        $this->configs->setConfigurationConfig($name, $config);
79
    }
80
81
    /**
82
     * @param array<string, mixed> $config
83
     *
84
     * @throws ConfigurationManagerException
85
     */
86
    private function create(string $name, array $config): Configuration
87
    {
88
        try {
89
            return $this->configurationFactory->create($config);
90
        } catch (ConfigurationFactoryException $e) {
91
            throw new ConfigurationManagerException(
92
                sprintf('Failed to create doctrine configuration \'%s\'', $name),
93
                $e->getCode(),
94
                $e
95
            );
96
        }
97
    }
98
}
99