Passed
Pull Request — master (#2)
by Alex
08:23
created

QueryServiceFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 33
c 0
b 0
f 0
dl 0
loc 84
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 35 3
A getLogger() 0 22 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Repository\Query;
6
7
use Arp\DoctrineEntityRepository\Query\QueryService;
8
use Arp\DoctrineEntityRepository\Query\QueryServiceInterface;
9
use Arp\LaminasDoctrine\Factory\Service\EntityManagerFactoryProviderTrait;
10
use Arp\LaminasFactory\AbstractFactory;
11
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
12
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
13
use Psr\Container\ContainerInterface;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\NullLogger;
16
17
/**
18
 * @author  Alex Patterson <[email protected]>
19
 * @package Arp\LaminasDoctrine\Factory\Repository\Query
20
 */
21
class QueryServiceFactory extends AbstractFactory
22
{
23
    use EntityManagerFactoryProviderTrait;
24
25
    /**
26
     * @param ContainerInterface        $container
27
     * @param string                    $requestedName
28
     * @param array<string, mixed>|null $options
29
     *
30
     * @return QueryServiceInterface
31
     *
32
     * @throws ServiceNotCreatedException
33
     * @throws ServiceNotFoundException
34
     */
35
    public function __invoke(
36
        ContainerInterface $container,
37
        string $requestedName,
38
        array $options = null
39
    ): QueryServiceInterface {
40
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'query_services');
41
42
        $className = $options['class_name'] ?? QueryService::class;
43
        $entityName = $options['entity_name'] ?? $requestedName;
44
45
        if (empty($entityName)) {
46
            throw new ServiceNotCreatedException(
47
                sprintf(
48
                    'The required \'entity_name\' configuration option is missing for service \'%s\'',
49
                    $requestedName
50
                )
51
            );
52
        }
53
54
        $entityManager = $options['entity_manager'] ?? null;
55
        if (empty($entityManager)) {
56
            throw new ServiceNotCreatedException(
57
                sprintf(
58
                    'The required \'entity_manager\' configuration option is missing for service \'%s\'',
59
                    $requestedName
60
                )
61
            );
62
        }
63
64
        $entityManager = $this->getEntityManager($container, $entityManager, $requestedName);
65
66
        return new $className(
67
            $entityName,
68
            $entityManager,
69
            $this->getLogger($container, $options['logger'] ?? null, $requestedName)
70
        );
71
    }
72
73
    /**
74
     * @param ContainerInterface          $container
75
     * @param LoggerInterface|string|null $logger
76
     * @param string                      $serviceName
77
     *
78
     * @return LoggerInterface
79
     *
80
     * @throws ServiceNotCreatedException
81
     * @throws ServiceNotFoundException
82
     */
83
    private function getLogger(ContainerInterface $container, $logger, string $serviceName): LoggerInterface
84
    {
85
        if (null === $logger) {
86
            return new NullLogger();
87
        }
88
89
        if (is_string($logger)) {
90
            $logger = $this->getService($container, $logger, $serviceName);
91
        }
92
93
        if (!$logger instanceof LoggerInterface) {
94
            throw new ServiceNotCreatedException(
95
                sprintf(
96
                    'The logger must be of type \'%s\'; \'%s\' provided for service \'%s\'',
97
                    LoggerInterface::class,
98
                    is_object($logger) ? get_class($logger) : gettype($logger),
99
                    $serviceName
100
                )
101
            );
102
        }
103
104
        return $logger;
105
    }
106
}
107