Passed
Pull Request — master (#2)
by Alex
02:52
created

QueryServiceFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 18
c 0
b 0
f 0
dl 0
loc 46
rs 10

1 Method

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