Passed
Pull Request — master (#2)
by Alex
08:14
created

ConfigurationManager::getConfiguration()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
c 0
b 0
f 0
rs 10
cc 4
nc 4
nop 1
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\ConfigurationManagerException;
9
use Doctrine\ORM\Configuration;
10
11
/**
12
 * @author  Alex Patterson <[email protected]>
13
 * @package Arp\LaminasDoctrine\Service\Configuration
14
 */
15
final class ConfigurationManager implements ConfigurationManagerInterface
16
{
17
    /**
18
     * @var ConfigurationFactoryInterface
19
     */
20
    private ConfigurationFactoryInterface $configurationFactory;
21
22
    /**
23
     * @var DoctrineConfig
24
     */
25
    private DoctrineConfig $config;
26
27
    /**
28
     * @var Configuration[]
29
     */
30
    private array $configurations = [];
31
32
    /**
33
     * @param ConfigurationFactoryInterface $configurationFactory
34
     * @param DoctrineConfig                $doctrineConfig
35
     */
36
    public function __construct(ConfigurationFactoryInterface $configurationFactory, DoctrineConfig $doctrineConfig)
37
    {
38
        $this->configurationFactory = $configurationFactory;
39
        $this->config = $doctrineConfig;
40
    }
41
42
    /**
43
     * @param string $name
44
     *
45
     * @return bool
46
     */
47
    public function hasConfiguration(string $name): bool
48
    {
49
        return isset($this->configurations[$name]) || $this->config->hasConfigurationConfig($name);
50
    }
51
52
    /**
53
     * @param string $name
54
     * @param array  $config
55
     */
56
    public function addConfigurationConfig(string $name, array $config): void
57
    {
58
        $this->config->setConfigurationConfig($name, $config);
59
    }
60
61
    /**
62
     * @param string $name
63
     *
64
     * @return Configuration
65
     *
66
     * @throws ConfigurationManagerException
67
     */
68
    public function getConfiguration(string $name): Configuration
69
    {
70
        if (!isset($this->configurations[$name]) && $this->config->hasConfigurationConfig($name)) {
71
            $this->configurations[$name] = $this->create($name, $this->config->getConfigurationConfig($name));
72
        }
73
74
        if (isset($this->configurations[$name])) {
75
            return $this->configurations[$name];
76
        }
77
78
        throw new ConfigurationManagerException(
79
            sprintf('Unable to find Doctrine Configuration registered with name \'%s\'', $name)
80
        );
81
    }
82
83
    /**
84
     * @param iterable $configurations
85
     */
86
    public function setConfigurations(iterable $configurations): void
87
    {
88
        $this->configurations = [];
89
90
        foreach ($configurations as $name => $configuration) {
91
            if (is_array($configuration)) {
92
                $this->addConfigurationConfig($name, $configuration);
93
            } else {
94
                $this->setConfiguration($name, $configuration);
95
            }
96
        }
97
    }
98
99
    /**
100
     * @param string        $name
101
     * @param Configuration $configuration
102
     */
103
    public function setConfiguration(string $name, Configuration $configuration): void
104
    {
105
        $this->configurations[$name] = $configuration;
106
    }
107
108
    /**
109
     * @param string $name
110
     * @param array  $config
111
     *
112
     * @return Configuration
113
     *
114
     * @throws ConfigurationManagerException
115
     */
116
    private function create(string $name, array $config): Configuration
117
    {
118
        try {
119
            return $this->configurationFactory->create($config);
120
        } catch (ConfigurationFactoryException $e) {
0 ignored issues
show
Bug introduced by
The type Arp\LaminasDoctrine\Serv...urationFactoryException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
121
            throw new ConfigurationManagerException(
122
                sprintf('Failed to create doctrine configuration \'%s\': %s', $name, $e->getMessage()),
123
                $e->getCode(),
124
                $e
125
            );
126
        }
127
    }
128
}
129