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) |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.