Completed
Push — master ( 1d5aab...1362ac )
by Yannick
02:39 queued 01:44
created

Version20250403121200   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 59
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 6 1
A getDescription() 0 3 1
A up() 0 43 3
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\CoreBundle\Repository\Node\AccessUrlRepository;
11
use Doctrine\DBAL\Schema\Schema;
12
use Exception;
13
14
final class Version20250403121200 extends AbstractMigrationChamilo
15
{
16
    public function getDescription(): string
17
    {
18
        return 'Set the first AccessUrl as default for all existing records in ticket_priority, ticket_project, ticket_status, and ticket_ticket if access_url_id is null.';
19
    }
20
21
    public function up(Schema $schema): void
22
    {
23
        $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

23
        $this->entityManager->/** @scrutinizer ignore-call */ 
24
                              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...
24
25
        try {
26
            /** @var AccessUrlRepository $accessUrlRepo */
27
            $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

27
            /** @scrutinizer ignore-call */ 
28
            $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...
28
            $firstAccessUrl = $accessUrlRepo->find($accessUrlRepo->getFirstId());
29
30
            if (!$firstAccessUrl) {
31
                throw new Exception('No AccessUrl found for migration');
32
            }
33
34
            $accessUrlId = $firstAccessUrl->getId();
35
36
            // Update ticket_priority
37
            $this->connection->executeStatement(
38
                'UPDATE ticket_priority SET access_url_id = :accessUrlId WHERE access_url_id IS NULL',
39
                ['accessUrlId' => $accessUrlId]
40
            );
41
42
            // Update ticket_project
43
            $this->connection->executeStatement(
44
                'UPDATE ticket_project SET access_url_id = :accessUrlId WHERE access_url_id IS NULL',
45
                ['accessUrlId' => $accessUrlId]
46
            );
47
48
            // Update ticket_status
49
            $this->connection->executeStatement(
50
                'UPDATE ticket_status SET access_url_id = :accessUrlId WHERE access_url_id IS NULL',
51
                ['accessUrlId' => $accessUrlId]
52
            );
53
54
            // Update ticket_ticket
55
            $this->connection->executeStatement(
56
                'UPDATE ticket_ticket SET access_url_id = :accessUrlId WHERE access_url_id IS NULL',
57
                ['accessUrlId' => $accessUrlId]
58
            );
59
60
            $this->entityManager->commit();
61
        } catch (Exception $e) {
62
            $this->entityManager->rollBack();
63
            error_log('[Migration] Failed to set default AccessUrl: ' . $e->getMessage());
64
        }
65
    }
66
67
    public function down(Schema $schema): void
68
    {
69
        $this->connection->executeStatement('UPDATE ticket_priority SET access_url_id = NULL');
70
        $this->connection->executeStatement('UPDATE ticket_project SET access_url_id = NULL');
71
        $this->connection->executeStatement('UPDATE ticket_status SET access_url_id = NULL');
72
        $this->connection->executeStatement('UPDATE ticket_ticket SET access_url_id = NULL');
73
    }
74
}
75