ConnectionManagerFactory::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 27
rs 9.8666
cc 4
nc 2
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\Connection\ConnectionFactoryInterface;
9
use Arp\LaminasDoctrine\Service\Connection\ConnectionManager;
10
use Arp\LaminasFactory\AbstractFactory;
11
use Doctrine\DBAL\Connection;
12
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
13
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
14
use Psr\Container\ContainerExceptionInterface;
15
use Psr\Container\ContainerInterface;
16
17
final class ConnectionManagerFactory extends AbstractFactory
18
{
19
    /**
20
     * @param ContainerInterface $container
21
     * @param string $requestedName
22
     * @param array<string, mixed>|null $options
23
     *
24
     * @return ConnectionManager
25
     *
26
     * @throws ServiceNotCreatedException
27
     * @throws ServiceNotFoundException
28
     * @throws ContainerExceptionInterface
29
     */
30
    public function __invoke(
31
        ContainerInterface $container,
32
        string $requestedName,
33
        array $options = null
34
    ): ConnectionManager {
35
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
36
37
        /** @var ConnectionConfigs $configs */
38
        $configs = $this->getService($container, ConnectionConfigs::class, $requestedName);
39
40
        /** @var ConnectionFactoryInterface $connectionFactory */
41
        $connectionFactory = $this->getService(
42
            $container,
43
            $options['factory'] ?? ConnectionFactoryInterface::class,
44
            $requestedName
45
        );
46
47
        $connections = [];
48
        if (!empty($options['connections'])) {
49
            foreach ($options['connections'] as $name => $connection) {
50
                if ($connection instanceof Connection) {
51
                    $connections[$name] = $connection;
52
                }
53
            }
54
        }
55
56
        return new ConnectionManager($configs, $connectionFactory, $connections);
57
    }
58
}
59