Passed
Push — master ( be9e8f...e71d0d )
by
unknown
10:05
created

Version20240202122300::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Chamilo\CourseBundle\Entity\CLink;
11
use Chamilo\CourseBundle\Repository\CLinkRepository;
12
use Chamilo\CourseBundle\Repository\CShortcutRepository;
13
use Doctrine\DBAL\Schema\Schema;
14
use Exception;
15
16
class Version20240202122300 extends AbstractMigrationChamilo
17
{
18
    public function getDescription(): string
19
    {
20
        return 'Create shortcuts for c_link entries with on_homepage = 1';
21
    }
22
23
    public function up(Schema $schema): void
24
    {
25
        $admin = $this->getAdmin();
26
27
        $linkRepo = $this->container->get(CLinkRepository::class);
0 ignored issues
show
Bug introduced by
The method get() 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

27
        /** @scrutinizer ignore-call */ 
28
        $linkRepo = $this->container->get(CLinkRepository::class);

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...
28
        $shortcutRepo = $this->container->get(CShortcutRepository::class);
29
30
        $sql = 'SELECT * FROM c_link WHERE on_homepage = 1';
31
        $stmt = $this->connection->prepare($sql);
32
        $result = $stmt->executeQuery();
33
34
        while ($row = $result->fetchAssociative()) {
35
            $linkId = $row['iid'];
36
37
            /** @var CLink $link */
38
            $link = $linkRepo->find($linkId);
39
40
            if (!$link) {
41
                error_log("Link with ID $linkId not found");
42
43
                continue;
44
            }
45
46
            if (null === $link->getFirstResourceLink()) {
47
                error_log("Link with ID $linkId does not have a resource_node");
48
49
                continue;
50
            }
51
52
            $course = $link->getFirstResourceLink()->getCourse();
53
            $session = $link->getFirstResourceLink()->getSession();
54
55
            $shortcut = $shortcutRepo->getShortcutFromResource($link);
56
            if (null === $shortcut) {
57
                try {
58
                    $shortcutRepo->addShortCut($link, $admin, $course, $session);
59
                    error_log("Shortcut created for link ID $linkId");
60
                } catch (Exception $e) {
61
                    error_log("Failed to create shortcut for link ID $linkId: ".$e->getMessage());
62
                }
63
            } else {
64
                error_log("Shortcut already exists for link ID $linkId");
65
            }
66
        }
67
68
        $this->entityManager->flush();
0 ignored issues
show
Bug introduced by
The method flush() 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

68
        $this->entityManager->/** @scrutinizer ignore-call */ 
69
                              flush();

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...
69
    }
70
71
    public function down(Schema $schema): void {}
72
}
73