Completed
Push — develop ( 33b7cf...e4b4af )
by greg
03:08
created

AbstractMapper::getServiceLocator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace PlaygroundCore\Mapper;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\DBAL\DBALException;
7
use Zend\ServiceManager\ServiceLocatorInterface;
8
use PlaygroundCore\Options\ModuleOptions;
9
10
abstract class AbstractMapper
11
{
12
13
    abstract protected function getEntityRepository();
14
15
    /**
16
     * @var \Doctrine\ORM\EntityManager
17
     */
18
    protected $em;
19
20
    /**
21
     * @var \Doctrine\ORM\EntityRepository
22
     */
23
    protected $er;
24
    
25
    /**
26
     * @var ServiceLocatorInterface
27
     */
28
    protected $serviceLocator;
29
30
    /**
31
     * @var \PlaygroundGame\Options\ModuleOptions
32
     */
33
    protected $options;
34
35
    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...
36
    {
37
        $this->em      = $em;
38
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options of type object<PlaygroundCore\Options\ModuleOptions> is incompatible with the declared type object<PlaygroundGame\Options\ModuleOptions> of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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