Passed
Push — main ( 8c05f9...b8035b )
by Rafael
04:37
created

ComposerScripts::copyAssets()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 26
rs 9.0777
cc 6
nc 6
nop 1
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;
20
21
use Composer\Installer\PackageEvent;
22
23
abstract class ComposerScripts
24
{
25
    public static function postUpdate(PackageEvent $event)
26
    {
27
        $io = $event->getIO();
28
29
        $io->write("*** Starting assets update process");
30
31
        if (getenv('SKIP_COPY_ASSETS')) {
32
            echo "Prevented copyAssets in scrutinizer environment.\n";
33
            return;
34
        }
35
36
        // Perform operations here
37
        $io->write("Running post-update script");
38
39
        static::copyAssets($io);
40
    }
41
42
    private static function copyAssets($io)
43
    {
44
        $io->write("Starting copyAssets...");
45
        $io->write("Current directory: " . __DIR__);
46
47
        $source = realpath(__DIR__ . '/../assets');
48
        if ($source === false) {
49
            $io->write("Source directory does not exist.");
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
                $io->write("Failed to create target directory: $target");
58
                return;
59
            }
60
        }
61
62
        $io->write("Copying assets from $source to $target...");
63
        if (!static::copyFolder($io, $source, $target)) {
64
            $io->write("An error has ocurred copying Assets.");
65
            return;
66
        }
67
        $io->write("Assets copied successfully.");
68
    }
69
70
    private static function copyFolder($io, 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($io, $sourcePath, $targetPath)) {
86
                    $io->write("\nError copying $sourcePath folder to $targetPath");
87
                    $result = false;
88
                }
89
                continue;
90
            }
91
92
            if (!copy($sourcePath, $targetPath)) {
93
                $io->write("\nError copying $sourcePath to $targetPath");
94
                $result = false;
95
            }
96
        }
97
98
        closedir($dir);
99
100
        return $result;
101
    }
102
}