ComposerScripts::copyFolder()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 40
rs 8.4444
c 4
b 0
f 0
cc 8
nc 8
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 postUpdate(PackageEvent $event)
27
    {
28
        $io = $event->getIO();
29
30
        $io->write("*** Starting assets update process");
31
32
        if (getenv('SKIP_COPY_ASSETS')) {
33
            echo "Prevented copyAssets in scrutinizer environment.\n";
34
            return;
35
        }
36
37
        // Perform operations here
38
        $io->write("Running post-update script");
39
40
        static::copyAssets($io);
41
    }
42
43
    private static function copyAssets($io)
44
    {
45
        $io->write("Starting copyAssets...");
46
        $io->write("Current directory: " . __DIR__);
47
48
        $source = realpath(__DIR__ . '/../../assets');
49
        $io->write("Assets directory: " . $source);
50
        if ($source === false) {
51
            $io->write("Source directory does not exist.");
52
            return;
53
        }
54
55
        $public = realpath(__DIR__ . '/../../../../../public');
56
        $io->write("Public directory: " . $public);
57
58
        $target = $public . '/alxarafe/assets';
59
        if (!static::makeDir($io, $target)) {
60
            return;
61
        }
62
        $io->write("Target directory: " . $target);
63
64
        $io->write("Copying assets from $source to $target...");
65
        if (!static::copyFolder($io, $source, $target)) {
66
            $io->write("An error has ocurred copying Assets.");
67
            return;
68
        }
69
        $io->write("Assets copied successfully.");
70
    }
71
72
    private static function makeDir($io, $path)
73
    {
74
        if (is_dir($path)) {
75
            return true;
76
        }
77
78
        if (!mkdir($path, 0777, true) && !is_dir($path)) {
79
            $io->write("Failed to create target directory: $path");
80
            return false;
81
        }
82
        return true;
83
    }
84
85
    private static function copyFolder($io, string $source, string $target): bool
86
    {
87
        if (!static::makeDir($io, $target)) {
88
            return false;
89
        }
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::makeDir($io, $targetPath)) {
105
                    return false;
106
                }
107
108
                if (!static::copyFolder($io, $sourcePath, $targetPath)) {
109
                    $io->write("\nError copying $sourcePath folder to $targetPath");
110
                    $result = false;
111
                }
112
                continue;
113
            }
114
115
            $io->write("\nCopying $sourcePath to $targetPath");
116
            if (!copy($sourcePath, $targetPath)) {
117
                $io->write("\nError copying $sourcePath to $targetPath");
118
                $result = false;
119
            }
120
        }
121
122
        closedir($dir);
123
124
        return $result;
125
    }
126
}