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
|
12 |
|
public function countAll() : int |
26
|
|
|
{ |
27
|
12 |
|
return $this->container->get('doctrine') |
28
|
12 |
|
->getManager('slave') |
29
|
12 |
|
->getRepository(Entry::class) |
30
|
12 |
|
->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
|
1 |
|
public function getList(?int $limit, ?int $offset) : array |
42
|
|
|
{ |
43
|
1 |
|
return $this->container->get('doctrine') |
44
|
1 |
|
->getManager('slave') |
45
|
1 |
|
->getRepository(Entry::class) |
46
|
1 |
|
->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
|
7 |
|
public function findById(string $id) : Entry |
63
|
|
|
{ |
64
|
7 |
|
$entry = $this->container->get('doctrine') |
65
|
7 |
|
->getManager('master') |
66
|
7 |
|
->getRepository(Entry::class) |
67
|
7 |
|
->find($id); |
68
|
|
|
|
69
|
7 |
|
if (! ($entry instanceof Entry)) { |
70
|
2 |
|
throw new EntityNotFoundException(); |
71
|
|
|
} |
72
|
|
|
|
73
|
7 |
|
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
|
12 |
|
public function create(array $data) : Entry |
88
|
|
|
{ |
89
|
12 |
|
$doctrine = $this->container->get('doctrine'); |
90
|
12 |
|
$validator = $this->container->get('validator'); |
91
|
|
|
|
92
|
|
|
// inits a new entry and maps the given data to it... |
93
|
12 |
|
$entry = $doctrine->getHydrator()->hydrate(Entry::class, $data); |
94
|
|
|
// validates the inited entry... |
95
|
12 |
|
InvalidEntityException::assert($entry, $validator); |
96
|
|
|
|
97
|
10 |
|
$manager = $doctrine->getManager('master'); |
98
|
10 |
|
$manager->persist($entry); |
99
|
10 |
|
$manager->flush(); |
100
|
|
|
|
101
|
10 |
|
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
|
4 |
|
public function update(Entry $entry, array $data) : void |
117
|
|
|
{ |
118
|
4 |
|
$doctrine = $this->container->get('doctrine'); |
119
|
4 |
|
$validator = $this->container->get('validator'); |
120
|
|
|
|
121
|
|
|
// to avoid serious problems re-reads the given entry from the master... |
122
|
4 |
|
$entry = $this->findById((string) $entry->getId()); |
123
|
|
|
// maps the given data to the given entry... |
124
|
4 |
|
$doctrine->getHydrator()->hydrate($entry, $data); |
125
|
|
|
// validates the given entry... |
126
|
4 |
|
InvalidEntityException::assert($entry, $validator); |
127
|
|
|
|
128
|
1 |
|
$manager = $doctrine->getManager('master'); |
129
|
1 |
|
$manager->flush(); |
130
|
1 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Deletes the given entry |
134
|
|
|
* |
135
|
|
|
* @param Entry $entry |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
1 |
|
public function delete(Entry $entry) : void |
140
|
|
|
{ |
141
|
1 |
|
$doctrine = $this->container->get('doctrine'); |
142
|
|
|
|
143
|
|
|
// to avoid serious problems re-reads the given entry from the master... |
144
|
1 |
|
$entry = $this->findById((string) $entry->getId()); |
145
|
|
|
|
146
|
1 |
|
$manager = $doctrine->getManager('master'); |
147
|
1 |
|
$manager->remove($entry); |
148
|
1 |
|
$manager->flush(); |
149
|
1 |
|
} |
150
|
|
|
} |
151
|
|
|
|