Total Complexity | 55 |
Total Lines | 361 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like XApiEventSubscriber often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XApiEventSubscriber, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class XApiEventSubscriber implements EventSubscriberInterface |
||
49 | { |
||
50 | use XApiActivityTrait; |
||
51 | |||
52 | protected XApiPlugin $plugin; |
||
53 | |||
54 | public function __construct() |
||
55 | { |
||
56 | $this->plugin = XApiPlugin::create(); |
||
57 | } |
||
58 | |||
59 | public static function getSubscribedEvents(): array |
||
79 | ]; |
||
80 | } |
||
81 | |||
82 | public function onCreateCourse(CourseCreatedEvent $event): void |
||
83 | { |
||
84 | if (!$this->plugin->isEnabled(true)) { |
||
85 | return; |
||
86 | } |
||
87 | |||
88 | $course = $event->getCourse(); |
||
89 | |||
90 | if (AbstractEvent::TYPE_POST === $event->getType() && $course) { |
||
91 | $this->plugin->addCourseToolForTinCan($course->getId()); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @throws ORMException |
||
97 | * @throws OptimisticLockException |
||
98 | * @throws NotSupported |
||
99 | * @throws TransactionRequiredException |
||
100 | */ |
||
101 | public function onExerciseQuestionAnswered(ExerciseQuestionAnsweredEvent $event): void |
||
102 | { |
||
103 | if (!$this->plugin->isEnabled(true) |
||
104 | || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_QUIZ_QUESTION_ACTIVE) |
||
105 | ) { |
||
106 | return; |
||
107 | } |
||
108 | |||
109 | $em = Database::getManager(); |
||
110 | $attemptRepo = $em->getRepository(TrackEAttempt::class); |
||
111 | |||
112 | $exe = $em->find(TrackEExercise::class, $event->getTrackingExeId()); |
||
113 | $question = $em->find(CQuizQuestion::class, $event->getQuestionId()); |
||
114 | $attempt = $attemptRepo->findOneBy( |
||
115 | [ |
||
116 | 'exeId' => $exe->getExeId(), |
||
117 | 'questionId' => $question->getId(), |
||
118 | ] |
||
119 | ); |
||
120 | $quiz = $em->find(CQuiz::class, $event->getExerciseId()); |
||
121 | |||
122 | $quizQuestionAnswered = new QuizQuestionAnswered($attempt, $question, $quiz); |
||
123 | |||
124 | $statement = $quizQuestionAnswered->generate(); |
||
125 | |||
126 | $this->saveSharedStatement($statement); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @throws OptimisticLockException |
||
131 | * @throws ORMException |
||
132 | * @throws TransactionRequiredException |
||
133 | */ |
||
134 | public function onExerciseEnded(ExerciseEndedEvent $event): void |
||
135 | { |
||
136 | if (!$this->plugin->isEnabled(true) |
||
137 | || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_QUIZ_ACTIVE) |
||
138 | ) { |
||
139 | return; |
||
140 | } |
||
141 | |||
142 | $em = Database::getManager(); |
||
143 | |||
144 | $exe = $em->find(TrackEExercise::class, $event->getTrackingExeId()); |
||
145 | $quiz = $em->find(CQuiz::class, $exe->getExeExoId()); |
||
146 | |||
147 | $quizCompleted = new QuizCompleted($exe, $quiz); |
||
148 | |||
149 | $statement = $quizCompleted->generate(); |
||
150 | |||
151 | $this->saveSharedStatement($statement); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @throws ORMException |
||
156 | * @throws OptimisticLockException |
||
157 | * @throws TransactionRequiredException |
||
158 | */ |
||
159 | public function onLpItemViewed(LearningPathItemViewedEvent $event): void |
||
160 | { |
||
161 | if (!$this->plugin->isEnabled(true) |
||
162 | || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_LP_ITEM_ACTIVE) |
||
163 | ) { |
||
164 | return; |
||
165 | } |
||
166 | |||
167 | $em = Database::getManager(); |
||
168 | |||
169 | $lpItemView = $em->find(CLpItemView::class, $event->getItemViewId()); |
||
170 | $lpItem = $em->find(CLpItem::class, $lpItemView->getLpItemId()); |
||
171 | |||
172 | if ('quiz' == $lpItem->getItemType()) { |
||
173 | return; |
||
174 | } |
||
175 | |||
176 | $lpView = $em->find(CLpView::class, $lpItemView->getLpViewId()); |
||
177 | |||
178 | $lpItemViewed = new LearningPathItemViewed($lpItemView, $lpItem, $lpView); |
||
179 | |||
180 | $this->saveSharedStatement( |
||
181 | $lpItemViewed->generate() |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @throws OptimisticLockException |
||
187 | * @throws ORMException |
||
188 | * @throws TransactionRequiredException |
||
189 | */ |
||
190 | public function onLpEnded(LearningPathEndedEvent $event): void |
||
191 | { |
||
192 | if (!$this->plugin->isEnabled(true) |
||
193 | || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_LP_ACTIVE) |
||
194 | ) { |
||
195 | return; |
||
196 | } |
||
197 | |||
198 | $em = Database::getManager(); |
||
199 | |||
200 | $lpView = $em->find(CLpView::class, $event->getLpViewId()); |
||
201 | $lp = $em->find(CLp::class, $lpView->getLpId()); |
||
202 | |||
203 | $learningPathEnded = new LearningPathCompleted($lpView, $lp); |
||
204 | |||
205 | $this->saveSharedStatement( |
||
206 | $learningPathEnded->generate() |
||
207 | ); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @throws OptimisticLockException |
||
212 | * @throws ORMException |
||
213 | */ |
||
214 | public function onPortfolioItemAdded(PortfolioItemAddedEvent $event): void |
||
215 | { |
||
216 | if (!$this->plugin->isEnabled(true) |
||
217 | || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE) |
||
218 | ) { |
||
219 | return; |
||
220 | } |
||
221 | |||
222 | $item = $event->getPortfolio(); |
||
223 | |||
224 | if (!$item) { |
||
225 | return; |
||
226 | } |
||
227 | |||
228 | $statement = (new PortfolioItemShared($item))->generate(); |
||
229 | |||
230 | $this->saveSharedStatement($statement); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @throws OptimisticLockException |
||
235 | * @throws ORMException |
||
236 | */ |
||
237 | public function onPortfolioItemEdited(PortfolioItemEditedEvent $event): void |
||
238 | { |
||
239 | if (!$this->plugin->isEnabled(true) |
||
240 | || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE) |
||
241 | ) { |
||
242 | return; |
||
243 | } |
||
244 | |||
245 | $item = $event->getPortfolio(); |
||
246 | |||
247 | if (!$item) { |
||
248 | return; |
||
249 | } |
||
250 | |||
251 | $statement = (new PortfolioItemShared($item))->generate(); |
||
252 | |||
253 | $this->saveSharedStatement($statement); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @throws OptimisticLockException |
||
258 | * @throws ORMException |
||
259 | */ |
||
260 | public function onPortfolioItemViewed(PortfolioItemViewedEvent $event): void |
||
261 | { |
||
262 | if (!$this->plugin->isEnabled(true) |
||
263 | || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE) |
||
264 | ) { |
||
265 | return; |
||
266 | } |
||
267 | |||
268 | $item = $event->getPortfolio(); |
||
269 | |||
270 | if (!$item) { |
||
271 | return; |
||
272 | } |
||
273 | |||
274 | $statement = (new PortfolioItemViewed($item))->generate(); |
||
275 | |||
276 | $this->saveSharedStatement($statement); |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @throws OptimisticLockException |
||
281 | * @throws ORMException |
||
282 | */ |
||
283 | public function onPortfolioItemCommented(PortfolioItemCommentedEvent $event): void |
||
284 | { |
||
285 | if (!$this->plugin->isEnabled(true) |
||
286 | || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE) |
||
287 | ) { |
||
288 | return; |
||
289 | } |
||
290 | |||
291 | $comment = $event->getComment(); |
||
292 | |||
293 | if (!$comment) { |
||
294 | return; |
||
295 | } |
||
296 | |||
297 | $portfolioItemCommented = new PortfolioItemCommented($comment); |
||
298 | |||
299 | $statement = $portfolioItemCommented->generate(); |
||
300 | |||
301 | $this->saveSharedStatement($statement); |
||
302 | } |
||
303 | |||
304 | public function onPortfolioItemHighlighted(PortfolioItemHighlightedEvent $event): void |
||
305 | { |
||
306 | if (!$this->plugin->isEnabled(true) |
||
307 | || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE) |
||
308 | ) { |
||
309 | return; |
||
310 | } |
||
311 | |||
312 | $item = $event->getPortfolio(); |
||
313 | |||
314 | if (!$item) { |
||
315 | return; |
||
316 | } |
||
317 | |||
318 | $statement = (new PortfolioItemHighlighted($item))->generate(); |
||
319 | |||
320 | $this->saveSharedStatement($statement); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * @throws OptimisticLockException |
||
325 | * @throws ORMException |
||
326 | */ |
||
327 | public function onPortfolioItemDownloaded(PortfolioItemDownloadedEvent $event): void |
||
344 | } |
||
345 | |||
346 | public function onPortfolioItemScored(PortfolioItemScoredEvent $event): void |
||
347 | { |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * @throws OptimisticLockException |
||
367 | * @throws ORMException |
||
368 | */ |
||
369 | public function onPortfolioCommentScored(PortfolioCommentScoredEvent $event): void |
||
370 | { |
||
371 | if (!$this->plugin->isEnabled(true) |
||
372 | || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE) |
||
373 | ) { |
||
374 | return; |
||
375 | } |
||
376 | |||
377 | $comment = $event->getComment(); |
||
378 | |||
379 | if (!$comment) { |
||
380 | return; |
||
381 | } |
||
382 | |||
383 | $statement = (new PortfolioCommentScored($comment))->generate(); |
||
384 | |||
385 | $this->saveSharedStatement($statement); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @throws OptimisticLockException |
||
390 | * @throws ORMException |
||
391 | */ |
||
392 | public function onPortfolioCommentEdited(PortfolioCommentEditedEvent $event): void |
||
409 | } |
||
410 | } |
||
411 |