AbstractDriverFactory::getOptions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 16
rs 10
cc 3
nc 3
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Mapping\Driver;
6
7
use Arp\LaminasDoctrine\Config\DoctrineConfigInterface;
8
use Arp\LaminasFactory\AbstractFactory;
9
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
10
use Psr\Container\ContainerExceptionInterface;
11
use Psr\Container\ContainerInterface;
12
use Psr\Container\NotFoundExceptionInterface;
13
14
abstract class AbstractDriverFactory extends AbstractFactory
15
{
16
    /**
17
     * @var array<mixed>
18
     */
19
    protected array $defaultOptions = [];
20
21
    /**
22
     * @param array<string, mixed>|null $options
23
     *
24
     * @return array<string, mixed>
25
     *
26
     * @throws ServiceNotCreatedException
27
     * @throws ContainerExceptionInterface
28
     * @throws NotFoundExceptionInterface
29
     */
30
    protected function getOptions(ContainerInterface $container, string $driverName, ?array $options = null): array
31
    {
32
        if (null === $options) {
33
            /** @var DoctrineConfigInterface $doctrineConfig */
34
            $doctrineConfig = $container->get(DoctrineConfigInterface::class);
35
36
            if (!$doctrineConfig->hasDriverConfig($driverName)) {
37
                throw new ServiceNotCreatedException(
38
                    sprintf('Unable to find driver configuration for \'%s\'', $driverName)
39
                );
40
            }
41
42
            $options = $doctrineConfig->getDriverConfig($driverName);
43
        }
44
45
        return array_replace_recursive($this->defaultOptions, $options);
46
    }
47
}
48