Passed
Push — master ( 13109f...2670e4 )
by Angel Fernando Quiroz
09:35
created

Version20250328161000   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 27 2
A getDescription() 0 3 1
A getNewIsoCode() 0 14 1
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
class Version20250328161000 extends AbstractMigrationChamilo
14
{
15
    public function getDescription(): string
16
    {
17
        return 'Migrate ext_translations.locale to new ISO code format for sublanguages';
18
    }
19
20
    public function up(Schema $schema): void
21
    {
22
        $sublanguages = $this->connection
23
            ->executeQuery(
24
                "SELECT * FROM language
25
                WHERE parent_id IS NOT NULL AND isocode NOT IN('".implode("', '", Version20::ALLOWED_SUBLANGUAGES)."')"
26
            )
27
            ->fetchAllAssociative()
28
        ;
29
30
        $filesystem = new Filesystem();
31
        $projectDir = $this->container->get('kernel')->getProjectDir();
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

31
        $projectDir = $this->container->/** @scrutinizer ignore-call */ get('kernel')->getProjectDir();

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...
32
33
        /** @var array $sublanguage */
34
        foreach ($sublanguages as $sublanguage) {
35
            $newIsoCode = $this->getNewIsoCode($sublanguage);
36
37
            // Update the isocode in the language table
38
            $this->connection->executeStatement(
39
                'UPDATE ext_translations SET locale = ? WHERE locale = ?',
40
                [$newIsoCode, $sublanguage['isocode']]
41
            );
42
43
            // Rename old PO file
44
            $filesystem->rename(
45
                $projectDir.'/var/translations/messages.'.$sublanguage['isocode'].'.po',
46
                $projectDir.'/var/translations/messages.'.$newIsoCode.'.po'
47
            );
48
        }
49
    }
50
51
    private function getNewIsoCode(array $sublanguage)
52
    {
53
        $parentId = $sublanguage['parent_id'];
54
55
        // Query to obtain the isocode of the parent language
56
        $parentIsoCode = $this->connection
57
            ->executeQuery('SELECT isocode FROM language WHERE id = ?', [$parentId])
58
            ->fetchOne()
59
        ;
60
61
        // Get the prefix of the parent language's isocode
62
        $firstIso = explode('_', $parentIsoCode)[0];
63
64
        return $firstIso.'_'.$sublanguage['id'];
65
    }
66
}