ObjectManagerAwareTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 36
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getObjectManager() 0 6 1
A getEnityClass() 0 6 1
A getRepository() 0 6 1
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/doctrine
4
 * @year    2016
5
 * @author  Lobanov Aleksandr <[email protected]>
6
 */
7
8
namespace Nnx\Doctrine\Traits\Factory;
9
10
use Doctrine\Common\Persistence\ObjectManager;
11
use Doctrine\Common\Persistence\ObjectRepository;
12
use Nnx\Doctrine\EntityManager\EntityManagerInterface;
13
use Nnx\Doctrine\ObjectManager\ObjectManagerAutoDetectorInterface;
14
15
/**
16
 * Class ObjectManagerAwareTrait
17
 * @package Nnx\Doctrine\Traits\Factory
18
 */
19
trait ObjectManagerAwareTrait
20
{
21
    use ServiceManagerAwareTrait;
22
23
    /**
24
     * @return ObjectManager
25
     */
26
    protected function getObjectManager($class)
27
    {
28
        /** @var ObjectManagerAutoDetectorInterface $serviceAutoDetector */
29
        $serviceAutoDetector = $this->getServiceManager()->get(ObjectManagerAutoDetectorInterface::class);
30
        return $serviceAutoDetector->getObjectManagerByClassName($class);
31
    }
32
33
    /**
34
     * @param string $interface
35
     * @return string
36
     */
37
    protected function getEnityClass($interface)
38
    {
39
        /** @var EntityManagerInterface $em */
40
        $em = $this->getServiceManager()->get(EntityManagerInterface::class);
41
        return $em->getEntityClassByInterface($interface);
42
    }
43
44
    /**
45
     * @param string $interface
46
     * @return ObjectRepository
47
     */
48
    protected function getRepository($interface)
49
    {
50
        $entityManager = $this->getObjectManager($interface);
51
        $class = $this->getEnityClass($interface);
52
        return $entityManager->getRepository($class);
53
    }
54
}
55