Passed
Push — master ( ea4f63...197c27 )
by Paul
20:35 queued 09:52
created

ChangeLogLevel::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Commands;
4
5
use GeminiLabs\SiteReviews\Contracts\CommandContract as Contract;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
use GeminiLabs\SiteReviews\Modules\Console;
8
use GeminiLabs\SiteReviews\Modules\Notice;
9
use GeminiLabs\SiteReviews\Request;
10
11
class ChangeLogLevel implements Contract
12
{
13
    /**
14
     * @var int
15
     */
16
    protected $level;
17
18
    /**
19
     * @var array
20
     */
21
    protected $levels = [
22
        Console::DEBUG,
23
        Console::INFO,
24
        Console::NOTICE,
25
        Console::WARNING,
26
    ];
27
28
    public function __construct(Request $request)
29
    {
30
        $this->level = is_numeric($request->level)
31
            ? Cast::toInt($request->level)
32
            : -1; // invalid level!
33
    }
34
35
    /**
36
     * @return void
37
     */
38
    public function handle()
39
    {
40
        if (in_array($this->level, $this->levels)) {
41
            update_option(Console::LOG_LEVEL_KEY, $this->level);
42
            glsr(Notice::class)->addSuccess(
43
                sprintf(_x('Console logging has been set to: Level %s', 'admin-text', 'site-reviews'), $this->level)
44
            );
45
            return;
46
        }
47
        glsr(Notice::class)->addError(
48
            _x('Console logging level could not be changed.', 'admin-text', 'site-reviews')
49
        );
50
    }
51
}
52