Completed
Push — master ( b4d6a5...352f6d )
by Basil
02:42
created

Command::selectModule()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 7.6666
c 0
b 0
f 0
cc 10
nc 18
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace luya\console;
4
5
use Yii;
6
use yii\helpers\StringHelper;
7
use yii\helpers\Inflector;
8
use luya\base\AdminModuleInterface;
9
use luya\base\CoreModuleInterface;
10
11
/**
12
 * Console Command base class.
13
 *
14
 * The main different to the `\luya\console\Controller` is by adding default options to each command like
15
 * the verbose and interactive properties you can always access and use. In addition there are some helper
16
 * methods commonly used to build wizzwards within command controllers.
17
 *
18
 * @author Basil Suter <[email protected]>
19
 * @since 1.0.0
20
 */
21
abstract class Command extends \luya\console\Controller
22
{
23
    /**
24
     * @var boolean Whether the verbose printing is enabled from options parameter or not.
25
     */
26
    public $verbose = false;
27
    
28
    /**
29
     * @var boolean Whether the command is in interactive mode or not, provided by option paremeters.
30
     */
31
    public $interactive = true;
32
    
33
    /**
34
     * Method to print informations directly when verbose is enabled.
35
     *
36
     * @param string $message
37
     * @param string $section
38
     */
39
    public function verbosePrint($message, $section = null)
40
    {
41
        if ($this->verbose) {
42
            $message = $this->printableMessage($message);
43
            $this->output(!empty($section) ? $section . ': ' . $message : $message);
44
        }
45
    }
46
    
47
    /**
48
     * @inheritdoc
49
     */
50
    public function options($actionID)
51
    {
52
        return ['verbose', 'interactive'];
53
    }
54
    
55
    // HELPER METHODS
56
57
    /**
58
     * Get selection list of all module types.
59
     *
60
     * @return string
61
     */
62
    public function selectModuleType()
63
    {
64
        return $this->select('What type of Module you want to create?', [
65
            'frontend' => 'Frontend Modules are used to render views.',
66
            'admin' => 'Admin Modules are used when the Data-Managment should be done inside the Administration area.',
67
        ]);
68
    }
69
    
70
    /**
71
     * Get selection list for console commands with defined options.
72
     *
73
     * @param array $options Define behavior of the module selector prompt, options are name-value pairs. The following options are available:
74
     *
75
     * - onlyAdmin: boolean, if enabled all not admin modules will not be included
76
     * - hideCore: boolean, if enabled all core modules (from luya dev team) will be hidden.
77
     *
78
     * @return string The name (ID) of the selected module.
79
     */
80
    public function selectModule(array $options = [])
81
    {
82
        $modules = [];
83
        foreach (Yii::$app->getModules() as $id => $object) {
84
            if (!$object instanceof \luya\base\Module) {
85
                continue;
86
            }
87
            if (isset($options['onlyAdmin']) && $options['onlyAdmin']) {
88
                if (!$object instanceof AdminModuleInterface) {
89
                    continue;
90
                }
91
            }
92
    
93
            if (isset($options['hideCore']) && $options['hideCore']) {
94
                if ($object instanceof CoreModuleInterface) {
95
                    continue;
96
                }
97
            }
98
            $modules[$id] = $id;
99
        }
100
    
101
        $text = (isset($options['text'])) ? $options['text'] : 'Please select a module:';
102
    
103
        return $this->select($text, $modules);
104
    }
105
    
106
    /**
107
     * Generates a class name with camelcase style and specific suffix, if not already provided
108
     *
109
     * @param string $string The name of the class, e.g.: hello_word would
110
     * @param string $suffix The suffix to append on the class name if not eixsts, e.g.: MySuffix
111
     * @return string The class name e.g. HelloWorldMySuffix
112
     */
113
    public function createClassName($string, $suffix = false)
114
    {
115
        $name = Inflector::camelize($string);
116
    
117
        if ($suffix !== false && StringHelper::endsWith($name, $suffix, false)) {
118
            $name = substr($name, 0, -(strlen($suffix)));
119
        }
120
    
121
        return $name . $suffix;
122
    }
123
    
124
    /**
125
     * Generates the LUYA text which all generator files should include.
126
     *
127
     * @param string $command The command which is used like `admin/crud/create``.
128
     * @return string The text to insert.
129
     */
130
    public function getGeneratorText($command)
131
    {
132
        return 'File has been created with `'.$command.'` command.';
133
    }
134
}
135