AbstractMapper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 2
dl 0
loc 91
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
getEntityRepository() 0 1 ?
A __construct() 0 6 1
A findById() 0 4 1
A findBy() 0 4 1
A findOneBy() 0 6 1
A findAll() 0 4 1
A update() 0 4 1
A insert() 0 4 1
A persist() 0 13 3
A remove() 0 5 1
A removeAll() 0 7 2
1
<?php
2
3
namespace PlaygroundGame\Mapper;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\DBAL\DBALException;
7
use Zend\ServiceManager\ServiceLocatorInterface;
8
use PlaygroundGame\Options\ModuleOptions;
9
10
abstract class AbstractMapper
11
{
12
    abstract protected function getEntityRepository();
13
14
    /**
15
     * @var \Doctrine\ORM\EntityManager
16
     */
17
    protected $em;
18
19
    /**
20
     * @var \Doctrine\ORM\EntityRepository
21
     */
22
    protected $er;
23
    
24
    /**
25
     * @var ServiceLocatorInterface
26
     */
27
    protected $serviceLocator;
28
29
    /**
30
     * @var \PlaygroundGame\Options\ModuleOptions
31
     */
32
    protected $options;
33
34
    public function __construct(EntityManager $em, ModuleOptions $options, ServiceLocatorInterface $locator)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
35
    {
36
        $this->em      = $em;
37
        $this->options = $options;
38
        $this->serviceLocator = $locator;
39
    }
40
41
    public function findById($id)
42
    {
43
        return $this->getEntityRepository()->find($id);
44
    }
45
46
    public function findBy($filter, $order = null, $limit = null, $offset = null)
47
    {
48
        return $this->getEntityRepository()->findBy($filter, $order, $limit, $offset);
49
    }
50
51
    public function findOneBy($array = array(), $sortBy = array())
52
    {
53
        $er = $this->getEntityRepository();
54
55
        return $er->findOneBy($array, $sortBy);
56
    }
57
58
    public function findAll()
59
    {
60
        return $this->getEntityRepository()->findAll();
61
    }
62
63
    public function update($entity)
64
    {
65
        return $this->persist($entity);
66
    }
67
68
    public function insert($entity)
69
    {
70
        return $this->persist($entity);
71
    }
72
73
    protected function persist($entity)
74
    {
75
        try {
76
            $this->em->persist($entity);
77
            $this->em->flush();
78
        } catch (DBALException $e) {
79
            throw $e;
80
        } catch (\Exception $e) {
81
            throw $e;
82
        }
83
84
        return $entity;
85
    }
86
87
    public function remove($entity)
88
    {
89
        $this->em->remove($entity);
90
        $this->em->flush();
91
    }
92
93
    public function removeAll($entities)
94
    {
95
        foreach ($entities as $entity) {
96
            $this->em->remove($entity);
97
        }
98
        $this->em->flush();
99
    }
100
}
101