Test Failed
Push — main ( 7fcb13...cd154d )
by Rafael
12:11
created

ComposerScripts::copyAssets()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 29
rs 9.0111
c 0
b 0
f 0
cc 6
nc 5
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 Rsanjoseo\Alxarafe\Scripts;
20
21
use Composer\Script\Event;
22
23
abstract class ComposerScripts
24
{
25
    public static function postUpdate(Event $event)
26
    {
27
        $composer = $event->getComposer();
0 ignored issues
show
Unused Code introduced by
The assignment to $composer is dead and can be removed.
Loading history...
28
        $io = $event->getIO();
29
30
        // Perform operations here
31
        $io->write("Running post-update script");
32
33
        static::copyAssets();
34
    }
35
36
    private static function copyAssets()
37
    {
38
        echo "Starting copyAssets...\n";
39
40
        if (getenv('SKIP_COPY_ASSETS')) {
41
            echo "Prevented copyAssets in scrutinizer environment.\n";
42
            return;
43
        }
44
45
        $source = realpath(__DIR__ . '/../assets');
46
        if ($source === false) {
47
            echo "Source directory does not exist.\n";
48
            return;
49
        }
50
51
        $target = realpath(__DIR__ . '/../../../../public/alxarafe');
52
        if ($target === false) {
53
            $target = __DIR__ . '/../../../../public/alxarafe';
54
            if (!mkdir($target, 0777, true) && !is_dir($target)) {
55
                echo "Failed to create target directory: $target\n";
56
                return;
57
            }
58
        }
59
60
        echo "Copying assets from $source to $target...\n";
61
        static::copyAssetsFolder($source, $target, 'css');
62
        static::copyAssetsFolder($source, $target, 'js');
63
        static::copyAssetsFolder($source, $target, 'img');
64
        echo "Assets copied successfully.\n";
65
    }
66
67
    private static function copyAssetsFolder($baseDir, $publicDir, $extension)
68
    {
69
        $dir = $baseDir . '/assets/' . $extension;
70
        if (!is_dir($dir)) {
71
            echo "Directory $dir does not exist.\n";
72
            return;
73
        }
74
75
        $targetDir = $publicDir . '/' . $extension;
76
        if (!is_dir($targetDir)) {
77
            if (!mkdir($targetDir, 0777, true) && !is_dir($targetDir)) {
78
                echo "Failed to create directory: $targetDir\n";
79
                return;
80
            }
81
        }
82
83
        foreach (glob($dir . '/*.' . $extension) as $file) {
84
            if (!copy($file, $publicDir . '/' . $extension . '/' . basename($file))) {
85
                echo "Failed to copy $file to $targetDir.\n";
86
                return;
87
            }
88
        }
89
90
        echo "Directory $dir copied successfully.\n";
91
    }
92
}