Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — main ( d3a2ba...cb4bdb )
by Pedro
34:31 queued 19:34
created

collectRelativeFiles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 8
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 17
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Console\Commands\Upgrade\v7\Steps;
4
5
use Backpack\CRUD\app\Console\Commands\Upgrade\Step;
6
use Backpack\CRUD\app\Console\Commands\Upgrade\StepResult;
7
use Illuminate\Filesystem\Filesystem;
8
9
class CheckFileManagerPublishedViewsStep extends Step
10
{
11
    private const LEGACY_VIEWS_DIRECTORY = 'resources/views/vendor/elfinder';
12
13
    /**
14
     * @var array<int, string>
15
     */
16
    protected array $legacyFiles = [];
17
18
    protected bool $legacyDirectoryDetected = false;
19
20
    public function title(): string
21
    {
22
        return 'Check if File Manager has published views';
23
    }
24
25
    public function run(): StepResult
26
    {
27
        $this->legacyFiles = [];
28
        $this->legacyDirectoryDetected = false;
29
30
        if (! $this->context()->hasComposerPackage('backpack/filemanager')) {
31
            return StepResult::success('File Manager add-on not detected; no published views to review.');
32
        }
33
34
        if (! $this->context()->fileExists(self::LEGACY_VIEWS_DIRECTORY)) {
35
            return StepResult::success('No File Manager views found in resources/views/vendor/elfinder.');
36
        }
37
38
        $filesystem = new Filesystem();
39
        $absoluteDirectory = $this->context()->basePath(self::LEGACY_VIEWS_DIRECTORY);
40
41
        if (! $filesystem->isDirectory($absoluteDirectory)) {
42
            return StepResult::success('No File Manager views found in resources/views/vendor/elfinder.');
43
        }
44
45
        $this->legacyDirectoryDetected = true;
46
47
        $this->legacyFiles = $this->collectRelativeFiles($filesystem, $absoluteDirectory);
48
49
        if (empty($this->legacyFiles)) {
50
            return StepResult::warning(
51
                'File Manager directory detected. Delete resources/views/vendor/elfinder if you have not customized those views.',
52
            );
53
        }
54
55
        return StepResult::warning(
56
            'File Manager views detected. Delete resources/views/vendor/elfinder if you have not customized those views.',
57
        );
58
    }
59
60
    public function canFix(StepResult $result): bool
61
    {
62
        if (! $result->status->isWarning()) {
63
            return false;
64
        }
65
66
        return $this->legacyDirectoryDetected;
67
    }
68
69
    public function fixMessage(StepResult $result): string
70
    {
71
        return 'We will delete resources/views/vendor/elfinder views. Proceed?';
72
    }
73
74
    public function fix(StepResult $result): StepResult
75
    {
76
        if (! $this->legacyDirectoryDetected) {
77
            return StepResult::skipped('Legacy File Manager directory no longer present.');
78
        }
79
80
        foreach ($this->legacyFiles as $path) {
81
            if (! $this->context()->deleteFile($path)) {
82
                return StepResult::failure("Could not delete {$path} automatically.");
83
            }
84
        }
85
86
        $filesystem = new Filesystem();
87
        $absoluteDirectory = $this->context()->basePath(self::LEGACY_VIEWS_DIRECTORY);
88
89
        if ($filesystem->isDirectory($absoluteDirectory) && ! $filesystem->deleteDirectory($absoluteDirectory)) {
90
            return StepResult::failure('Could not delete resources/views/vendor/elfinder automatically.');
91
        }
92
93
        $this->legacyFiles = [];
94
        $this->legacyDirectoryDetected = false;
95
96
        return StepResult::success('Removed resources/views/vendor/elfinder so the default package views are used.');
97
    }
98
99
    protected function collectRelativeFiles(Filesystem $filesystem, string $absoluteDirectory): array
100
    {
101
        $files = [];
102
103
        foreach ($filesystem->allFiles($absoluteDirectory) as $file) {
104
            $realPath = $file->getRealPath();
105
106
            if ($realPath === false) {
107
                continue;
108
            }
109
110
            $files[] = $this->context()->relativePath($realPath);
111
        }
112
113
        sort($files);
114
115
        return $files;
116
    }
117
}
118