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

ConnectionManagerFactory::__invoke()   A

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
/**
18
 * @author  Alex Patterson <[email protected]>
19
 * @package Arp\LaminasDoctrine\Factory\Service
20
 */
21
final class ConnectionManagerFactory extends AbstractFactory
22
{
23
    /**
24
     * @param ContainerInterface        $container
25
     * @param string                    $requestedName
26
     * @param array<string, mixed>|null $options
27
     *
28
     * @return ConnectionManager
29
     *
30
     * @throws ServiceNotCreatedException
31
     * @throws ServiceNotFoundException
32
     * @throws ContainerExceptionInterface
33
     */
34
    public function __invoke(
35
        ContainerInterface $container,
36
        string $requestedName,
37
        array $options = null
38
    ): ConnectionManager {
39
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
40
41
        /** @var ConnectionConfigs $configs */
42
        $configs = $this->getService($container, ConnectionConfigs::class, $requestedName);
43
44
        /** @var ConnectionFactoryInterface $connectionFactory */
45
        $connectionFactory = $this->getService(
46
            $container,
47
            $options['factory'] ?? ConnectionFactoryInterface::class,
48
            $requestedName
49
        );
50
51
        $connections = [];
52
        if (!empty($options['connections'])) {
53
            foreach ($options['connections'] as $name => $connection) {
54
                if ($connection instanceof Connection) {
55
                    $connections[$name] = $connection;
56
                }
57
            }
58
        }
59
60
        return new ConnectionManager($configs, $connectionFactory, $connections);
61
    }
62
}
63