Completed
Push — master ( ccf562...798527 )
by greg
11:12 queued 10s
created

AbstractMapper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 25.25 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 2
dl 25
loc 99
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() 12 12 3
A persist() 13 13 3
A remove() 0 5 1
A removeAll() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    public function insert($entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        try {
71
            $entity = $this->persist($entity);
72
        } catch (DBALException $e) {
73
            throw $e;
74
        } catch (\Exception $e) {
75
            throw $e;
76
        }
77
78
        return $entity;
79
    }
80
81 View Code Duplication
    protected function persist($entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        try {
84
            $this->em->persist($entity);
85
            $this->em->flush();
86
        } catch (DBALException $e) {
87
            throw $e;
88
        } catch (\Exception $e) {
89
            throw $e;
90
        }
91
92
        return $entity;
93
    }
94
95
    public function remove($entity)
96
    {
97
        $this->em->remove($entity);
98
        $this->em->flush();
99
    }
100
101
    public function removeAll($entities)
102
    {
103
        foreach ($entities as $entity) {
104
            $this->em->remove($entity);
105
        }
106
        $this->em->flush();
107
    }
108
}
109