Completed
Push — master ( a5514d...03f81b )
by Derek Stephen
01:53
created

PersonService   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 96
ccs 28
cts 36
cp 0.7778
rs 10
c 0
b 0
f 0

8 Methods

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

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
87
    }
88
89
    /**
90
     * @param PersonCriteria $criteria
91
     * @return Person|null
92
     */
93
    public function findOneByCriteria(PersonCriteria $criteria)
94
    {
95
        $results = $this->findByCriteria($criteria);
96
        return (isset($results[0])) ? $results[0] : null;
97
    }
98
99
    /**
100
     * @return \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
101
     */
102 2
    protected function getRepository()
103
    {
104 2
        return $this->em->getRepository(Person::class);
105
    }
106
}
107