Completed
Push — master ( 03f81b...0833b6 )
by Derek Stephen
03:44 queued 10s
created

PersonService::createFromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
33 4
        return $this->populateFromArray($person, $data);
34
    }
35
36
    /**
37
     * @param Person $person
38
     * @param array $data
39
     * @return Person
40
     */
41 4
    public function populateFromArray(Person $person, array $data): Person
42
    {
43 4
        isset($data['id']) ? $person->setId($data['id']) : null;
44 4
        isset($data['firstname']) ? $person->setFirstname($data['firstname']) : null;
45 4
        isset($data['middlename']) ? $person->setMiddlename($data['middlename']) : null;
46 4
        isset($data['lastname']) ? $person->setLastname($data['lastname']) : null;
47 4
        isset($data['aka']) ? $person->setAka($data['aka']) : null;
48 4
        isset($data['dob']) ? $person->setDob($data['dob']) : null;
49 4
        isset($data['birthplace']) ? $person->setBirthplace($data['birthplace']) : null;
50 4
        isset($data['country']) ? $person->setCountry($data['country']) : null;
51 4
        isset($data['image']) ? $person->setImage($data['image']) : null;
52
53 4
        return $person;
54
    }
55
56
    /**
57
     * @param Person $person
58
     * @return array
59
     */
60 1
    public function toArray(Person $person): array
61
    {
62
        $data = [
63 1
            'id' => $person->getId(),
64 1
            'firstname' => $person->getFirstname(),
65 1
            'middlename' => $person->getMiddlename(),
66 1
            'lastname' => $person->getLastname(),
67 1
            'aka' => $person->getAka(),
68 1
            'dob' => $person->getDob(),
69 1
            'birthplace' => $person->getBirthplace(),
70 1
            'country' => $person->getCountry(),
71
        ];
72 1
        return $data;
73
    }
74
75
    /**
76
     * @param Person $person
77
     * @return Person
78
     */
79 2
    public function savePerson(Person $person): Person
80
    {
81 2
        return $this->getRepository()->save($person);
82
    }
83
84
    /**
85
     * @param Person $person
86
     */
87 2
    public function deletePerson(Person $person): void
88
    {
89 2
        $this->getRepository()->delete($person);
90 2
    }
91
92
    /**
93
     * @param PersonCriteria $criteria
94
     * @return Person[]
95
     */
96 1
    public function findByCriteria(PersonCriteria $criteria)
97
    {
98 1
        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...
99
    }
100
101
    /**
102
     * @param PersonCriteria $criteria
103
     * @return Person|null
104
     */
105 1
    public function findOneByCriteria(PersonCriteria $criteria)
106
    {
107 1
        $results = $this->findByCriteria($criteria);
108 1
        return (isset($results[0])) ? $results[0] : null;
109
    }
110
111
    /**
112
     * @return \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
113
     */
114 2
    protected function getRepository()
115
    {
116 2
        return $this->em->getRepository(Person::class);
117
    }
118
}
119