Passed
Push — master ( 212654...8aa330 )
by Angel Fernando Quiroz
11:04
created

TopLinksEventSubscriber::onCreateCourse()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
nc 4
nop 1
dl 0
loc 13
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
use Chamilo\CoreBundle\Event\CourseCreatedEvent;
8
use Chamilo\CoreBundle\Event\AbstractEvent;
9
use Chamilo\CoreBundle\Event\Events;
10
use Chamilo\PluginBundle\Entity\TopLinks\TopLink;
11
use Doctrine\ORM\EntityManagerInterface;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
readonly class TopLinksEventSubscriber implements EventSubscriberInterface
15
{
16
    private TopLinksPlugin $plugin;
17
18
    public function __construct(
19
        private EntityManagerInterface $entityManager
20
    ) {
21
        $this->plugin = TopLinksPlugin::create();
22
    }
23
24
    public static function getSubscribedEvents(): array
25
    {
26
        return [
27
            Events::COURSE_CREATED => 'onCreateCourse',
28
        ];
29
    }
30
31
    public function onCreateCourse(CourseCreatedEvent $event): void
32
    {
33
        if (!$this->plugin->isEnabled(true)) {
34
            return;
35
        }
36
37
        $linkRepo = $this->entityManager->getRepository(TopLink::class);
38
39
        $course = $event->getCourse();
40
41
        if (AbstractEvent::TYPE_POST === $event->getType() && $course) {
42
            foreach ($linkRepo->findAll() as $link) {
43
                $this->plugin->addToolInCourse($course->getId(), $link);
44
            }
45
        }
46
    }
47
}
48