Passed
Push — master ( 79bf3c...52a24b )
by Alex
01:04 queued 13s
created

ConfigurationFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Service\Configuration;
6
7
use Arp\LaminasDoctrine\Service\Configuration\Exception\ConfigurationFactoryException;
8
use Doctrine\ORM\Configuration;
9
use Laminas\ServiceManager\ServiceLocatorInterface;
10
use Psr\Container\ContainerExceptionInterface;
11
12
/**
13
 * Factory class for the Doctrine Configuration via the Laminas ServiceManager. This is not ideal as we treat the
14
 * manager as a ServiceLocator, however this class is already an abstraction that is used as an implementation detail
15
 * of ConfigurationManager.
16
 *
17
 * @author  Alex Patterson <[email protected]>
18
 * @package Arp\LaminasDoctrine\Service
19
 */
20
final class ConfigurationFactory implements ConfigurationFactoryInterface
21
{
22
    /**
23
     * @var ServiceLocatorInterface
24
     */
25
    private ServiceLocatorInterface $serviceManager;
26
27
    /**
28
     * @param ServiceLocatorInterface $serviceManager
29
     */
30
    public function __construct(ServiceLocatorInterface $serviceManager)
31
    {
32
        $this->serviceManager = $serviceManager;
33
    }
34
35
    /**
36
     * @param array<string, mixed> $config
37
     *
38
     * @return Configuration
39
     *
40
     * @throws ConfigurationFactoryException
41
     */
42
    public function create(array $config): Configuration
43
    {
44
        try {
45
            return $this->serviceManager->build(Configuration::class, $config);
46
        } catch (ContainerExceptionInterface $e) {
47
            throw new ConfigurationFactoryException(
48
                sprintf('Failed to create ORM Configuration: %s', $e->getMessage()),
49
                $e->getCode(),
50
                $e
51
            );
52
        }
53
    }
54
}
55