Passed
Pull Request — master (#6182)
by Yannick
16:31 queued 06:13
created

Version20250331202800::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\Entity\AccessUrl;
10
use Chamilo\CoreBundle\Entity\Page;
11
use Chamilo\CoreBundle\Entity\PageCategory;
12
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
13
use Doctrine\DBAL\Schema\Schema;
14
15
final class Version20250331202800 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Migrates registration introduction pages from Chamilo 1 (custom HTML files) into the CMS Page entity.';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $accessUrlRepo = $this->entityManager->getRepository(AccessUrl::class);
0 ignored issues
show
Bug introduced by
The method getRepository() 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

24
        /** @scrutinizer ignore-call */ 
25
        $accessUrlRepo = $this->entityManager->getRepository(AccessUrl::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...
25
        $pageCategoryRepo = $this->entityManager->getRepository(PageCategory::class);
26
        $pageRepo = $this->entityManager->getRepository(Page::class);
27
        $adminUser = $this->getAdmin();
28
29
        // Source directory where Chamilo 1 custom pages are located
30
        $sourcePath = $this->getUpdateRootPath().'/app/home';
31
        error_log("[MIGRATION] Looking for registration HTML files in: $sourcePath");
32
33
        // Get or create the "introduction" category
34
        $category = $pageCategoryRepo->findOneBy(['title' => 'introduction']);
35
        if (!$category) {
36
            $category = new PageCategory();
37
            $category
38
                ->setTitle('introduction')
39
                ->setType('cms')
40
                ->setCreator($adminUser);
41
            $this->entityManager->persist($category);
42
            $this->entityManager->flush();
43
            error_log('[MIGRATION] Created "introduction" category.');
44
        }
45
46
        // Loop through directories like /app/home/localhost/
47
        $accessUrls = scandir($sourcePath);
48
        foreach ($accessUrls as $dirName) {
49
            if (in_array($dirName, ['.', '..'])) {
50
                continue;
51
            }
52
53
            $dirPath = $sourcePath.'/'.$dirName;
54
            if (!is_dir($dirPath)) {
55
                continue;
56
            }
57
58
            error_log("[MIGRATION] Checking directory: $dirName");
59
60
            // Look for files like register_top_spanish.html
61
            foreach (glob($dirPath.'/register_top_*.html') as $filePath) {
62
                $matches = [];
63
                if (!preg_match('/register_top_(.+)\.html$/', basename($filePath), $matches)) {
64
                    error_log("[MIGRATION] File name does not match expected pattern: $filePath");
65
                    continue;
66
                }
67
68
                $locale = $matches[1];
69
70
                // Try to find AccessUrl with both http/https and with/without trailing slash
71
                $normalizedUrls = [
72
                    'http://' . $dirName . '/',
73
                    'https://' . $dirName . '/',
74
                    'http://' . $dirName,
75
                    'https://' . $dirName,
76
                ];
77
78
                $accessUrl = null;
79
                foreach ($normalizedUrls as $url) {
80
                    $accessUrl = $accessUrlRepo->findOneBy(['url' => $url]);
81
                    if ($accessUrl) {
82
                        break;
83
                    }
84
                }
85
86
                if (!$accessUrl) {
87
                    error_log("[MIGRATION] AccessUrl not found for http(s)://$dirName with or without trailing slash");
88
                    continue;
89
                }
90
91
                // Skip if page already exists
92
                $existingPage = $pageRepo->findOneBy([
93
                    'category' => $category,
94
                    'url' => $accessUrl,
95
                    'locale' => $locale,
96
                ]);
97
98
                if ($existingPage) {
99
                    error_log("[MIGRATION] Page already exists for URL=$accessUrl->getUrl(), locale=$locale. Skipped.");
100
                    continue;
101
                }
102
103
                // Read file content
104
                $content = file_get_contents($filePath);
105
                if (empty($content)) {
106
                    error_log("[MIGRATION] File is empty: $filePath");
107
                    continue;
108
                }
109
110
                // Create new Page entity
111
                $page = new Page();
112
                $page
113
                    ->setTitle('Intro inscription')
114
                    ->setSlug('intro-inscription')
115
                    ->setContent($content)
116
                    ->setLocale($locale)
117
                    ->setCategory($category)
118
                    ->setEnabled(true)
119
                    ->setCreator($adminUser)
120
                    ->setUrl($accessUrl)
121
                    ->setPosition(1);
122
123
                $this->entityManager->persist($page);
124
125
                error_log("[MIGRATION] Page created for: URL={$accessUrl->getUrl()}, locale=$locale");
126
            }
127
        }
128
129
        $this->entityManager->flush();
130
        error_log('[MIGRATION] Migration completed successfully.');
131
    }
132
133
    public function down(Schema $schema): void {}
134
}
135