ObjectRegistry::configureManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the sauls/object-registry-bundle package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Bundle\ObjectRegistryBundle\Registry;
14
15
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Sauls\Bundle\ObjectRegistryBundle\Collection\ObjectManagerCollectionInterface;
18
use Sauls\Bundle\ObjectRegistryBundle\Manager\DoctrineEntityManager;
19
use Sauls\Bundle\ObjectRegistryBundle\Manager\ManagerInterface;
20
use Sauls\Bundle\ObjectRegistryBundle\Manager\ObjectManager;
21
use Sauls\Component\Collection\ArrayCollection;
22
23
class ObjectRegistry implements RegistryInterface
24
{
25
    /**
26
     * @var ObjectManagerCollectionInterface|ArrayCollection
27
     */
28
    private $objectManagerCollection;
29
    /**
30
     * @var ClassMetadataFactory
31
     */
32
    private $classMetadataFactory;
33
34 3
    public function __construct(
35
        ObjectManagerCollectionInterface $objectManagerCollection,
36
        EntityManagerInterface $entityManager
37
    ) {
38 3
        $this->objectManagerCollection = $objectManagerCollection;
39 3
        $this->classMetadataFactory = $entityManager->getMetadataFactory();
40 3
    }
41
42 3
    public function getManager(string $class): ManagerInterface
43
    {
44 3
        if ($this->hasConcreteManager($class)) {
45 1
            return $this->configureManager(
46 1
                $class,
47 1
                $this->objectManagerCollection->get($class)
48
            );
49
        }
50
51 2
        if ($this->isDoctrineObject($class)) {
52 1
            return $this->configureManager(
53 1
                $class,
54 1
                $this->objectManagerCollection->get(DoctrineEntityManager::DOCTRINE_OBJECT_MANAGER_NAME)
55
            );
56
        }
57
58 1
        return $this->configureManager(
59 1
            $class,
60 1
            $this->objectManagerCollection->get(ObjectManager::DEFAULT_OBJECT_MANAGER_NAME)
61
        );
62
    }
63
64 3
    public function hasConcreteManager(string $class): bool
65
    {
66 3
        return $this->objectManagerCollection->keyExists($class);
67
    }
68
69 3
    private function configureManager(string $class, ManagerInterface $manager): ManagerInterface
70
    {
71 3
        $objectManager = clone $manager;
72 3
        $objectManager->setObjectClass($class);
73 3
        return $objectManager;
74
    }
75
76 2
    public function isDoctrineObject(string $class): bool
77
    {
78 2
        return false === $this->classMetadataFactory->isTransient($class);
79
    }
80
}
81