EntityManagerHelperFactory::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 27
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Console\Helper;
6
7
use Arp\LaminasDoctrine\Factory\Service\EntityManager\EntityManagerFactoryProviderTrait;
8
use Arp\LaminasDoctrine\Factory\Service\EntityManager\ObjectManagerArgvInputProviderTrait;
9
use Arp\LaminasFactory\AbstractFactory;
10
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
11
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
12
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
13
use Psr\Container\ContainerExceptionInterface;
14
use Psr\Container\ContainerInterface;
15
use Psr\Container\NotFoundExceptionInterface;
16
17
final class EntityManagerHelperFactory extends AbstractFactory
18
{
19
    use ObjectManagerArgvInputProviderTrait;
20
    use EntityManagerFactoryProviderTrait;
21
22
    /**
23
     * @param ContainerInterface $container
24
     * @param string $requestedName
25
     * @param array<string, mixed>|null $options
26
     *
27
     * @return EntityManagerHelper
28
     *
29
     * @throws ServiceNotCreatedException
30
     * @throws ServiceNotFoundException
31
     * @throws ContainerExceptionInterface
32
     * @throws NotFoundExceptionInterface
33
     */
34
    public function __invoke(
35
        ContainerInterface $container,
36
        string $requestedName,
37
        array $options = null
38
    ): EntityManagerHelper {
39
        $options = $options ?? $this->getServiceOptions($container, $requestedName);
40
41
        // Attempt to fetch the name of the entity manager from command line arguments
42
        if (empty($options['entity_manager'])) {
43
            $options['entity_manager'] = $this->getEntityManagerArgvInput();
44
        }
45
46
        if (!empty($options['default_object_manager'])) {
47
            $options['entity_manager'] = $options['default_object_manager'];
48
        }
49
50
        if (empty($options['entity_manager'])) {
51
            throw new ServiceNotCreatedException(
52
                sprintf(
53
                    'The required \'entity_manager\' configuration option is missing for service \'%s\'',
54
                    $requestedName
55
                )
56
            );
57
        }
58
59
        return new EntityManagerHelper(
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\ORM\Tools\Conso...per\EntityManagerHelper has been deprecated: This class will be removed in ORM 3.0 without replacement. ( Ignorable by Annotation )

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

59
        return /** @scrutinizer ignore-deprecated */ new EntityManagerHelper(
Loading history...
60
            $this->getEntityManager($container, $options['entity_manager'], $requestedName)
61
        );
62
    }
63
}
64