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 ( 1611a2...0fa6fa )
by Pedro
13:26
created

CheckOperationConfigFilesStep::fixMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 Backpack\CRUD\app\Console\Commands\Upgrade\UpgradeContext;
8
use Backpack\CRUD\app\Console\Commands\Upgrade\Support\ConfigFilesHelper;
9
10
class CheckOperationConfigFilesStep extends Step
11
{   
12
    protected ConfigFilesHelper $configs;
13
14
    public function __construct(UpgradeContext $context)
15
    {
16
        parent::__construct($context);
17
18
        $this->configs = new ConfigFilesHelper(
19
            $context,
20
            config_path('backpack/operations'),
21
            base_path('vendor/backpack/crud/src/config/backpack/operations')
22
        );
23
    }
24
25
    public function title(): string
26
    {
27
        return 'Check if operation config files are published and up to date';
28
    }
29
30
    public function run(): StepResult
31
    {
32
        $issues = [];
33
34
        if (! $this->configs->configFilesPublished()) {
35
            return StepResult::skipped('Operation config files are not published.');
36
        }
37
38
        foreach ($this->configs->missingKeysPerFile() as $relativePath => $missingKeys) {
39
            if (empty($missingKeys)) {
40
                continue;
41
            }
42
43
            $issues[] = sprintf('Add the missing keys to %s:', $relativePath);
44
            $issues = array_merge($issues, $this->previewList($missingKeys));
45
        }
46
47
        if (empty($issues)) {
48
            return StepResult::success('Published operation config are up to date with package version.');
49
        }
50
51
        return StepResult::warning(
52
            'Copy the new configuration options into your published operation config files so you can keep up to date with the latest features.',
53
            $issues,
54
            [
55
                'missing_entries' => $this->configs->collectedEntries(),
56
                'missing_entries_per_file' => $this->configs->topLevelMissingKeysPerFile(),
57
            ]
58
        );
59
    }
60
61
    public function canFix(StepResult $result): bool
62
    {
63
        if (! $result->status->isWarning()) {
64
            return false;
65
        }
66
67
        return ! empty($this->configs->topLevelEntriesPerFile());
68
    }
69
70
    public function fixMessage(StepResult $result): string
71
    {
72
        return 'Add the missing configuration keys to your published operation config files?';
73
    }
74
75
    public function fix(StepResult $result): StepResult
76
    {
77
        $entriesPerFile = $this->configs->topLevelEntriesPerFile();
78
        $absolutePaths = $this->configs->absolutePaths();
79
80
        if (empty($entriesPerFile)) {
81
            return StepResult::skipped('No missing configuration keys detected to apply automatically.');
82
        }
83
84
        $updatedFiles = [];
85
86
        foreach ($entriesPerFile as $displayPath => $entries) {
87
            if (empty($entries)) {
88
                continue;
89
            }
90
91
            $error = null;
92
93
            $absolutePath = $absolutePaths[$displayPath] ?? null;
94
95
            if ($absolutePath === null) {
96
                return StepResult::failure("Could not locate {$displayPath} on disk.");
97
            }
98
99
            if (! $this->configs->addEntriesToPublishedFile($absolutePath, $entries, $error)) {
100
                return StepResult::failure($error ?? "Could not update {$displayPath} automatically.");
101
            }
102
103
            $updatedFiles[] = $displayPath;
104
        }
105
106
        if (empty($updatedFiles)) {
107
            return StepResult::skipped('No missing configuration keys were eligible for automatic updates.');
108
        }
109
110
        $details = $this->previewList($updatedFiles, limit: count($updatedFiles));
111
112
        return StepResult::success('Added missing operation configuration keys automatically.', $details);
113
    }
114
}
115