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 ( 44cb20 )
by Cristian
14:39
created

CheckThemeTablerConfigStep::fixOptions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
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 Backpack\CRUD\app\Console\Commands\Upgrade\StepStatus;
8
use Backpack\CRUD\app\Console\Commands\Upgrade\Support\ConfigFilesHelper;
9
use Backpack\CRUD\app\Console\Commands\Upgrade\UpgradeContext;
10
11
class CheckThemeTablerConfigStep extends Step
12
{
13
    protected ConfigFilesHelper $configs;
14
15
    protected string $configFilename = 'theme-tabler.php';
16
17
    private ?string $currentContents = null;
18
19
    private bool $needsPublish = false;
20
21
    private ?string $selectedOption = null;
22
23
    private bool $acceptedNewStyle = false;
24
25
    public function __construct(UpgradeContext $context)
26
    {
27
        parent::__construct($context);
28
29
        $this->configs = new ConfigFilesHelper(
30
            $context,
31
            config_path('backpack/theme-tabler.php'),
32
            base_path('vendor/backpack/theme-tabler/config/theme-tabler.php')
33
        );
34
35
        $this->configs->setDefaultConfigFile($this->configFilename);
36
    }
37
38
    public function title(): string
39
    {
40
        return 'Check if Theme tabler config is published';
41
    }
42
43
    public function run(): StepResult
44
    {
45
        $this->needsPublish = false;
46
        $this->selectedOption = null;
47
        $this->currentContents = $this->configs->readPublishedFile($this->configFilename);
48
49
        if ($this->currentContents === null) {
50
            if ($this->acceptedNewStyle) {
51
                return StepResult::success('Using the Backpack v7 Tabler defaults without publishing the config.');
52
            }
53
54
            $this->needsPublish = true;
55
56
            return StepResult::warning(
57
                'Tabler theme config not published yet. Backpack v7 ships with a new tabler skin and layout.'
58
            );
59
        }
60
61
        return StepResult::success('Tabler theme config already published.');
62
    }
63
64
    public function canFix(StepResult $result): bool
65
    {
66
        return $result->status === StepStatus::Warning && $this->needsPublish;
67
    }
68
69
    public function fixMessage(StepResult $result): string
70
    {
71
        if ($this->needsPublish) {
72
            return 'Do you want to keep the OLD Backpack style?';
73
        }
74
75
        return 'Do you want to revert to v6 skin and layout?';
76
    }
77
78
    public function fixOptions(StepResult $result): array
79
    {
80
        if (! $this->needsPublish || $this->acceptedNewStyle) {
81
            return [];
82
        }
83
84
        return [
85
            [
86
                'key' => 'publish-old',
87
                'label' => 'Yes',
88
                'default' => true,
89
            ],
90
            [
91
                'key' => 'try-new',
92
                'label' => 'No',
93
            ],
94
        ];
95
    }
96
97
    public function selectFixOption(?string $option): void
98
    {
99
        $this->selectedOption = $option;
100
    }
101
102
    public function fix(StepResult $result): StepResult
103
    {
104
        if (! $this->needsPublish) {
105
            return StepResult::skipped('Tabler theme config already published.');
106
        }
107
108
        return $this->handleMissingConfigFix();
109
    }
110
111
    private function handleMissingConfigFix(): StepResult
112
    {
113
        $option = $this->selectedOption ?? 'publish-old';
114
115
        if ($option === 'try-new') {
116
            $this->acceptedNewStyle = true;
117
            $this->needsPublish = false;
118
            $this->currentContents = null;
119
            $this->selectedOption = null;
120
121
            return StepResult::success('No configuration file published.');
122
        }
123
124
        if ($option !== 'publish-old') {
125
            $this->selectedOption = null;
126
127
            return StepResult::skipped('No Tabler config changes applied.');
128
        }
129
130
        $packagePath = $this->configs->packageConfigPath($this->configFilename);
131
132
        if (! is_file($packagePath)) {
133
            return StepResult::failure('Could not publish config/backpack/theme-tabler.php automatically.');
134
        }
135
136
        $defaultContents = @file_get_contents($packagePath);
137
138
        if ($defaultContents === false) {
139
            return StepResult::failure('Could not read the default Tabler config to publish it automatically.');
140
        }
141
142
        if (! $this->configs->writePublishedFile($this->configFilename, $defaultContents)) {
143
            return StepResult::failure('Failed writing changes to config/backpack/theme-tabler.php.');
144
        }
145
146
        $this->needsPublish = false;
147
        $this->acceptedNewStyle = false;
148
        $this->selectedOption = null;
149
        $this->currentContents = $defaultContents;
150
151
        return StepResult::success('Published config/backpack/theme-tabler.php.');
152
    }
153
}
154