Passed
Push — master ( ec327a...c19cb6 )
by Yannick
11:02
created

Version20250321000100::down()   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 1
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\Entity\AccessUrl;
10
use Chamilo\CoreBundle\Entity\CatalogueCourseRelAccessUrlRelUsergroup;
11
use Chamilo\CoreBundle\Entity\Course;
12
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
13
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
14
use Doctrine\DBAL\Schema\Schema;
15
use Exception;
16
17
final class Version20250321000100 extends AbstractMigrationChamilo
18
{
19
    public function getDescription(): string
20
    {
21
        return 'Migrates courses using the show_in_catalogue extra field into the catalogue_course_rel_access_url_rel_usergroup table.';
22
    }
23
24
    public function up(Schema $schema): void
25
    {
26
        $this->entityManager->beginTransaction();
0 ignored issues
show
Bug introduced by
The method beginTransaction() 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

26
        $this->entityManager->/** @scrutinizer ignore-call */ 
27
                              beginTransaction();

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...
27
28
        try {
29
            if (!$this->tableExists('extra_field') || !$this->tableExists('extra_field_values')) {
30
                return;
31
            }
32
33
            /** @var AccessUrlRepository $accessUrlRepo */
34
            $accessUrlRepo = $this->container->get(AccessUrlRepository::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

34
            /** @scrutinizer ignore-call */ 
35
            $accessUrlRepo = $this->container->get(AccessUrlRepository::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...
35
            $accessUrlId = $accessUrlRepo->getFirstId();
36
37
            if ($accessUrlId === 0) {
38
                throw new Exception('No AccessUrl found for migration');
39
            }
40
41
            /** @var AccessUrl|null $accessUrl */
42
            $accessUrl = $this->entityManager->find(AccessUrl::class, $accessUrlId);
43
            if (!$accessUrl) {
44
                throw new Exception('AccessUrl entity not found for ID: ' . $accessUrlId);
45
            }
46
47
            $courseRepo = $this->entityManager->getRepository(Course::class);
48
49
            $courseIds = $this->connection->fetchFirstColumn('
50
                SELECT fv.item_id
51
                FROM extra_field_values fv
52
                INNER JOIN extra_field f ON f.id = fv.field_id
53
                WHERE f.item_type = 2
54
                  AND f.variable = "show_in_catalogue"
55
                  AND fv.field_value = 1
56
            ');
57
58
            foreach ($courseIds as $courseId) {
59
                $course = $courseRepo->find($courseId);
60
61
                if (!$course) {
62
                    continue;
63
                }
64
65
                $rel = new CatalogueCourseRelAccessUrlRelUsergroup();
66
                $rel->setAccessUrl($accessUrl);
67
                $rel->setCourse($course);
68
                $rel->setUsergroup(null);
69
70
                $this->entityManager->persist($rel);
71
            }
72
73
            $this->entityManager->flush();
74
            $this->entityManager->commit();
75
        } catch (Exception $e) {
76
            $this->entityManager->rollBack();
77
            error_log('Migration failed: ' . $e->getMessage());
78
        }
79
    }
80
81
    public function down(Schema $schema): void
82
    {
83
        $this->addSql('DELETE FROM catalogue_course_rel_access_url_rel_usergroup');
84
    }
85
86
    private function tableExists(string $tableName): bool
87
    {
88
        try {
89
            $this->connection->executeQuery("SELECT 1 FROM $tableName LIMIT 1");
90
            return true;
91
        } catch (Exception $e) {
92
            return false;
93
        }
94
    }
95
}
96