Passed
Push — master ( 79bf3c...52a24b )
by Alex
01:04 queued 13s
created

EntityManagerFactoryProviderTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 22
c 1
b 0
f 0
dl 0
loc 51
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getEntityManager() 0 38 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Factory\Service\EntityManager;
6
7
use Arp\LaminasDoctrine\Service\EntityManager\EntityManagerProvider;
8
use Arp\LaminasDoctrine\Service\EntityManager\EntityManagerProviderInterface;
9
use Arp\LaminasDoctrine\Service\EntityManager\Exception\EntityManagerProviderException;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
12
use Psr\Container\ContainerExceptionInterface;
13
use Psr\Container\ContainerInterface;
14
use Psr\Container\NotFoundExceptionInterface;
15
16
/**
17
 * @author  Alex Patterson <[email protected]>
18
 * @package Arp\LaminasDoctrine\Factory\Service
19
 */
20
trait EntityManagerFactoryProviderTrait
21
{
22
    /**
23
     * @param ContainerInterface                  $container
24
     * @param string|EntityManagerInterface|mixed $name
25
     * @param string                              $serviceName
26
     *
27
     * @return EntityManagerInterface
28
     *
29
     * @throws ServiceNotCreatedException
30
     * @throws ContainerExceptionInterface
31
     * @throws NotFoundExceptionInterface
32
     */
33
    protected function getEntityManager(
34
        ContainerInterface $container,
35
        $name,
36
        string $serviceName
37
    ): EntityManagerInterface {
38
        $entityManager = $name;
39
40
        if (is_string($name)) {
41
            /** @var EntityManagerProviderInterface $entityManagerProvider */
42
            $entityManagerProvider = $container->get(EntityManagerProvider::class);
43
44
            try {
45
                $entityManager = $entityManagerProvider->getEntityManager($name);
46
            } catch (EntityManagerProviderException $e) {
47
                throw new ServiceNotCreatedException(
48
                    sprintf(
49
                        'The entity manager \'%s\' could not be found for service \'%s\'',
50
                        $name,
51
                        $serviceName
52
                    ),
53
                    $e->getCode(),
54
                    $e
55
                );
56
            }
57
        }
58
59
        if (!$entityManager instanceof EntityManagerInterface) {
60
            throw new ServiceNotCreatedException(
61
                sprintf(
62
                    'The entity manager must be an object of type \'%s\'; \'%s\' provided for service \'%s\'',
63
                    EntityManagerInterface::class,
64
                    is_object($entityManager) ? get_class($entityManager) : gettype($entityManager),
65
                    $serviceName
66
                )
67
            );
68
        }
69
70
        return $entityManager;
71
    }
72
}
73