Passed
Pull Request — master (#5310)
by Angel Fernando Quiroz
07:41
created

Version20240327172830::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
/* 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\Migrations\AbstractMigrationChamilo;
10
use Chamilo\CourseBundle\Repository\CLpCategoryRepository;
11
use Chamilo\CourseBundle\Repository\CLpRepository;
12
use Chamilo\CourseBundle\Repository\CShortcutRepository;
13
use Doctrine\DBAL\Schema\Schema;
14
15
class Version20240327172830 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Create shortcuts for c_lp and c_lp_category published on course home';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        // File generated in the Version20180928172830 migration
25
        $toolLinksContent = $this->readFile('tool_links');
26
27
        if (empty($toolLinksContent)) {
28
            $this->write('tool_links file not found. Exiting.');
29
30
            return;
31
        }
32
33
        $toolLinks = unserialize($toolLinksContent);
34
35
        $container = $this->getContainer();
36
        $em = $this->getEntityManager();
37
        $lpRepo = $container->get(CLpRepository::class);
38
        $lpCategoryRepo = $container->get(CLpCategoryRepository::class);
39
        $shortcutRepo = $container->get(CShortcutRepository::class);
40
41
        foreach ($toolLinks as $toolLink) {
42
            $url = parse_url($toolLink['link']);
43
            $query = [];
44
            parse_str($url['query'] ?? '', $query);
45
46
            $admin = $this->getAdmin();
47
            $course = $this->findCourse($toolLink['c_id']);
48
            $session = $this->findSession($toolLink['session_id']);
49
            $resource = null;
50
51
            if (str_contains($url['path'], 'lp_controller.php') && isset($query['action'])) {
52
                if (isset($query['lp_id']) && 'view' === $query['action']) {
53
                    $resource = $lpRepo->find($query['lp_id']);
54
                } elseif (isset($query['id']) && 'view_category' === $query['action']) {
55
                    $resource = $lpCategoryRepo->find($query['id']);
56
                }
57
            }
58
59
            if ($resource) {
60
                $shortcut = $shortcutRepo->getShortcutFromResource($resource);
61
62
                if ($shortcut) {
63
                    continue;
64
                }
65
66
                $shortcutRepo->addShortCut($resource, $admin, $course, $session);
67
            }
68
        }
69
70
        $em->flush();
71
72
        $this->removeFile('tool_links');
73
    }
74
}
75