1 | <?php |
||
23 | trait SessionDomainTrait { |
||
24 | |||
25 | /** |
||
26 | */ |
||
27 | protected $pool; |
||
28 | |||
29 | /** |
||
30 | * Creates a new Session with the provided data |
||
31 | * |
||
32 | * @param mixed $data |
||
33 | * @return PayloadInterface |
||
34 | */ |
||
35 | public function create($data) { |
||
36 | // hydrate |
||
37 | $serializer = Session::getSerializer(); |
||
38 | $model = $serializer->hydrate(new Session(), $data); |
||
39 | $this->hydrateRelationships($model, $data); |
||
|
|||
40 | |||
41 | // dispatch pre save hooks |
||
42 | $this->dispatch(SessionEvent::PRE_CREATE, $model, $data); |
||
43 | $this->dispatch(SessionEvent::PRE_SAVE, $model, $data); |
||
44 | |||
45 | // validate |
||
46 | $validator = $this->getValidator(); |
||
47 | if ($validator !== null && !$validator->validate($model)) { |
||
48 | return new NotValid([ |
||
49 | 'errors' => $validator->getValidationFailures() |
||
50 | ]); |
||
51 | } |
||
52 | |||
53 | // save and dispatch post save hooks |
||
54 | $model->save(); |
||
55 | $this->dispatch(SessionEvent::POST_CREATE, $model, $data); |
||
56 | $this->dispatch(SessionEvent::POST_SAVE, $model, $data); |
||
57 | |||
58 | return new Created(['model' => $model]); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Deletes a Session with the given id |
||
63 | * |
||
64 | * @param mixed $id |
||
65 | * @return PayloadInterface |
||
66 | */ |
||
67 | public function delete($id) { |
||
68 | // find |
||
69 | $model = $this->get($id); |
||
70 | |||
71 | if ($model === null) { |
||
72 | return new NotFound(['message' => 'Session not found.']); |
||
73 | } |
||
74 | |||
75 | // delete |
||
76 | $this->dispatch(SessionEvent::PRE_DELETE, $model); |
||
77 | $model->delete(); |
||
78 | |||
79 | if ($model->isDeleted()) { |
||
80 | $this->dispatch(SessionEvent::POST_DELETE, $model); |
||
81 | return new Deleted(['model' => $model]); |
||
82 | } |
||
83 | |||
84 | return new NotDeleted(['message' => 'Could not delete Session']); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Returns a paginated result |
||
89 | * |
||
90 | * @param Parameters $params |
||
91 | * @return PayloadInterface |
||
92 | */ |
||
93 | public function paginate(Parameters $params) { |
||
120 | |||
121 | /** |
||
122 | * Returns one Session with the given id |
||
123 | * |
||
124 | * @param mixed $id |
||
125 | * @return PayloadInterface |
||
126 | */ |
||
127 | public function read($id) { |
||
138 | |||
139 | /** |
||
140 | * Sets the User id |
||
141 | * |
||
142 | * @param mixed $id |
||
143 | * @param mixed $relatedId |
||
144 | * @return PayloadInterface |
||
145 | */ |
||
146 | public function setUserId($id, $relatedId) { |
||
167 | |||
168 | /** |
||
169 | * Updates a Session with the given idand the provided data |
||
170 | * |
||
171 | * @param mixed $id |
||
172 | * @param mixed $data |
||
173 | * @return PayloadInterface |
||
174 | */ |
||
175 | public function update($id, $data) { |
||
213 | |||
214 | /** |
||
215 | * @param mixed $query |
||
216 | * @param mixed $filter |
||
217 | * @return void |
||
218 | */ |
||
219 | protected function applyFilter($query, $filter) { |
||
239 | |||
240 | /** |
||
241 | * @param string $type |
||
242 | * @param Session $model |
||
243 | * @param array $data |
||
244 | */ |
||
245 | protected function dispatch($type, Session $model, array $data = []) { |
||
267 | |||
268 | /** |
||
269 | * Internal mechanism to set the User id |
||
270 | * |
||
271 | * @param Session $model |
||
272 | * @param mixed $relatedId |
||
273 | */ |
||
274 | protected function doSetUserId(Session $model, $relatedId) { |
||
283 | |||
284 | /** |
||
285 | * Returns one Session with the given id from cache |
||
286 | * |
||
287 | * @param mixed $id |
||
288 | * @return Session|null |
||
289 | */ |
||
290 | protected function get($id) { |
||
302 | |||
303 | /** |
||
304 | * Returns the service container |
||
305 | * |
||
306 | * @return ServiceContainer |
||
307 | */ |
||
308 | abstract protected function getServiceContainer(); |
||
309 | } |
||
310 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.