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

QueryServiceFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 23
c 1
b 0
f 0
dl 0
loc 57
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 40 3
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\EntityManager\EntityManagerFactoryProviderTrait;
10
use Arp\LaminasFactory\AbstractFactory;
11
use Arp\LaminasMonolog\Factory\FactoryLoggerProviderTrait;
12
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
13
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
14
use Laminas\ServiceManager\ServiceLocatorInterface;
15
use Psr\Container\ContainerExceptionInterface;
16
use Psr\Container\ContainerInterface;
17
use Psr\Container\NotFoundExceptionInterface;
18
19
/**
20
 * @author  Alex Patterson <[email protected]>
21
 * @package Arp\LaminasDoctrine\Factory\Repository\Query
22
 */
23
class QueryServiceFactory extends AbstractFactory
24
{
25
    use EntityManagerFactoryProviderTrait;
26
    use FactoryLoggerProviderTrait;
27
28
    /**
29
     * @param ContainerInterface&ServiceLocatorInterface $container
30
     * @param string                                     $requestedName
31
     * @param array<string, mixed>|null                  $options
32
     *
33
     * @return QueryServiceInterface
34
     *
35
     * @throws ServiceNotCreatedException
36
     * @throws ServiceNotFoundException
37
     * @throws ContainerExceptionInterface
38
     * @throws NotFoundExceptionInterface
39
     */
40
    public function __invoke(
41
        ContainerInterface $container,
42
        string $requestedName,
43
        array $options = null
44
    ): QueryServiceInterface {
45
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'query_services');
46
47
        $className = $options['class_name'] ?? QueryService::class;
48
        $entityName = $options['entity_name'] ?? $requestedName;
49
50
        if (empty($entityName)) {
51
            throw new ServiceNotCreatedException(
52
                sprintf(
53
                    'The required \'entity_name\' configuration option is missing for service \'%s\'',
54
                    $requestedName
55
                )
56
            );
57
        }
58
59
        $entityManager = $options['entity_manager'] ?? null;
60
        if (empty($entityManager)) {
61
            throw new ServiceNotCreatedException(
62
                sprintf(
63
                    'The required \'entity_manager\' configuration option is missing for service \'%s\'',
64
                    $requestedName
65
                )
66
            );
67
        }
68
69
        $entityManager = $this->getEntityManager($container, $entityManager, $requestedName);
70
71
        /** @var QueryServiceInterface $queryService */
72
        /** @noinspection PhpUnnecessaryLocalVariableInspection */
73
        $queryService = new $className(
74
            $entityName,
75
            $entityManager,
76
            $this->getLogger($container, $options['logger'] ?? null, $requestedName)
77
        );
78
79
        return $queryService;
80
    }
81
}
82