ConsoleController::confirmAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
/**
3
 * Console controller
4
 *
5
 * @link https://github.com/inblank/yii2-activeuser
6
 * @copyright Copyright (c) 2016 Pavel Aleksandrov <[email protected]>
7
 * @license http://opensource.org/licenses/MIT
8
 */
9
10
namespace inblank\activeuser\components;
11
12
use yii;
13
use yii\helpers\Console;
14
15
/**
16
 * Class ConsoleController
17
 *
18
 * @package inblank\activeuser\traits
19
 */
20
class ConsoleController extends yii\console\Controller
21
{
22
    /**
23
     * Confirm action
24
     * @param string $message message for confirm
25
     * @param bool $default default selection
26
     * @throws yii\base\ExitException
27
     */
28
    public function confirmAction($message, $default = false)
29
    {
30
        if (!Console::confirm($message, $default)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \yii\helpers\Console::confirm($message, $default) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
31
            Console::error(Yii::t('activeuser_backend', 'Action canceled'));
32
            Yii::$app->end();
33
        }
34
    }
35
36
    /**
37
     * Show action usage
38
     * @param string $required required params separated by comma
39
     * @param string $optional optional params separated by comma
40
     * @param array $errors errors to output
41
     * @throws yii\base\ExitException
42
     */
43
    protected function showUsage($required = '', $optional = '', $errors = [])
44
    {
45
        $description = Yii::t('activeuser_backend', $this->getActionHelp($this->action));
46
        Console::output($description);
47
        Console::output(str_pad('', mb_strlen($description), '-'));
48
        $paramsString = [];
49 View Code Duplication
        if (!empty($required)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            foreach (explode(',', $required) as $param) {
51
                $paramsString[] = '<' . trim($param) . '>';
52
            }
53
        }
54 View Code Duplication
        if (!empty($optional)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
            foreach (explode(',', $optional) as $param) {
56
                $paramsString[] = '[' . trim($param) . ']';
57
            }
58
        }
59
        Console::output(
60
            Yii::t('activeuser_backend', 'Usage') . ': ' .
61
            Console::ansiFormat(
62
                'yii ' . $this->id . "/" . $this->action->id . ' ' .
63
                implode(' ', $paramsString),
64
                [Console::BOLD]
65
            )
66
        );
67
        Console::output();
68
        if (!empty($errors)) {
69
            $this->showErrors($errors);
70
        }
71
        yii::$app->end();
72
    }
73
74
    /**
75
     * Show errors
76
     * @param array $errors array of errors string
77
     * @throws yii\base\ExitException
78
     */
79
    protected function showErrors($errors)
80
    {
81
        foreach ((array)$errors as $err) {
82
            Console::error(Console::ansiFormat(Yii::t('activeuser_backend', "Error") . ": ", [Console::FG_RED]) . $err[0]);
83
        }
84
        yii::$app->end();
85
    }
86
87
}
88