Passed
Pull Request — master (#5614)
by Angel Fernando Quiroz
07:17
created

Version20231110194300   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 75
dl 0
loc 118
rs 10
c 2
b 0
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 0 38 6
A getDefaultThemeNames() 0 31 1
A updateWebpackConfig() 0 21 2
A getDescription() 0 3 1
A copyDirectory() 0 14 5
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
use Symfony\Component\Finder\Finder;
13
14
final class Version20231110194300 extends AbstractMigrationChamilo
15
{
16
    public function getDescription(): string
17
    {
18
        return 'Copy custom theme folder to assets and update webpack.config';
19
    }
20
21
    private function getDefaultThemeNames(): array
22
    {
23
        return [
24
            'academica',
25
            'chamilo',
26
            'chamilo_red',
27
            'cosmic_campus',
28
            'holi',
29
            'readable',
30
            'sober_brown',
31
            'baby_orange',
32
            'chamilo_electric_blue',
33
            'chamilo_sport_red',
34
            'delicious_bordeaux',
35
            'journal',
36
            'royal_purple',
37
            'spacelab',
38
            'beach',
39
            'chamilo_green',
40
            'cool_blue',
41
            'empire_green',
42
            'kiddy',
43
            'silver_line',
44
            'steel_grey',
45
            'blue_lagoon',
46
            'chamilo_orange',
47
            'corporate',
48
            'fruity_orange',
49
            'medical',
50
            'simplex',
51
            'tasty_olive',
52
        ];
53
    }
54
55
    public function up(Schema $schema): void
56
    {
57
        $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

57
        /** @scrutinizer ignore-call */ 
58
        $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...
58
        $rootPath = $kernel->getProjectDir();
59
60
        $defaulThemesFolders = $this->getDefaultThemeNames();
61
62
        $sourceDir = $rootPath.'/app/Resources/public/css/themes';
63
        $destinationDir = $rootPath.'/assets/css/themes/';
64
        $chamiloDefaultCssPath = $destinationDir.'chamilo/default.css';
65
66
        if (!is_dir($sourceDir)) {
67
            return;
68
        }
69
70
        $finder = new Finder();
71
        $finder->directories()->in($sourceDir)->depth('== 0');
72
        $newThemes = [];
73
        foreach ($finder as $folder) {
74
            $folderName = $folder->getRelativePathname();
75
76
            if (!\in_array($folderName, $defaulThemesFolders, true)) {
77
                $sourcePath = $folder->getRealPath();
78
                $destinationPath = $destinationDir.$folderName;
79
80
                if (!file_exists($destinationPath)) {
81
                    $this->copyDirectory($sourcePath, $destinationPath);
82
                    $newThemes[] = $folderName;
83
84
                    if (file_exists($chamiloDefaultCssPath)) {
85
                        $newThemeDefaultCssPath = $destinationPath.'/default.css';
86
                        copy($chamiloDefaultCssPath, $newThemeDefaultCssPath);
87
                    }
88
                }
89
            }
90
        }
91
92
        $this->updateWebpackConfig($rootPath, $newThemes);
93
    }
94
95
    private function copyDirectory($src, $dst): void
96
    {
97
        $dir = opendir($src);
98
        @mkdir($dst);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

98
        /** @scrutinizer ignore-unhandled */ @mkdir($dst);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
99
        while (false !== ($file = readdir($dir))) {
100
            if (('.' !== $file) && ('..' !== $file)) {
101
                if (is_dir($src.'/'.$file)) {
102
                    $this->copyDirectory($src.'/'.$file, $dst.'/'.$file);
103
                } else {
104
                    copy($src.'/'.$file, $dst.'/'.$file);
105
                }
106
            }
107
        }
108
        closedir($dir);
109
    }
110
111
    private function updateWebpackConfig(string $rootPath, array $newThemes): void
112
    {
113
        $webpackConfigPath = $rootPath.'/webpack.config.js';
114
115
        if (!file_exists($webpackConfigPath)) {
116
            return;
117
        }
118
119
        $content = file_get_contents($webpackConfigPath);
120
        $pattern = '/(const themes = \\[\\s*")([^"\\]]+)("\\s*\\])/';
121
        $replacement = function ($matches) use ($newThemes) {
122
            $existingThemes = explode('", "', trim($matches[2], '"'));
123
            $allThemes = array_unique(array_merge($existingThemes, $newThemes));
124
            $newThemesString = implode('", "', $allThemes);
125
126
            return $matches[1].$newThemesString.$matches[3];
127
        };
128
129
        $newContent = preg_replace_callback($pattern, $replacement, $content);
130
131
        file_put_contents($webpackConfigPath, $newContent);
132
    }
133
}
134