Passed
Push — main ( be663c...41e3ba )
by Rafael
04:48 queued 15s
created

ComposerScripts::postUpdateFromScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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