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 — add-update-command ( 60d3c1...c664f4 )
by Pedro
14:19
created

CheckFileManagerPublishedViewsStep::run()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 28
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 46
rs 9.1608
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 'Legacy File Manager 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 legacy 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 legacy File Manager views found in resources/views/vendor/elfinder.');
43
        }
44
45
        $this->legacyDirectoryDetected = true;
46
47
        $relativeFiles = $this->collectRelativeFiles($filesystem, $absoluteDirectory);
48
        $this->legacyFiles = $relativeFiles;
49
50
        if (empty($relativeFiles)) {
51
            return StepResult::warning(
52
                'Legacy File Manager directory detected. Delete resources/views/vendor/elfinder if you have not customized those views.',
53
                ['Published directory: '.self::LEGACY_VIEWS_DIRECTORY]
54
            );
55
        }
56
57
        $details = array_merge(
58
            ['Published directory: '.self::LEGACY_VIEWS_DIRECTORY],
59
            $this->previewList(
60
                $relativeFiles,
61
                10,
62
                static fn (string $path): string => "- {$path}",
63
                '... %d more file(s) omitted.'
64
            )
65
        );
66
67
        return StepResult::warning(
68
            'Legacy File Manager views detected. Delete resources/views/vendor/elfinder if you have not customized those views.',
69
            $details,
70
            ['paths' => $relativeFiles]
71
        );
72
    }
73
74
    public function canFix(StepResult $result): bool
75
    {
76
        if (! $result->status->isWarning()) {
77
            return false;
78
        }
79
80
        return $this->legacyDirectoryDetected;
81
    }
82
83
    public function fixMessage(StepResult $result): string
84
    {
85
        return 'We can delete resources/views/vendor/elfinder so Backpack uses the updated File Manager views. Proceed?';
86
    }
87
88
    public function fix(StepResult $result): StepResult
89
    {
90
        if (! $this->legacyDirectoryDetected) {
91
            return StepResult::skipped('Legacy File Manager directory no longer present.');
92
        }
93
94
        foreach ($this->legacyFiles as $path) {
95
            if (! $this->context()->deleteFile($path)) {
96
                return StepResult::failure("Could not delete {$path} automatically.");
97
            }
98
        }
99
100
        $filesystem = new Filesystem();
101
        $absoluteDirectory = $this->context()->basePath(self::LEGACY_VIEWS_DIRECTORY);
102
103
        if ($filesystem->isDirectory($absoluteDirectory) && ! $filesystem->deleteDirectory($absoluteDirectory)) {
104
            return StepResult::failure('Could not delete resources/views/vendor/elfinder automatically.');
105
        }
106
107
        $this->legacyFiles = [];
108
        $this->legacyDirectoryDetected = false;
109
110
        return StepResult::success('Removed resources/views/vendor/elfinder so the package views are used.');
111
    }
112
113
    protected function collectRelativeFiles(Filesystem $filesystem, string $absoluteDirectory): array
114
    {
115
        $files = [];
116
117
        foreach ($filesystem->allFiles($absoluteDirectory) as $file) {
118
            $realPath = $file->getRealPath();
119
120
            if ($realPath === false) {
121
                continue;
122
            }
123
124
            $files[] = $this->context()->relativePath($realPath);
125
        }
126
127
        sort($files);
128
129
        return $files;
130
    }
131
}
132