Passed
Pull Request — master (#2)
by Alex
24:05 queued 15:48
created

QueryServiceFactory::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 35
rs 9.6333
cc 3
nc 3
nop 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\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\ContainerInterface;
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
    use FactoryLoggerProviderTrait;
25
26
    /**
27
     * @param ContainerInterface&ServiceLocatorInterface $container
28
     * @param string                                     $requestedName
29
     * @param array<string, mixed>|null                  $options
30
     *
31
     * @return QueryServiceInterface
32
     *
33
     * @throws ServiceNotCreatedException
34
     * @throws ServiceNotFoundException
35
     */
36
    public function __invoke(
37
        ContainerInterface $container,
38
        string $requestedName,
39
        array $options = null
40
    ): QueryServiceInterface {
41
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'query_services');
42
43
        $className = $options['class_name'] ?? QueryService::class;
44
        $entityName = $options['entity_name'] ?? $requestedName;
45
46
        if (empty($entityName)) {
47
            throw new ServiceNotCreatedException(
48
                sprintf(
49
                    'The required \'entity_name\' configuration option is missing for service \'%s\'',
50
                    $requestedName
51
                )
52
            );
53
        }
54
55
        $entityManager = $options['entity_manager'] ?? null;
56
        if (empty($entityManager)) {
57
            throw new ServiceNotCreatedException(
58
                sprintf(
59
                    'The required \'entity_manager\' configuration option is missing for service \'%s\'',
60
                    $requestedName
61
                )
62
            );
63
        }
64
65
        $entityManager = $this->getEntityManager($container, $entityManager, $requestedName);
66
67
        return new $className(
68
            $entityName,
69
            $entityManager,
70
            $this->getLogger($container, $options['logger'] ?? null, $requestedName)
71
        );
72
    }
73
}
74