Completed
Push — master ( 126192...4f5242 )
by Derek Stephen
03:27
created

Person   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.94%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 15
c 7
b 0
f 0
lcom 1
cbo 3
dl 0
loc 82
ccs 31
cts 33
cp 0.9394
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C createFromArray() 0 13 9
A toArray() 0 14 1
A savePerson() 0 4 1
A deletePerson() 0 4 1
A findByCriteria() 0 4 1
A getRepository() 0 4 1
1
<?php
2
3
namespace Del\Service;
4
5
use Del\Criteria\PersonCriteria;
6
use Del\Entity\Person as PersonEntity;
7
use Del\Repository\Person as PersonRepository;
8
use Doctrine\ORM\EntityManager;
9
use Pimple\Container;
10
11
class Person
12
{
13
    /** @var EntityManager $em */
14
    protected $em;
15
16 3
    public function __construct(EntityManager $em)
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...
17
    {
18 3
        $this->em = $em;
19 3
    }
20
21
   /** 
22
    * @param array $data
23
    * @return PersonEntity
24
    */
25 3
    public function createFromArray(array $data)
26
    {
27 3
        $person = new PersonEntity();
28 3
        isset($data['id']) ? $person->setId($data['id']) : null;
29 3
        isset($data['firstname']) ? $person->setFirstname($data['firstname']) : null;
30 3
        isset($data['middlename']) ? $person->setMiddlename($data['middlename']) : null;
31 3
        isset($data['lastname']) ? $person->setLastname($data['lastname']) : null;
32 3
        isset($data['aka']) ? $person->setAka($data['aka']) : null;
33 3
        isset($data['dob']) ? $person->setDob($data['dob']) : null;
34 3
        isset($data['birthplace']) ? $person->setBirthplace($data['birthplace']) : null;
35 3
        isset($data['country']) ? $person->setCountry($data['country']) : null;
36 3
        return $person;
37
    }
38
39
    /**
40
     * @param array $person
41
     * @return PersonEntity
42
     */
43 1
    public function toArray(PersonEntity $person)
44
    {
45
        $data = [
46 1
            'id' => $person->getId(),
47 1
            'firstname' => $person->getFirstname(),
48 1
            'middlename' => $person->getMiddlename(),
49 1
            'lastname' => $person->getLastname(),
50 1
            'aka' => $person->getAka(),
51 1
            'dob' => $person->getDob(),
52 1
            'birthplace' => $person->getBirthplace(),
53 1
            'country' => $person->getCountry(),
54 1
        ];
55 1
        return $data;
56
    }
57
58
    /**
59
     * @param PersonEntity $person
60
     * @return PersonEntity
61
     */
62 1
    public function savePerson(PersonEntity $person)
63
    {
64 1
        return $this->getRepository()->save($person);
65
    }
66
67
    /**
68
     * @param PersonEntity $person
69
     * @return PersonEntity
70
     */
71 1
    public function deletePerson(PersonEntity $person)
72
    {
73 1
        return $this->getRepository()->delete($person);
74
    }
75
76
    /**
77
     * @param PersonCriteria $criteria
78
     * @return array
79
     */
80
    public function findByCriteria(PersonCriteria $criteria)
81
    {
82
        return $this->getRepository()->findByCriteria($criteria);
83
    }
84
85
   /**
86
    * @return PersonRepository
87
    */
88 1
    protected function getRepository()
89
    {
90 1
        return $this->em->getRepository('Del\Entity\Person');
91
    }
92
}
93