1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\domain\base; |
3
|
|
|
|
4
|
|
|
use keeko\core\model\Activity; |
5
|
|
|
use keeko\core\model\ActivityQuery; |
6
|
|
|
use keeko\framework\service\ServiceContainer; |
7
|
|
|
use keeko\framework\domain\payload\PayloadInterface; |
8
|
|
|
use phootwork\collection\Map; |
9
|
|
|
use keeko\framework\domain\payload\Found; |
10
|
|
|
use keeko\framework\domain\payload\NotFound; |
11
|
|
|
use Tobscure\JsonApi\Parameters; |
12
|
|
|
use keeko\framework\utils\NameUtils; |
13
|
|
|
use keeko\framework\domain\payload\Created; |
14
|
|
|
use keeko\framework\domain\payload\Updated; |
15
|
|
|
use keeko\framework\domain\payload\NotUpdated; |
16
|
|
|
use keeko\framework\domain\payload\NotValid; |
17
|
|
|
use keeko\framework\domain\payload\Deleted; |
18
|
|
|
use keeko\framework\domain\payload\NotDeleted; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
*/ |
22
|
|
|
trait ActivityDomainTrait { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
*/ |
26
|
|
|
protected $pool; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Creates a new Activity with the provided data |
30
|
|
|
* |
31
|
|
|
* @param mixed $data |
32
|
|
|
* @return PayloadInterface |
33
|
|
|
*/ |
34
|
|
|
public function create($data) { |
35
|
|
|
// hydrate |
36
|
|
|
$serializer = Activity::getSerializer(); |
37
|
|
|
$activity = $serializer->hydrate(new Activity(), $data); |
38
|
|
|
|
39
|
|
|
// validate |
40
|
|
|
if (!$activity->validate()) { |
41
|
|
|
return new NotValid([ |
42
|
|
|
'errors' => $activity->getValidationFailures() |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$activity->save(); |
47
|
|
|
return new Created(['model' => $activity]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Deletes a Activity with the given id |
52
|
|
|
* |
53
|
|
|
* @param mixed $id |
54
|
|
|
* @return PayloadInterface |
55
|
|
|
*/ |
56
|
|
|
public function delete($id) { |
57
|
|
|
// find |
58
|
|
|
$activity = $this->get($id); |
59
|
|
|
|
60
|
|
|
if ($activity === null) { |
61
|
|
|
return new NotFound(['message' => 'Activity not found.']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// delete |
65
|
|
|
$activity->delete(); |
66
|
|
|
|
67
|
|
|
if ($activity->isDeleted()) { |
68
|
|
|
return new Deleted(['model' => $activity]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return new NotDeleted(['message' => 'Could not delete Activity']); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns a paginated result |
76
|
|
|
* |
77
|
|
|
* @param Parameters $params |
78
|
|
|
* @return PayloadInterface |
79
|
|
|
*/ |
80
|
|
|
public function paginate(Parameters $params) { |
81
|
|
|
$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences(); |
82
|
|
|
$defaultSize = $sysPrefs->getPaginationSize(); |
83
|
|
|
$page = $params->getPage('number'); |
|
|
|
|
84
|
|
|
$size = $params->getPage('size', $defaultSize); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$query = ActivityQuery::create(); |
87
|
|
|
|
88
|
|
|
// sorting |
89
|
|
|
$sort = $params->getSort(Activity::getSerializer()->getSortFields()); |
90
|
|
|
foreach ($sort as $field => $order) { |
91
|
|
|
$method = 'orderBy' . NameUtils::toStudlyCase($field); |
92
|
|
|
$query->$method($order); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// filtering |
96
|
|
|
$filter = $params->getFilter(); |
97
|
|
|
if (!empty($filter)) { |
98
|
|
|
$this->applyFilter($query, $filter); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// paginate |
102
|
|
|
$activity = $query->paginate($page, $size); |
103
|
|
|
|
104
|
|
|
// run response |
105
|
|
|
return new Found(['model' => $activity]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns one Activity with the given id |
110
|
|
|
* |
111
|
|
|
* @param mixed $id |
112
|
|
|
* @return PayloadInterface |
113
|
|
|
*/ |
114
|
|
|
public function read($id) { |
115
|
|
|
// read |
116
|
|
|
$activity = $this->get($id); |
117
|
|
|
|
118
|
|
|
// check existence |
119
|
|
|
if ($activity === null) { |
120
|
|
|
return new NotFound(['message' => 'Activity not found.']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return new Found(['model' => $activity]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Sets the User id |
128
|
|
|
* |
129
|
|
|
* @param mixed $id |
130
|
|
|
* @param mixed $actorId |
131
|
|
|
* @return PayloadInterface |
132
|
|
|
*/ |
133
|
|
|
public function setActorId($id, $actorId) { |
134
|
|
|
// find |
135
|
|
|
$activity = $this->get($id); |
136
|
|
|
|
137
|
|
|
if ($activity === null) { |
138
|
|
|
return new NotFound(['message' => 'Activity not found.']); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// update |
142
|
|
|
if ($activity->getActorId() !== $actorId) { |
143
|
|
|
$activity->setActorId($actorId); |
144
|
|
|
$activity->save(); |
145
|
|
|
return Updated(['model' => $activity]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return NotUpdated(['model' => $activity]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Sets the ActivityObject id |
153
|
|
|
* |
154
|
|
|
* @param mixed $id |
155
|
|
|
* @param mixed $targetId |
156
|
|
|
* @return PayloadInterface |
157
|
|
|
*/ |
158
|
|
|
public function setTargetId($id, $targetId) { |
159
|
|
|
// find |
160
|
|
|
$activity = $this->get($id); |
161
|
|
|
|
162
|
|
|
if ($activity === null) { |
163
|
|
|
return new NotFound(['message' => 'Activity not found.']); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// update |
167
|
|
|
if ($activity->getTargetId() !== $targetId) { |
168
|
|
|
$activity->setTargetId($targetId); |
169
|
|
|
$activity->save(); |
170
|
|
|
return Updated(['model' => $activity]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return NotUpdated(['model' => $activity]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Updates a Activity with the given idand the provided data |
178
|
|
|
* |
179
|
|
|
* @param mixed $id |
180
|
|
|
* @param mixed $data |
181
|
|
|
* @return PayloadInterface |
182
|
|
|
*/ |
183
|
|
|
public function update($id, $data) { |
184
|
|
|
// find |
185
|
|
|
$activity = $this->get($id); |
186
|
|
|
|
187
|
|
|
if ($activity === null) { |
188
|
|
|
return new NotFound(['message' => 'Activity not found.']); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// hydrate |
192
|
|
|
$serializer = Activity::getSerializer(); |
193
|
|
|
$activity = $serializer->hydrate($activity, $data); |
194
|
|
|
|
195
|
|
|
// validate |
196
|
|
|
if (!$activity->validate()) { |
197
|
|
|
return new NotValid([ |
198
|
|
|
'errors' => $activity->getValidationFailures() |
199
|
|
|
]); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$rows = $activity->save(); |
203
|
|
|
$payload = ['model' => $activity]; |
204
|
|
|
|
205
|
|
|
if ($rows === 0) { |
206
|
|
|
return new NotUpdated($payload); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return new Updated($payload); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Implement this functionality at keeko\core\domain\ActivityDomain |
214
|
|
|
* |
215
|
|
|
* @param ActivityQuery $query |
216
|
|
|
* @param mixed $filter |
217
|
|
|
*/ |
218
|
|
|
abstract protected function applyFilter(ActivityQuery $query, $filter); |
|
|
|
|
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Returns one Activity with the given id from cache |
222
|
|
|
* |
223
|
|
|
* @param mixed $id |
224
|
|
|
* @return Activity|null |
225
|
|
|
*/ |
226
|
|
|
protected function get($id) { |
227
|
|
|
if ($this->pool === null) { |
228
|
|
|
$this->pool = new Map(); |
229
|
|
|
} else if ($this->pool->has($id)) { |
230
|
|
|
return $this->pool->get($id); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$activity = ActivityQuery::create()->findOneById($id); |
234
|
|
|
$this->pool->set($id, $activity); |
235
|
|
|
|
236
|
|
|
return $activity; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Returns the service container |
241
|
|
|
* |
242
|
|
|
* @return ServiceContainer |
243
|
|
|
*/ |
244
|
|
|
abstract protected function getServiceContainer(); |
245
|
|
|
} |
246
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.