Passed
Push — main ( 8f4e67...ec9833 )
by Rafael
04:43
created

ComposerScripts::makeDir()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 4
nc 3
nop 2
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 makeDir($io, $path) {
44
        if ($path === false) {
45
            if (!mkdir($path, 0777, true) && !is_dir($path)) {
46
                $io->write("Failed to create target directory: $path");
47
                return false;
48
            }
49
        }
50
        return true;
51
    }
52
53
    private static function copyAssets($io)
54
    {
55
        $io->write("Starting copyAssets...");
56
        $io->write("Current directory: " . __DIR__);
57
58
        $source = realpath(__DIR__ . '/../../assets');
59
        if ($source === false) {
60
            $io->write("Source directory does not exist.");
61
            return;
62
        }
63
64
        $targetSource = __DIR__ . '/../../../../../public/alxarafe';
65
        $target = realpath($targetSource);
66
        if (!static::makeDir($target)) {
0 ignored issues
show
Bug introduced by
The call to Rsanjoseo\Alxarafe\ComposerScripts::makeDir() has too few arguments starting with path. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        if (!static::/** @scrutinizer ignore-call */ makeDir($target)) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
67
            return;
68
        }
69
70
        $io->write("Copying assets from $source to $target...");
71
        if (!static::copyFolder($io, $source, $target)) {
72
            $io->write("An error has ocurred copying Assets.");
73
            return;
74
        }
75
        $io->write("Assets copied successfully.");
76
    }
77
78
    private static function copyFolder($io, string $source, string $target): bool
79
    {
80
        $result = true;
81
82
        $dir = opendir($source);
83
84
        while (false !== ($file = readdir($dir))) {
85
            if (in_array($file, ['.', '..']) || !static::makeDir($io, $target)) {
86
                continue;
87
            }
88
89
            $sourcePath = $source . '/' . $file;
90
            $targetPath = $target . '/' . $file;
91
92
            if (is_dir($sourcePath)) {
93
                if (!static::copyFolder($io, $sourcePath, $targetPath)) {
94
                    $io->write("\nError copying $sourcePath folder to $targetPath");
95
                    $result = false;
96
                }
97
                continue;
98
            }
99
100
            if (!copy($sourcePath, $targetPath)) {
101
                $io->write("\nError copying $sourcePath to $targetPath");
102
                $result = false;
103
            }
104
        }
105
106
        closedir($dir);
107
108
        return $result;
109
    }
110
}