Passed
Push — main ( bb93f8...8f4e67 )
by Rafael
06:15 queued 01:33
created

ComposerScripts::copyFolder()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 38
rs 8.0555
cc 9
nc 9
nop 3
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
use Composer\Script\Event;
23
24
abstract class ComposerScripts
25
{
26
    public static function postUpdateFromScript(Event $event)
27
    {
28
        $composer = $event->getComposer();
29
        $localRepo = $composer->getRepositoryManager()->getLocalRepository();
30
31
        $packageEvent = new PackageEvent(
32
            $event->getName(),
33
            $composer,
34
            $event->getIO(),
35
            $event->isDevMode(),
36
            $localRepo,
37
            $composer->getRepositoryManager()->getLocalRepository(),
38
            []
39
        );
40
41
        static::postUpdate($packageEvent);
42
    }
43
44
    public static function postUpdate(PackageEvent $event)
45
    {
46
        $io = $event->getIO();
47
48
        $io->write("*** Starting assets update process");
49
50
        if (getenv('SKIP_COPY_ASSETS')) {
51
            echo "Prevented copyAssets in scrutinizer environment.\n";
52
            return;
53
        }
54
55
        // Perform operations here
56
        $io->write("Running post-update script");
57
58
        static::copyAssets($io);
59
    }
60
61
    private static function copyAssets($io)
62
    {
63
        $io->write("Starting copyAssets...");
64
        $io->write("Current directory: " . __DIR__);
65
66
        $source = realpath(__DIR__ . '/../../assets');
67
        if ($source === false) {
68
            $io->write("Source directory does not exist.");
69
            return;
70
        }
71
72
        $targetSource = __DIR__ . '/../../../../../public/alxarafe';
73
        $target = realpath($targetSource);
74
        if ($target === false) {
75
            if (!mkdir($target, 0777, true) && !is_dir($target)) {
76
                $io->write("Failed to create target directory: $target");
77
                return;
78
            }
79
        }
80
81
        $io->write("Copying assets from $source to $target...");
82
        if (!static::copyFolder($io, $source, $target)) {
83
            $io->write("An error has ocurred copying Assets.");
84
            return;
85
        }
86
        $io->write("Assets copied successfully.");
87
    }
88
89
    private static function copyFolder($io, string $source, string $target): bool
90
    {
91
        $result = true;
92
93
        $dir = opendir($source);
94
95
        while (false !== ($file = readdir($dir))) {
96
            if (in_array($file, ['.', '..'])) {
97
                continue;
98
            }
99
100
            $sourcePath = $source . '/' . $file;
101
            $targetPath = $target . '/' . $file;
102
103
            if (is_dir($sourcePath)) {
104
                if (!static::copyFolder($io, $sourcePath, $targetPath)) {
105
                    $io->write("\nError copying $sourcePath folder to $targetPath");
106
                    $result = false;
107
                }
108
                continue;
109
            }
110
111
            if (!is_dir($target)) {
112
                if (!mkdir($target, 0777, true) && !is_dir($target)) {
113
                    $io->write("Failed to create target directory: $target");
114
                    continue;
115
                }
116
            }
117
118
            if (!copy($sourcePath, $targetPath)) {
119
                $io->write("\nError copying $sourcePath to $targetPath");
120
                $result = false;
121
            }
122
        }
123
124
        closedir($dir);
125
126
        return $result;
127
    }
128
}