QueryServiceFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

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

1 Method

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