MappingDriverChainFactory::createDriver()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 10
nop 3
dl 0
loc 30
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Mapping\Driver;
6
7
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
8
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
9
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
10
use Laminas\ServiceManager\ServiceLocatorInterface;
11
use Psr\Container\ContainerExceptionInterface;
12
use Psr\Container\ContainerInterface;
13
use Psr\Container\NotFoundExceptionInterface;
14
15
final class MappingDriverChainFactory extends AbstractDriverFactory
16
{
17
    /**
18
     * @param ContainerInterface&ServiceLocatorInterface $container
19
     * @param array<mixed>|null $options
20
     *
21
     * @throws ServiceNotCreatedException
22
     * @throws ContainerExceptionInterface
23
     * @throws NotFoundExceptionInterface
24
     */
25
    public function __invoke(
26
        ContainerInterface $container,
27
        string $requestedName,
28
        array $options = null
29
    ): MappingDriverChain {
30
        $options = $options ?? $this->getOptions($container, $requestedName, $options);
31
32
        $driverChain = new MappingDriverChain();
33
34
        if (!empty($options['drivers'])) {
35
            foreach ($options['drivers'] as $namespace => $driver) {
36
                if (empty($driver)) {
37
                    continue;
38
                }
39
                $driverChain->addDriver($this->createDriver($container, $driver, $requestedName), $namespace);
40
            }
41
        }
42
43
        return $driverChain;
44
    }
45
46
    /**
47
     * @param MappingDriver|string|array<mixed> $driver
48
     *
49
     * @throws ContainerExceptionInterface
50
     * @throws NotFoundExceptionInterface
51
     * @throws ServiceNotCreatedException
52
     */
53
    private function createDriver(
54
        ServiceLocatorInterface $container,
55
        MappingDriver|string|array $driver,
56
        string $serviceName
57
    ): MappingDriver {
58
        if (is_string($driver)) {
0 ignored issues
show
introduced by
The condition is_string($driver) is always false.
Loading history...
59
            $driver = $this->getOptions($container, $driver);
60
        }
61
62
        if (is_array($driver)) {
0 ignored issues
show
introduced by
The condition is_array($driver) is always true.
Loading history...
63
            if (empty($driver['class'])) {
64
                throw new ServiceNotCreatedException(
65
                    sprintf('The required \'class\' configuration option is missing for service \'%s\'', $serviceName),
66
                );
67
            }
68
            $driver = $this->buildService($container, $driver['class'], $driver, $serviceName);
69
        }
70
71
        if (!$driver instanceof MappingDriver) {
72
            throw new ServiceNotCreatedException(
73
                sprintf(
74
                    'The mapping driver must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
75
                    MappingDriver::class,
76
                    is_object($driver) ? get_class($driver) : gettype($driver),
77
                    $serviceName
78
                )
79
            );
80
        }
81
82
        return $driver;
83
    }
84
}
85