Passed
Push — master ( 2a0c4b...cf1b71 )
by Angel Fernando Quiroz
07:25 queued 23s
created

addGlobalResourceLinkToNode()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
nc 6
nop 1
dl 0
loc 22
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Entity\ResourceLink;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
12
use Chamilo\CourseBundle\Entity\CCalendarEvent;
13
use DateTime;
14
use DateTimeZone;
15
use Doctrine\DBAL\Schema\Schema;
16
17
class Version20240323181500 extends AbstractMigrationChamilo
18
{
19
    public function getDescription(): string
20
    {
21
        return 'Migrate sys_calendar to c_calendar_event';
22
    }
23
24
    public function up(Schema $schema): void
25
    {
26
        $sysCalendars = $this->connection->fetchAllAssociative('SELECT * FROM sys_calendar');
27
28
        $utc = new DateTimeZone('UTC');
29
        $oldNewEventIdMap = [];
30
31
        $admin = $this->getAdmin();
32
        foreach ($sysCalendars as $sysCalendar) {
33
            $calendarEvent = $this->createCCalendarEvent(
34
                $sysCalendar['title'] ?: '-',
35
                $sysCalendar['content'],
36
                $sysCalendar['start_date'] ? new DateTime($sysCalendar['start_date'], $utc) : null,
37
                $sysCalendar['end_date'] ? new DateTime($sysCalendar['end_date'], $utc) : null,
38
                (bool) $sysCalendar['all_day'],
39
                $sysCalendar['color'] ?? '',
40
                $admin
41
            );
42
43
            $this->entityManager->persist($calendarEvent);
0 ignored issues
show
Bug introduced by
The method persist() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            $this->entityManager->/** @scrutinizer ignore-call */ 
44
                                  persist($calendarEvent);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
            $this->entityManager->flush();
45
46
            $this->addGlobalResourceLinkToNode($calendarEvent->getResourceNode());
47
48
            $oldNewEventIdMap[$sysCalendar['id']] = $calendarEvent;
49
        }
50
51
        $this->entityManager->flush();
52
53
        if ($schema->hasTable('agenda_reminder')) {
54
            $this->updateAgendaReminders($oldNewEventIdMap);
55
        }
56
    }
57
58
    private function createCCalendarEvent(
59
        string $title,
60
        string $content,
61
        ?DateTime $startDate,
62
        ?DateTime $endDate,
63
        bool $allDay,
64
        string $color,
65
        User $creator
66
    ): CCalendarEvent {
67
        $calendarEvent = new CCalendarEvent();
68
        $calendarEvent
69
            ->setTitle($title)
70
            ->setContent($content)
71
            ->setStartDate($startDate)
72
            ->setEndDate($endDate)
73
            ->setAllDay($allDay)
74
            ->setColor($color)
75
            ->setCreator($creator)
76
            ->setResourceName($title)
77
            ->setParentResourceNode($creator->getResourceNode()->getId())
78
        ;
79
80
        return $calendarEvent;
81
    }
82
83
    private function addGlobalResourceLinkToNode($resourceNode): void
84
    {
85
        $globalLink = new ResourceLink();
86
        $globalLink->setCourse(null)
87
            ->setSession(null)
88
            ->setGroup(null)
89
            ->setUser(null)
90
        ;
91
92
        $alreadyHasGlobalLink = false;
93
        foreach ($resourceNode->getResourceLinks() as $existingLink) {
94
            if (null === $existingLink->getCourse() && null === $existingLink->getSession()
95
                && null === $existingLink->getGroup() && null === $existingLink->getUser()) {
96
                $alreadyHasGlobalLink = true;
97
98
                break;
99
            }
100
        }
101
102
        if (!$alreadyHasGlobalLink) {
103
            $resourceNode->addResourceLink($globalLink);
104
            $this->entityManager->persist($globalLink);
105
        }
106
    }
107
108
    /**
109
     * @param array<int, CCalendarEvent> $oldNewEventIdMap
110
     */
111
    private function updateAgendaReminders(array $oldNewEventIdMap): void
112
    {
113
        $result = $this->connection->executeQuery("SELECT * FROM agenda_reminder WHERE type = 'admin'");
114
115
        while (($reminder = $result->fetchAssociative()) !== false) {
116
            $oldEventId = $reminder['event_id'];
117
            if (\array_key_exists($oldEventId, $oldNewEventIdMap)) {
118
                $newEvent = $oldNewEventIdMap[$oldEventId];
119
                $this->addSql(
120
                    sprintf(
121
                        "UPDATE agenda_reminder SET event_id = %d WHERE id = %d",
122
                        $newEvent->getIid(),
123
                        $reminder['id']
124
                    )
125
                );
126
            }
127
        }
128
    }
129
130
    public function down(Schema $schema): void
131
    {
132
        // Down migration is not defined, as data migration cannot be easily reverted
133
    }
134
}
135