Test Failed
Pull Request — master (#2)
by Alex
02:32
created

QueryServiceFactory::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 32
rs 9.7333
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 Interop\Container\ContainerInterface;
12
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
13
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
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
     * @noinspection PhpMissingParamTypeInspection
26
     *
27
     * @param ContainerInterface $container
28
     * @param string             $requestedName
29
     * @param array|null         $options
30
     *
31
     * @return QueryServiceInterface
32
     *
33
     * @throws ServiceNotCreatedException
34
     * @throws ServiceNotFoundException
35
     */
36
    public function __invoke(
37
        ContainerInterface $container,
38
        $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($entityName, $entityManager, new NullLogger());
68
    }
69
}
70