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

ConfigurationManager::setConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
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
 * @deprecated
14
 * @author  Alex Patterson <[email protected]>
15
 * @package Arp\LaminasDoctrine\Service\Configuration
16
 */
17
final class ConfigurationManager implements ConfigurationManagerInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Arp\LaminasDoctrine\Serv...urationManagerInterface has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

17
final class ConfigurationManager implements /** @scrutinizer ignore-deprecated */ ConfigurationManagerInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
18
{
19
    /**
20
     * @var ConfigurationFactoryInterface
21
     */
22
    private ConfigurationFactoryInterface $configurationFactory;
23
24
    /**
25
     * @var DoctrineConfig
26
     */
27
    private DoctrineConfig $config;
28
29
    /**
30
     * @var Configuration[]
31
     */
32
    private array $configurations = [];
33
34
    /**
35
     * @param ConfigurationFactoryInterface $configurationFactory
36
     * @param DoctrineConfig                $doctrineConfig
37
     */
38
    public function __construct(ConfigurationFactoryInterface $configurationFactory, DoctrineConfig $doctrineConfig)
39
    {
40
        $this->configurationFactory = $configurationFactory;
41
        $this->config = $doctrineConfig;
42
    }
43
44
    /**
45
     * @param string $name
46
     *
47
     * @return bool
48
     */
49
    public function hasConfiguration(string $name): bool
50
    {
51
        return isset($this->configurations[$name]) || $this->config->hasConfigurationConfig($name);
52
    }
53
54
    /**
55
     * @param string $name
56
     *
57
     * @return Configuration
58
     *
59
     * @throws ConfigurationManagerException
60
     */
61
    public function getConfiguration(string $name): Configuration
62
    {
63
        if (!isset($this->configurations[$name]) && $this->config->hasConfigurationConfig($name)) {
64
            $this->configurations[$name] = $this->create($name, $this->config->getConfigurationConfig($name));
65
        }
66
67
        if (isset($this->configurations[$name])) {
68
            return $this->configurations[$name];
69
        }
70
71
        throw new ConfigurationManagerException(
0 ignored issues
show
Deprecated Code introduced by
The class Arp\LaminasDoctrine\Serv...urationManagerException has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

71
        throw /** @scrutinizer ignore-deprecated */ new ConfigurationManagerException(
Loading history...
72
            sprintf('Unable to find Doctrine Configuration registered with name \'%s\'', $name)
73
        );
74
    }
75
76
    /**
77
     * @param iterable<string, Configuration|array> $configurations
78
     */
79
    public function setConfigurations(iterable $configurations): void
80
    {
81
        $this->configurations = [];
82
83
        foreach ($configurations as $name => $configuration) {
84
            if (is_array($configuration)) {
85
                $this->addConfigurationConfig($name, $configuration);
86
            } else {
87
                $this->setConfiguration($name, $configuration);
88
            }
89
        }
90
    }
91
92
    /**
93
     * @param string        $name
94
     * @param Configuration $configuration
95
     */
96
    public function setConfiguration(string $name, Configuration $configuration): void
97
    {
98
        $this->configurations[$name] = $configuration;
99
    }
100
101
    /**
102
     * @param string               $name
103
     * @param array<string, mixed> $config
104
     */
105
    public function addConfigurationConfig(string $name, array $config): void
106
    {
107
        $this->config->setConfigurationConfig($name, $config);
108
    }
109
110
    /**
111
     * @param string               $name
112
     * @param array<string, mixed> $config
113
     *
114
     * @return Configuration
115
     *
116
     * @throws ConfigurationManagerException
117
     */
118
    private function create(string $name, array $config): Configuration
119
    {
120
        try {
121
            return $this->configurationFactory->create($config);
122
        } catch (ConfigurationFactoryException $e) {
123
            throw new ConfigurationManagerException(
0 ignored issues
show
Deprecated Code introduced by
The class Arp\LaminasDoctrine\Serv...urationManagerException has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

123
            throw /** @scrutinizer ignore-deprecated */ new ConfigurationManagerException(
Loading history...
124
                sprintf('Failed to create doctrine configuration \'%s\': %s', $name, $e->getMessage()),
125
                $e->getCode(),
126
                $e
127
            );
128
        }
129
    }
130
}
131