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

ConfigurationFactory::__construct()   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 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\ServiceManager;
10
11
/**
12
 * Factory class for the Doctrine Configuration via the Laminas ServiceManager. This is not ideal as we treat the
13
 * manager as a ServiceLocator, however this class is already an abstraction that is used as an implementation detail
14
 * of ConfigurationManager.
15
 *
16
 * @author  Alex Patterson <[email protected]>
17
 * @package Arp\LaminasDoctrine\Service\Configuration
18
 */
19
final class ConfigurationFactory implements ConfigurationFactoryInterface
20
{
21
    /**
22
     * @var ServiceManager
23
     */
24
    private ServiceManager $serviceManager;
25
26
    /**
27
     * @param ServiceManager $serviceManager
28
     */
29
    public function __construct(ServiceManager $serviceManager)
30
    {
31
        $this->serviceManager = $serviceManager;
32
    }
33
34
    /**
35
     * @param array $config
36
     *
37
     * @return Configuration
38
     *
39
     * @throws ConfigurationFactoryException
40
     */
41
    public function create(array $config): Configuration
42
    {
43
        try {
44
            return $this->serviceManager->build(Configuration::class, $config);
45
        } catch (\Throwable $e) {
46
            throw new ConfigurationFactoryException(
47
                sprintf('Failed to create ORM Configuration: %s', $e->getMessage()),
48
                $e->getCode(),
49
                $e
50
            );
51
        }
52
    }
53
}
54