1 | <?php |
||
14 | class AchievementsStorage implements AchievementsStorageInterface |
||
15 | { |
||
16 | const MODEL_ACHIEVEMENT = 'achievement'; |
||
17 | const MODEL_CRITERIA = 'criteria'; |
||
18 | |||
19 | /** |
||
20 | * @var Repository |
||
21 | */ |
||
22 | protected $config; |
||
23 | |||
24 | /** |
||
25 | * AchievementsStorage constructor. |
||
26 | * |
||
27 | * @param Repository $config |
||
28 | */ |
||
29 | public function __construct(Repository $config) |
||
33 | |||
34 | /** |
||
35 | * Returns actual class name from config. |
||
36 | * |
||
37 | * @param string $section |
||
38 | * @param string $type |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | protected function getConfigurableClassName(string $section, string $type) |
||
46 | |||
47 | /** |
||
48 | * Injects model class name into given callback. |
||
49 | * |
||
50 | * @param string $type |
||
51 | * @param callable $callback |
||
52 | * |
||
53 | * @return mixed|string |
||
54 | */ |
||
55 | protected function getModelClass(string $type, callable $callback) |
||
59 | |||
60 | /** |
||
61 | * Dispatches new Achievements event. |
||
62 | * |
||
63 | * @param string $type |
||
64 | * @param array $args |
||
65 | * @return mixed |
||
66 | */ |
||
67 | protected function dispatchEvent(string $type, array $args = []) |
||
73 | |||
74 | /** |
||
75 | * Fetches given owner's criterias by given type. |
||
76 | * |
||
77 | * @param mixed $owner |
||
78 | * @param string $type |
||
79 | * |
||
80 | * @return array |
||
81 | */ |
||
82 | public function getOwnerCriteriasByType($owner, string $type) |
||
100 | |||
101 | /** |
||
102 | * Loads criteria progress for given owner, applies given query callback |
||
103 | * and returns array of AchievementCriteria objects transformed from AchievementCriteriaModels. |
||
104 | * |
||
105 | * @param mixed $owner |
||
106 | * @param callable $callback |
||
107 | * |
||
108 | * @return \Illuminate\Support\Collection |
||
109 | */ |
||
110 | protected function getOwnerCriteriasProgress($owner, callable $callback) |
||
118 | |||
119 | /** |
||
120 | * Loads achievement criterias with given type. |
||
121 | * |
||
122 | * @param string $type |
||
123 | * |
||
124 | * @return \Illuminate\Database\Eloquent\Collection |
||
125 | */ |
||
126 | protected function getCriteriasByType(string $type) |
||
132 | |||
133 | /** |
||
134 | * Transforms AchievementCriteriaModel collection int AchievementCriteriaProgress collection. |
||
135 | * |
||
136 | * @param Collection $criterias |
||
137 | * |
||
138 | * @return \Illuminate\Support\Collection |
||
139 | */ |
||
140 | protected function transformOwnerCriteriasToProgress(Collection $criterias) |
||
153 | |||
154 | /** |
||
155 | * Transforms AchievementCriteriaModel to AchievementCriteria object and attaches progress data. |
||
156 | * |
||
157 | * @param AchievementCriteriaModel $criteria |
||
158 | * @param AchievementCriteriaProgress|null $progress |
||
159 | * |
||
160 | * @return AchievementCriteria |
||
161 | */ |
||
162 | protected function transformCriteriaWithProgress($criteria, AchievementCriteriaProgress $progress = null) |
||
184 | |||
185 | /** |
||
186 | * Returns list of criterias' achievements. |
||
187 | * |
||
188 | * @param array $criterias |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | public function getAchievementsByCriterias(array $criterias) |
||
206 | |||
207 | /** |
||
208 | * Loads collection of achievements by IDs. |
||
209 | * |
||
210 | * @param array $achievementIds |
||
211 | * |
||
212 | * @return mixed |
||
213 | */ |
||
214 | public function getAchievementsByIds(array $achievementIds) |
||
220 | |||
221 | /** |
||
222 | * Converts collection of AchievementModel objects to array of Achievement objects. |
||
223 | * |
||
224 | * @param Collection $achievements |
||
225 | * @param bool $reloadCriteriasRelation = true |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | public function convertAchievementModelsWithCriterias(Collection $achievements, bool $reloadCriteriasRelation = true) |
||
237 | |||
238 | /** |
||
239 | * Converts single AchievementModel to Achievement object. |
||
240 | * |
||
241 | * @param AchievementModel $achievement |
||
242 | * |
||
243 | * @return Achievement |
||
244 | */ |
||
245 | public function convertAchievementModelWithCriterias($achievement) |
||
255 | |||
256 | /** |
||
257 | * Transforms AchievementModel collection to Achievement collection and applies appropriate criterias. |
||
258 | * |
||
259 | * @param Collection $achievements |
||
260 | * @param array $criterias |
||
261 | * |
||
262 | * @return \Illuminate\Support\Collection |
||
263 | */ |
||
264 | protected function transformAchievements(Collection $achievements, array $criterias) |
||
272 | |||
273 | /** |
||
274 | * Transforms single AchievementModel object to Achievement object with appropriate criterias. |
||
275 | * |
||
276 | * @param AchievementModel $achievement |
||
277 | * @param array $criterias |
||
278 | * |
||
279 | * @return Achievement |
||
280 | */ |
||
281 | protected function transformSingleAchievement($achievement, array $criterias) |
||
304 | |||
305 | /** |
||
306 | * Extracts single Achievement from given list for given criteria. |
||
307 | * |
||
308 | * @param AchievementCriteria $criteria |
||
309 | * @param array $achievements |
||
310 | * |
||
311 | * @return Achievement |
||
312 | */ |
||
313 | public function getAchievementForCriteria(AchievementCriteria $criteria, array $achievements) |
||
327 | |||
328 | /** |
||
329 | * Loads achievements with progresses for given owner. |
||
330 | * |
||
331 | * @param mixed $owner |
||
332 | * @param array $achievementIds |
||
333 | * |
||
334 | * @return array |
||
335 | */ |
||
336 | public function getAchievementsWithProgressFor($owner, array $achievementIds) |
||
356 | |||
357 | /** |
||
358 | * Loads criterias by achievement IDs. |
||
359 | * |
||
360 | * @param array $achievementIds |
||
361 | * |
||
362 | * @return \Illuminate\Database\Eloquent\Collection |
||
363 | */ |
||
364 | public function getCriteriasByAchievementIds(array $achievementIds) |
||
370 | |||
371 | /** |
||
372 | * Saves criteria progress for given owner. |
||
373 | * |
||
374 | * @param mixed $owner |
||
375 | * @param AchievementCriteria $criteria |
||
376 | * @param Achievement $achievement |
||
377 | * @param AchievementCriteriaProgress $progress |
||
378 | * |
||
379 | * @return mixed |
||
380 | */ |
||
381 | public function setCriteriaProgressUpdated($owner, AchievementCriteria $criteria, Achievement $achievement, AchievementCriteriaProgress $progress) |
||
395 | |||
396 | /** |
||
397 | * Saves given achievements completeness state for given owner. |
||
398 | * |
||
399 | * @param mixed $owner |
||
400 | * @param array $achievements |
||
401 | * |
||
402 | * @return mixed |
||
403 | */ |
||
404 | public function setAchievementsCompleted($owner, array $achievements) |
||
419 | } |
||
420 |