Completed
Push — master ( abae3b...04c3fa )
by Thomas
05:59
created

ActivityDomainTrait::setActorId()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 15
nc 3
nop 2
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 keeko\framework\utils\Parameters;
12
use keeko\framework\utils\NameUtils;
13
use keeko\core\event\ActivityEvent;
14
use keeko\framework\domain\payload\Created;
15
use keeko\framework\domain\payload\Updated;
16
use keeko\framework\domain\payload\NotUpdated;
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
		// dispatch
40
		$event = new ActivityEvent($activity);
41
		$dispatcher = $this->getServiceContainer()->getDispatcher();
42
		$dispatcher->dispatch(ActivityEvent::PRE_CREATE, $event);
43
		$dispatcher->dispatch(ActivityEvent::PRE_SAVE, $event);
44
		$activity->save();
45
		$dispatcher->dispatch(ActivityEvent::POST_CREATE, $event);
46
		$dispatcher->dispatch(ActivityEvent::POST_SAVE, $event);
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
		$event = new ActivityEvent($activity);
66
		$dispatcher = $this->getServiceContainer()->getDispatcher();
67
		$dispatcher->dispatch(ActivityEvent::PRE_DELETE, $event);
68
		$activity->delete();
69
70
		if ($activity->isDeleted()) {
71
			$dispatcher->dispatch(ActivityEvent::POST_DELETE, $event);
72
			return new Deleted(['model' => $activity]);
73
		}
74
75
		return new NotDeleted(['message' => 'Could not delete Activity']);
76
	}
77
78
	/**
79
	 * Returns a paginated result
80
	 * 
81
	 * @param Parameters $params
82
	 * @return PayloadInterface
83
	 */
84
	public function paginate(Parameters $params) {
85
		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
86
		$defaultSize = $sysPrefs->getPaginationSize();
87
		$page = $params->getPage('number');
88
		$size = $params->getPage('size', $defaultSize);
89
90
		$query = ActivityQuery::create();
91
92
		// sorting
93
		$sort = $params->getSort(Activity::getSerializer()->getSortFields());
94
		foreach ($sort as $field => $order) {
95
			$method = 'orderBy' . NameUtils::toStudlyCase($field);
96
			$query->$method($order);
97
		}
98
99
		// filtering
100
		$filter = $params->getFilter();
101
		if (!empty($filter)) {
102
			$this->applyFilter($query, $filter);
103
		}
104
105
		// paginate
106
		$activity = $query->paginate($page, $size);
107
108
		// run response
109
		return new Found(['model' => $activity]);
110
	}
111
112
	/**
113
	 * Returns one Activity with the given id
114
	 * 
115
	 * @param mixed $id
116
	 * @return PayloadInterface
117
	 */
118
	public function read($id) {
119
		// read
120
		$activity = $this->get($id);
121
122
		// check existence
123
		if ($activity === null) {
124
			return new NotFound(['message' => 'Activity not found.']);
125
		}
126
127
		return new Found(['model' => $activity]);
128
	}
129
130
	/**
131
	 * Sets the User id
132
	 * 
133
	 * @param mixed $id
134
	 * @param mixed $actorId
135
	 * @return PayloadInterface
136
	 */
137
	public function setActorId($id, $actorId) {
138
		// find
139
		$activity = $this->get($id);
140
141
		if ($activity === null) {
142
			return new NotFound(['message' => 'Activity not found.']);
143
		}
144
145
		// update
146
		if ($activity->getActorId() !== $actorId) {
147
			$activity->setActorId($actorId);
148
149
			$event = new ActivityEvent($activity);
150
			$dispatcher = $this->getServiceContainer()->getDispatcher();
151
			$dispatcher->dispatch(ActivityEvent::PRE_ACTOR_UPDATE, $event);
152
			$dispatcher->dispatch(ActivityEvent::PRE_SAVE, $event);
153
			$activity->save();
154
			$dispatcher->dispatch(ActivityEvent::POST_ACTOR_UPDATE, $event);
155
			$dispatcher->dispatch(ActivityEvent::POST_SAVE, $event);
156
			
157
			return Updated(['model' => $activity]);
158
		}
159
160
		return NotUpdated(['model' => $activity]);
161
	}
162
163
	/**
164
	 * Sets the ActivityObject id
165
	 * 
166
	 * @param mixed $id
167
	 * @param mixed $targetId
168
	 * @return PayloadInterface
169
	 */
170
	public function setTargetId($id, $targetId) {
171
		// find
172
		$activity = $this->get($id);
173
174
		if ($activity === null) {
175
			return new NotFound(['message' => 'Activity not found.']);
176
		}
177
178
		// update
179
		if ($activity->getTargetId() !== $targetId) {
180
			$activity->setTargetId($targetId);
181
182
			$event = new ActivityEvent($activity);
183
			$dispatcher = $this->getServiceContainer()->getDispatcher();
184
			$dispatcher->dispatch(ActivityEvent::PRE_TARGET_UPDATE, $event);
185
			$dispatcher->dispatch(ActivityEvent::PRE_SAVE, $event);
186
			$activity->save();
187
			$dispatcher->dispatch(ActivityEvent::POST_TARGET_UPDATE, $event);
188
			$dispatcher->dispatch(ActivityEvent::POST_SAVE, $event);
189
			
190
			return Updated(['model' => $activity]);
191
		}
192
193
		return NotUpdated(['model' => $activity]);
194
	}
195
196
	/**
197
	 * Updates a Activity with the given idand the provided data
198
	 * 
199
	 * @param mixed $id
200
	 * @param mixed $data
201
	 * @return PayloadInterface
202
	 */
203
	public function update($id, $data) {
204
		// find
205
		$activity = $this->get($id);
206
207
		if ($activity === null) {
208
			return new NotFound(['message' => 'Activity not found.']);
209
		}
210
211
		// hydrate
212
		$serializer = Activity::getSerializer();
213
		$activity = $serializer->hydrate($activity, $data);
214
215
		// dispatch
216
		$event = new ActivityEvent($activity);
217
		$dispatcher = $this->getServiceContainer()->getDispatcher();
218
		$dispatcher->dispatch(ActivityEvent::PRE_UPDATE, $event);
219
		$dispatcher->dispatch(ActivityEvent::PRE_SAVE, $event);
220
		$rows = $activity->save();
221
		$dispatcher->dispatch(ActivityEvent::POST_UPDATE, $event);
222
		$dispatcher->dispatch(ActivityEvent::POST_SAVE, $event);
223
224
		$payload = ['model' => $activity];
225
226
		if ($rows === 0) {
227
			return new NotUpdated($payload);
228
		}
229
230
		return new Updated($payload);
231
	}
232
233
	/**
234
	 * Implement this functionality at keeko\core\domain\ActivityDomain
235
	 * 
236
	 * @param ActivityQuery $query
237
	 * @param mixed $filter
238
	 * @return void
239
	 */
240
	abstract protected function applyFilter(ActivityQuery $query, $filter);
241
242
	/**
243
	 * Returns one Activity with the given id from cache
244
	 * 
245
	 * @param mixed $id
246
	 * @return Activity|null
247
	 */
248
	protected function get($id) {
249
		if ($this->pool === null) {
250
			$this->pool = new Map();
251
		} else if ($this->pool->has($id)) {
252
			return $this->pool->get($id);
253
		}
254
255
		$activity = ActivityQuery::create()->findOneById($id);
256
		$this->pool->set($id, $activity);
257
258
		return $activity;
259
	}
260
261
	/**
262
	 * Returns the service container
263
	 * 
264
	 * @return ServiceContainer
265
	 */
266
	abstract protected function getServiceContainer();
267
}
268