Passed
Push — master ( 1e0b50...40a262 )
by Angel Fernando Quiroz
09:11
created

CalendarEventTransformer::mapCCalendarToDto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 9.9332
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\DataTransformer;
8
9
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
10
use Chamilo\CoreBundle\ApiResource\CalendarEvent;
11
use Chamilo\CoreBundle\Entity\Session;
12
use Chamilo\CoreBundle\Entity\SessionRelCourse;
13
use Chamilo\CourseBundle\Entity\CCalendarEvent;
14
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15
use Symfony\Component\Routing\RouterInterface;
16
17
class CalendarEventTransformer implements DataTransformerInterface
18
{
19
    public function __construct(
20
        private readonly RouterInterface $router,
21
    ) {}
22
23
    public function transform($object, string $to, array $context = []): object
24
    {
25
        if ($object instanceof Session) {
26
            return $this->mapSessionToDto($object);
27
        }
28
29
        return $this->mapCCalendarToDto($object);
30
    }
31
32
    public function supportsTransformation($data, string $to, array $context = []): bool
33
    {
34
        return ($data instanceof CCalendarEvent || $data instanceof Session) && CalendarEvent::class === $to;
35
    }
36
37
    private function mapCCalendarToDto(object $object): CalendarEvent
38
    {
39
        \assert($object instanceof CCalendarEvent);
40
41
        return new CalendarEvent(
42
            'calendar_event_'.$object->getIid(),
43
            $object->getTitle(),
44
            $object->getContent(),
45
            $object->getStartDate(),
46
            $object->getEndDate(),
47
            $object->isAllDay(),
48
            null,
49
            $object->getResourceNode(),
50
        );
51
    }
52
53
    private function mapSessionToDto(object $object): CalendarEvent
54
    {
55
        \assert($object instanceof Session);
56
57
        /** @var ?SessionRelCourse $sessionRelCourse */
58
        $sessionRelCourse = $object->getCourses()->first();
59
        $course = $sessionRelCourse?->getCourse();
60
61
        $sessionUrl = null;
62
63
        if ($course) {
0 ignored issues
show
introduced by
$course is of type Chamilo\CoreBundle\Entity\Course, thus it always evaluated to true.
Loading history...
64
            $baseUrl = $this->router->generate('index', [], UrlGeneratorInterface::ABSOLUTE_URL);
65
66
            $sessionUrl = "{$baseUrl}course/{$course->getId()}/home?".http_build_query(['sid' => $object->getId()]);
67
        }
68
69
        return new CalendarEvent(
70
            'session_'.$object->getId(),
71
            $object->getTitle(),
72
            $object->getShowDescription() ? $object->getDescription() : null,
73
            $object->getDisplayStartDate(),
74
            $object->getDisplayEndDate(),
75
            false,
76
            $sessionUrl,
77
        );
78
    }
79
}
80