|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Bundle\Example\Service; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Import classes |
|
7
|
|
|
*/ |
|
8
|
|
|
use App\ContainerAwareTrait; |
|
9
|
|
|
use App\Exception\EntityNotFoundException; |
|
10
|
|
|
use App\Exception\InvalidEntityException; |
|
11
|
|
|
use App\Bundle\Example\Entity\Entry; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* EntryManager |
|
15
|
|
|
*/ |
|
16
|
|
|
final class EntryManager |
|
17
|
|
|
{ |
|
18
|
|
|
use ContainerAwareTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Counts all entries |
|
22
|
|
|
* |
|
23
|
|
|
* @return int |
|
24
|
|
|
*/ |
|
25
|
|
|
public function countAll() : int |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->container->get('doctrine') |
|
28
|
|
|
->getManager('slave') |
|
29
|
|
|
->getRepository(Entry::class) |
|
30
|
|
|
->countAll(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Gets the list of entries |
|
35
|
|
|
* |
|
36
|
|
|
* @param null|int $limit |
|
37
|
|
|
* @param null|int $offset |
|
38
|
|
|
* |
|
39
|
|
|
* @return Entry[] |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getList(?int $limit, ?int $offset) : array |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->container->get('doctrine') |
|
44
|
|
|
->getManager('slave') |
|
45
|
|
|
->getRepository(Entry::class) |
|
46
|
|
|
->getList($limit, $offset); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Finds an entry by the given ID |
|
51
|
|
|
* |
|
52
|
|
|
* Please note that an entry will be read from the master. |
|
53
|
|
|
* |
|
54
|
|
|
* Throws an exception if an entry wasn't found. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $id |
|
57
|
|
|
* |
|
58
|
|
|
* @return Entry |
|
59
|
|
|
* |
|
60
|
|
|
* @throws EntityNotFoundException |
|
61
|
|
|
*/ |
|
62
|
|
|
public function findById(string $id) : Entry |
|
63
|
|
|
{ |
|
64
|
|
|
$entry = $this->container->get('doctrine') |
|
65
|
|
|
->getManager('master') |
|
66
|
|
|
->getRepository(Entry::class) |
|
67
|
|
|
->find($id); |
|
68
|
|
|
|
|
69
|
|
|
if (! ($entry instanceof Entry)) { |
|
70
|
|
|
throw new EntityNotFoundException(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $entry; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Creates a new entry from the given data |
|
78
|
|
|
* |
|
79
|
|
|
* Throws an exception if the given data isn't valid. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $data |
|
82
|
|
|
* |
|
83
|
|
|
* @return Entry |
|
84
|
|
|
* |
|
85
|
|
|
* @throws InvalidEntityException |
|
86
|
|
|
*/ |
|
87
|
|
|
public function create(array $data) : Entry |
|
88
|
|
|
{ |
|
89
|
|
|
$doctrine = $this->container->get('doctrine'); |
|
90
|
|
|
$validator = $this->container->get('validator'); |
|
91
|
|
|
|
|
92
|
|
|
// inits a new entry and maps the given data to it... |
|
93
|
|
|
$entry = $doctrine->getHydrator()->hydrate(Entry::class, $data); |
|
94
|
|
|
// validates the inited entry... |
|
95
|
|
|
InvalidEntityException::assert($entry, $validator); |
|
96
|
|
|
|
|
97
|
|
|
$manager = $doctrine->getManager('master'); |
|
98
|
|
|
$manager->persist($entry); |
|
99
|
|
|
$manager->flush(); |
|
100
|
|
|
|
|
101
|
|
|
return $entry; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Updates the given entry from the given data |
|
106
|
|
|
* |
|
107
|
|
|
* Throws an exception if the given data isn't valid. |
|
108
|
|
|
* |
|
109
|
|
|
* @param Entry $entry |
|
110
|
|
|
* @param array $data |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
* |
|
114
|
|
|
* @throws InvalidEntityException |
|
115
|
|
|
*/ |
|
116
|
|
|
public function update(Entry $entry, array $data) : void |
|
117
|
|
|
{ |
|
118
|
|
|
$doctrine = $this->container->get('doctrine'); |
|
119
|
|
|
$validator = $this->container->get('validator'); |
|
120
|
|
|
|
|
121
|
|
|
// to avoid serious problems re-reads the given entry from the master... |
|
122
|
|
|
$entry = $this->findById((string) $entry->getId()); |
|
123
|
|
|
// maps the given data to the given entry... |
|
124
|
|
|
$doctrine->getHydrator()->hydrate($entry, $data); |
|
125
|
|
|
// validates the given entry... |
|
126
|
|
|
InvalidEntityException::assert($entry, $validator); |
|
127
|
|
|
|
|
128
|
|
|
$manager = $doctrine->getManager('master'); |
|
129
|
|
|
$manager->flush(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Deletes the given entry |
|
134
|
|
|
* |
|
135
|
|
|
* @param Entry $entry |
|
136
|
|
|
* |
|
137
|
|
|
* @return void |
|
138
|
|
|
*/ |
|
139
|
|
|
public function delete(Entry $entry) : void |
|
140
|
|
|
{ |
|
141
|
|
|
$doctrine = $this->container->get('doctrine'); |
|
142
|
|
|
|
|
143
|
|
|
// to avoid serious problems re-reads the given entry from the master... |
|
144
|
|
|
$entry = $this->findById((string) $entry->getId()); |
|
145
|
|
|
|
|
146
|
|
|
$manager = $doctrine->getManager('master'); |
|
147
|
|
|
$manager->remove($entry); |
|
148
|
|
|
$manager->flush(); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|