Passed
Push — main ( 4e6733...8ed175 )
by Rafael
04:54
created

ComposerScripts::copyAssets()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 25
rs 9.1111
c 0
b 0
f 0
cc 6
nc 6
nop 0
1
<?php
2
3
/* Copyright (C) 2024       Rafael San José         <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Scripts;
20
21
use Composer\Installer\PackageEvent;
22
23
abstract class ComposerScripts
24
{
25
    public static function postUpdate(PackageEvent $event)
26
    {
27
        echo "\n*** Starting assets update process...\n\n";
28
29
        if (getenv('SKIP_COPY_ASSETS')) {
30
            echo "Prevented copyAssets in scrutinizer environment.\n";
31
            return;
32
        }
33
34
        $composer = $event->getComposer();
0 ignored issues
show
Unused Code introduced by
The assignment to $composer is dead and can be removed.
Loading history...
35
        $io = $event->getIO();
36
37
        // Perform operations here
38
        $io->write("Running post-update script");
39
40
        static::copyAssets();
41
    }
42
43
    private static function copyAssets()
44
    {
45
        echo "Starting copyAssets...\n";
46
47
        $source = realpath(__DIR__ . '/../assets');
48
        if ($source === false) {
49
            echo "Source directory does not exist.\n";
50
            return;
51
        }
52
53
        $target = realpath(__DIR__ . '/../../../../public/alxarafe');
54
        if ($target === false) {
55
            $target = __DIR__ . '/../../../../public/alxarafe';
56
            if (!mkdir($target, 0777, true) && !is_dir($target)) {
57
                echo "Failed to create target directory: $target\n";
58
                return;
59
            }
60
        }
61
62
        echo "Copying assets from $source to $target...\n";
63
        if (!static::copyFolder($source, $target)) {
64
            echo "An error has ocurred copying Assets.\n";
65
            return;
66
        }
67
        echo "Assets copied successfully.\n";
68
    }
69
70
    private static function copyFolder(string $source, string $target): bool
71
    {
72
        $result = true;
73
74
        $dir = opendir($source);
75
76
        while (false !== ($file = readdir($dir))) {
77
            if (in_array($file, ['.', '..'])) {
78
                continue;
79
            }
80
81
            $sourcePath = $source . '/' . $file;
82
            $targetPath = $target . '/' . $file;
83
84
            if (is_dir($sourcePath)) {
85
                if (!static::copyFolder($sourcePath, $targetPath)) {
86
                    echo "\nError copying $sourcePath folder to $targetPath\n";
87
                    $result = false;
88
                }
89
                continue;
90
            }
91
92
            if (!copy($sourcePath, $targetPath)) {
93
                echo "\nError copying $sourcePath to $targetPath\n";
94
                $result = false;
95
            }
96
        }
97
98
        closedir($dir);
99
100
        return $result;
101
    }
102
}