GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 86dce3...98cf71 )
by Nur
02:40
created

Maintenance   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 88
dl 0
loc 199
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A optionTitle() 0 3 1
A optionMessage() 0 3 1
A optionLifetime() 0 3 1
A optionSwitch() 0 6 2
B execute() 0 77 6
A optionMode() 0 3 1
1
<?php
2
/**
3
 * This file is part of the O2System PHP Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Reactor\Cli\Commanders;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Cache\Item;
19
use O2System\Kernel\Cli\Commander;
20
use O2System\Kernel\Cli\Writers\Format;
21
22
/**
23
 * Class Maintenance
24
 *
25
 * @package O2System\Reactor\Cli\Commanders
26
 */
27
class Maintenance extends Commander
28
{
29
    /**
30
     * Maintenance::$commandVersion
31
     *
32
     * Command version.
33
     *
34
     * @var string
35
     */
36
    protected $commandVersion = '1.0.0';
37
38
    /**
39
     * Maintenance::$commandDescription
40
     *
41
     * Command description.
42
     *
43
     * @var string
44
     */
45
    protected $commandDescription = 'CLI_MAINTENANCE_DESC';
46
47
    /**
48
     * Maintenance::$commandOptions
49
     *
50
     * Command options.
51
     *
52
     * @var array
53
     */
54
    protected $commandOptions = [
55
        'mode'    => [
56
            'description' => 'CLI_MAINTENANCE_MODE_HELP',
57
            'required'    => true,
58
        ],
59
        'switch'  => [
60
            'description' => 'CLI_MAINTENANCE_SWITCH_HELP',
61
            'required'    => true,
62
        ],
63
        'title'   => [
64
            'description' => 'CLI_MAINTENANCE_TITLE_HELP',
65
            'required'    => false,
66
        ],
67
        'message' => [
68
            'description' => 'CLI_MAINTENANCE_MESSAGE_HELP',
69
            'required'    => false,
70
        ],
71
    ];
72
73
    /**
74
     * Maintenance::$optionSwitch
75
     *
76
     * Maintenance switch.
77
     *
78
     * @var string
79
     */
80
    protected $optionSwitch;
81
82
    /**
83
     * Maintenance::$optionMode
84
     *
85
     * Maintenance mode.
86
     *
87
     * @var string
88
     */
89
    protected $optionMode;
90
91
    /**
92
     * Maintenance::$optionLifetime
93
     *
94
     * Maintenance mode.
95
     *
96
     * @var string
97
     */
98
    protected $optionLifetime;
99
100
    /**
101
     * Maintenance::$optionTitle
102
     *
103
     * Maintenance title.
104
     *
105
     * @var string
106
     */
107
    protected $optionTitle;
108
109
    /**
110
     * Maintenance::$optionMessage
111
     *
112
     * Maintenance message.
113
     *
114
     * @var string
115
     */
116
    protected $optionMessage;
117
118
    // ------------------------------------------------------------------------
119
120
    public function optionSwitch($switch)
121
    {
122
        $switch = strtoupper($switch);
123
124
        if (in_array($switch, ['ON', 'OFF'])) {
125
            $this->optionSwitch = $switch;
126
        }
127
    }
128
129
    public function optionMode($mode)
130
    {
131
        $this->optionMode = $mode;
132
    }
133
134
    public function optionLifetime($lifetime)
135
    {
136
        $this->optionLifetime = (int)$lifetime;
137
    }
138
139
    public function optionTitle($title)
140
    {
141
        $this->optionTitle = trim($title);
142
    }
143
144
    public function optionMessage($message)
145
    {
146
        $this->optionMessage = $message;
147
    }
148
149
    public function execute()
150
    {
151
        $options = input()->get();
152
153
        if (empty($options)) {
154
            $_GET[ 'switch' ] = 'ON';
155
            $_GET[ 'mode' ] = 'default';
156
            $_GET[ 'lifetime' ] = 300;
157
            $_GET[ 'title' ] = language()->getLine(strtoupper('CLI_MAINTENANCE_TITLE'));
158
            $_GET[ 'message' ] = language()->getLine(strtoupper('CLI_MAINTENANCE_MESSAGE'));
159
        } else {
160
            $_GET[ 'mode' ] = 'default';
161
            $_GET[ 'lifetime' ] = 300;
162
            $_GET[ 'title' ] = language()->getLine(strtoupper('CLI_MAINTENANCE_TITLE'));
163
            $_GET[ 'message' ] = language()->getLine(strtoupper('CLI_MAINTENANCE_MESSAGE'));
164
        }
165
166
        parent::execute();
167
168
        if ($this->optionSwitch === 'ON') {
169
            if (cache()->hasItem('maintenance')) {
170
171
                $maintenanceInfo = cache()->getItem('maintenance')->get();
172
                output()->write(
0 ignored issues
show
Bug introduced by
The method write() does not exist on O2System\Kernel\Http\Output. ( Ignorable by Annotation )

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

172
                output()->/** @scrutinizer ignore-call */ write(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
173
                    (new Format())
174
                        ->setContextualClass(Format::DANGER)
175
                        ->setString(language()->getLine('CLI_MAINTENANCE_ALREADY_STARTED', [
176
                            $maintenanceInfo[ 'mode' ],
177
                            $maintenanceInfo[ 'datetime' ],
178
                            date('r', strtotime($maintenanceInfo[ 'datetime' ]) + $maintenanceInfo[ 'lifetime' ]),
179
                            $maintenanceInfo[ 'title' ],
180
                            $maintenanceInfo[ 'message' ],
181
                        ]))
182
                        ->setNewLinesAfter(1)
183
                );
184
            } else {
185
                output()->write(
186
                    (new Format())
187
                        ->setContextualClass(Format::WARNING)
188
                        ->setString(language()->getLine('CLI_MAINTENANCE_STARTED', [
189
                            $datetime = date('r'),
190
                            $this->optionLifetime,
191
                            $this->optionMode,
192
                            $this->optionTitle,
193
                            $this->optionMessage,
194
                        ]))
195
                        ->setNewLinesAfter(1)
196
                );
197
198
                cache()->save(new Item('maintenance', [
199
                    'datetime' => $datetime,
200
                    'lifetime' => $this->optionLifetime,
201
                    'mode'     => $this->optionMode,
202
                    'title'    => $this->optionTitle,
203
                    'message'  => $this->optionMessage,
204
                ], $this->optionLifetime));
0 ignored issues
show
Bug introduced by
$this->optionLifetime of type string is incompatible with the type DateInterval|integer expected by parameter $expiresAfter of O2System\Cache\Item::__construct(). ( Ignorable by Annotation )

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

204
                ], /** @scrutinizer ignore-type */ $this->optionLifetime));
Loading history...
205
            }
206
207
        } elseif ($this->optionSwitch === 'OFF') {
208
            if (cache()->hasItem('maintenance')) {
209
                output()->write(
210
                    (new Format())
211
                        ->setContextualClass(Format::DANGER)
212
                        ->setString(language()->getLine('CLI_MAINTENANCE_STOPPED', [
213
                            $this->optionMode,
214
                            date('r'),
215
                        ]))
216
                        ->setNewLinesAfter(1)
217
                );
218
219
                cache()->deleteItem('maintenance');
220
            } else {
221
                output()->write(
222
                    (new Format())
223
                        ->setContextualClass(Format::DANGER)
224
                        ->setString(language()->getLine('CLI_MAINTENANCE_INACTIVE'))
225
                        ->setNewLinesAfter(1)
226
                );
227
            }
228
        }
229
    }
230
}