Passed
Push — master ( db12d9...b58c60 )
by Julito
12:19
created

Version20201215072918::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
4
5
use Chamilo\CoreBundle\Entity\AccessUrl;
6
use Chamilo\CoreBundle\Entity\AccessUrlRelCourse;
7
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
8
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
9
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
10
use Chamilo\CoreBundle\Repository\Node\UserRepository;
11
use Chamilo\CoreBundle\Repository\SessionRepository;
12
use Chamilo\CourseBundle\Entity\CCalendarEvent;
13
use Chamilo\CourseBundle\Entity\CCalendarEventAttachment;
14
use Chamilo\CourseBundle\Repository\CCalendarEventAttachmentRepository;
15
use Chamilo\CourseBundle\Repository\CCalendarEventRepository;
16
use Chamilo\CourseBundle\Repository\CGroupRepository;
17
use Chamilo\Kernel;
18
use Doctrine\DBAL\Connection;
19
use Doctrine\DBAL\Schema\Schema;
20
21
final class Version20201215072918 extends AbstractMigrationChamilo
22
{
23
    public function getDescription(): string
24
    {
25
        return 'Migrate agenda';
26
    }
27
28
    public function up(Schema $schema): void
29
    {
30
        $container = $this->getContainer();
31
        $doctrine = $container->get('doctrine');
32
        $em = $doctrine->getManager();
33
        /** @var Connection $connection */
34
        $connection = $em->getConnection();
35
36
        $urlRepo = $em->getRepository(AccessUrlRepository::class);
37
        $eventRepo = $container->get(CCalendarEventRepository::class);
38
        $eventAttachmentRepo = $container->get(CCalendarEventAttachmentRepository::class);
39
        $courseRepo = $container->get(CourseRepository::class);
40
        $sessionRepo = $container->get(SessionRepository::class);
41
        $groupRepo = $container->get(CGroupRepository::class);
42
        $userRepo = $container->get(UserRepository::class);
43
44
        /** @var Kernel $kernel */
45
        $kernel = $container->get('kernel');
46
        $rootPath = $kernel->getProjectDir();
47
        $admin = $this->getAdmin();
48
        $urls = $urlRepo->findAll();
49
50
        /** @var AccessUrl $url */
51
        foreach ($urls as $url) {
52
            $accessUrlRelCourses = $url->getCourses();
53
            /** @var AccessUrlRelCourse $accessUrlRelCourse */
54
            foreach ($accessUrlRelCourses as $accessUrlRelCourse) {
55
                $counter = 1;
56
                $course = $accessUrlRelCourse->getCourse();
57
                $courseId = $course->getId();
58
                $courseCode = $course->getCode();
59
                $course = $courseRepo->find($courseId);
60
61
                $sql = "SELECT * FROM c_calendar_event WHERE c_id = $courseId
62
                        ORDER BY iid";
63
                $result = $connection->executeQuery($sql);
64
                $events = $result->fetchAllAssociative();
65
                foreach ($events as $event) {
66
                    $id = $event['iid'];
67
                    /** @var CCalendarEvent $event */
68
                    $event = $eventRepo->find($id);
69
                    if ($event->hasResourceNode()) {
70
                        continue;
71
                    }
72
                    $sql = "SELECT * FROM c_item_property
73
                            WHERE tool = 'calendar_event' AND c_id = $courseId AND ref = $id";
74
                    $result = $connection->executeQuery($sql);
75
                    $items = $result->fetchAllAssociative();
76
77
                    // For some reason this document doesnt have a c_item_property value.
78
                    if (empty($items)) {
79
                        continue;
80
                    }
81
                    $parent = null;
82
                    if (!empty($event['parent_event_id'])) {
83
                        $parent = $eventRepo->find($event['parent_event_id']);
84
                    }
85
                    if (null === $parent) {
86
                        $parent = $course;
87
                    }
88
                    $this->fixItemProperty($eventRepo, $course, $admin, $event, $parent, $items);
89
                }
90
91
                $sql = "SELECT * FROM c_calendar_event_attachment WHERE c_id = $courseId
92
                        ORDER BY iid";
93
                $result = $connection->executeQuery($sql);
94
                $events = $result->fetchAllAssociative();
95
                foreach ($events as $event) {
96
                    $id = $event['iid'];
97
                    $attachmentPath = $event['filename'];
98
                    /** @var CCalendarEventAttachment $event */
99
                    $event = $eventAttachmentRepo->find($id);
100
                    if ($event->hasResourceNode()) {
101
                        continue;
102
                    }
103
                    $sql = "SELECT * FROM c_item_property
104
                            WHERE tool = 'calendar_event_attachment' AND c_id = $courseId AND ref = $id";
105
                    $result = $connection->executeQuery($sql);
106
                    $items = $result->fetchAllAssociative();
107
                    $this->fixItemProperty($eventAttachmentRepo, $event, $parent, $items);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parent does not seem to be defined for all execution paths leading up to this point.
Loading history...
Bug introduced by
The call to Chamilo\CoreBundle\Migra...milo::fixItemProperty() has too few arguments starting with parent. ( Ignorable by Annotation )

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

107
                    $this->/** @scrutinizer ignore-call */ 
108
                           fixItemProperty($eventAttachmentRepo, $event, $parent, $items);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
108
                    $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/upload/calendar/'.$attachmentPath;
109
                    $this->addLegacyFileToResource($filePath, $eventAttachmentRepo, $event, $id);
110
                }
111
            }
112
        }
113
    }
114
115
    public function down(Schema $schema): void
116
    {
117
    }
118
}
119