Passed
Pull Request — master (#2)
by Alex
08:14
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
 * @deprecated
19
 *
20
 * @author  Alex Patterson <[email protected]>
21
 * @package Arp\LaminasDoctrine\Factory\Repository\Query
22
 */
23
class QueryServiceFactory extends AbstractFactory
24
{
25
    use EntityManagerFactoryProviderTrait;
0 ignored issues
show
Deprecated Code introduced by
The trait Arp\LaminasDoctrine\Fact...gerFactoryProviderTrait has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

25
    use /** @scrutinizer ignore-deprecated */ EntityManagerFactoryProviderTrait;

This trait has been deprecated. The supplier of the trait has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.

Loading history...
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
     */
38
    public function __invoke(
39
        ContainerInterface $container,
40
        string $requestedName,
41
        array $options = null
42
    ): QueryServiceInterface {
43
        $options = $options ?? $this->getServiceOptions($container, $requestedName, 'query_services');
44
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