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

ConnectionFactoryFactory::__invoke()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 33
rs 9.1111
cc 6
nc 6
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Service\Connection;
6
7
use Arp\LaminasDoctrine\Config\ConnectionConfigs;
8
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationManager;
9
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationManagerInterface;
10
use Arp\LaminasDoctrine\Service\Connection\ConnectionFactory;
11
use Arp\LaminasFactory\AbstractFactory;
12
use Doctrine\DBAL\Driver\PDO\MySQL\Driver;
13
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
14
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
15
use Psr\Container\ContainerExceptionInterface;
16
use Psr\Container\ContainerInterface;
17
18
/**
19
 * @author  Alex Patterson <[email protected]>
20
 * @package Arp\LaminasDoctrine\Factory\Service
21
 */
22
final class ConnectionFactoryFactory extends AbstractFactory
23
{
24
    /**
25
     * @var array<string, mixed>
26
     */
27
    private array $defaultConnectionConfig = [
28
        'driverClass'   => Driver::class,
29
        'driverOptions' => null,
30
    ];
31
32
    /**
33
     * @param ContainerInterface $container
34
     * @param string             $requestedName
35
     * @param array<mixed>|null  $options
36
     *
37
     * @return ConnectionFactory
38
     *
39
     * @throws ServiceNotCreatedException
40
     * @throws ServiceNotFoundException
41
     * @throws ContainerExceptionInterface
42
     */
43
    public function __invoke(
44
        ContainerInterface $container,
45
        string $requestedName,
46
        array $options = null
47
    ): ConnectionFactory {
48
        if (null === $options) {
49
            /** @var ConnectionConfigs $connectionConfigs */
50
            $connectionConfigs = $this->getService($container, ConnectionConfigs::class, $requestedName);
51
52
            $options = $connectionConfigs->hasConnectionConfig($requestedName)
53
                ? $connectionConfigs->getConnectionConfig($requestedName)
54
                : [];
55
        }
56
57
        $connectionFactory = $options['factory'] ?? null;
58
        if (null !== $connectionFactory && !is_callable($connectionFactory)) {
59
            throw new ServiceNotCreatedException(
60
                sprintf(
61
                    'The \'factory\' must be of type \'callable\'; \'%s\' provided for service \'%s\'',
62
                    is_object($connectionFactory) ? get_class($connectionFactory) : gettype($connectionFactory),
63
                    $requestedName
64
                )
65
            );
66
        }
67
68
        /** @var ConfigurationManager $configurationManager */
69
        $configurationManager = $this->getService(
70
            $container,
71
            $options['manager'] ?? ConfigurationManagerInterface::class,
72
            $requestedName
73
        );
74
75
        return new ConnectionFactory($configurationManager, $connectionFactory, $this->defaultConnectionConfig);
76
    }
77
78
    /**
79
     * @param array<mixed> $defaultConnectionConfig
80
     */
81
    public function setDefaultConnectionConfig(array $defaultConnectionConfig): void
82
    {
83
        $this->defaultConnectionConfig = $defaultConnectionConfig;
84
    }
85
}
86