Passed
Pull Request — master (#6016)
by Angel Fernando Quiroz
19:45 queued 12:16
created

Version20250106152601::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 Doctrine\DBAL\Schema\Schema;
11
use Symfony\Component\Filesystem\Filesystem;
12
13
final class Version20250106152601 extends AbstractMigrationChamilo
14
{
15
    public function getDescription(): string
16
    {
17
        return 'Set new ISO code for sub-languages.';
18
    }
19
20
    /**
21
     * @inheritDoc
22
     */
23
    public function up(Schema $schema): void
24
    {
25
        $kernel = $this->container->get('kernel');
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

25
        /** @scrutinizer ignore-call */ 
26
        $kernel = $this->container->get('kernel');

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...
26
        $baseTranslationPath = $kernel->getProjectDir().'/var/translations/messages.';
27
28
        $fs = new Filesystem();
29
30
        $subLanguages = $this->connection
31
            ->executeQuery("SELECT id, isocode, parent_id FROM language WHERE parent_id IS NOT NULL")
32
            ->fetchAllAssociative()
33
        ;
34
35
        /** @var array $subLanguage */
36
        foreach ($subLanguages as $subLanguage) {
37
38
            $parentIsoCode = $this->connection
39
                ->executeQuery('SELECT isocode FROM language WHERE id = ?', [$subLanguage['parent_id']])
40
                ->fetchOne()
41
            ;
42
43
            $newIsoCode = sprintf(
44
                '%s_%d',
45
                explode('_', $parentIsoCode)[0],
46
                $subLanguage['id']
47
            );
48
49
            $params = [
50
                'new_iso' => $newIsoCode,
51
                'old_iso' => $subLanguage['isocode'],
52
            ];
53
54
            if ($params['new_iso'] === $params['old_iso']) {
55
                continue;
56
            }
57
58
            $this->addSql(
59
                'UPDATE language SET isocode = :new_iso WHERE id = :id',
60
                [
61
                    'new_iso' => $newIsoCode,
62
                    'id' => $subLanguage['id'],
63
                ]
64
            );
65
66
            $this->addSql('UPDATE user SET locale = :new_iso WHERE locale = :old_iso', $params);
67
            $this->addSql('UPDATE course SET course_language = :new_iso WHERE course_language = :old_iso', $params);
68
            $this->addSql("UPDATE settings SET selected_value = :new_iso WHERE variable = 'platform_language' AND selected_value = :old_iso", $params);
69
70
            $oldPoFile = $baseTranslationPath.$params['old_iso'].'.po';
71
            $newPoFile = $baseTranslationPath.$params['new_iso'].'.po';
72
73
            if ($fs->exists($oldPoFile)) {
74
                $fs->rename($oldPoFile, $newPoFile);
75
            }
76
        }
77
    }
78
}
79