Passed
Pull Request — master (#110)
by Dmitriy
03:29
created

Command   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 39.58%

Importance

Changes 0
Metric Value
wmc 11
eloc 38
c 0
b 0
f 0
dl 0
loc 130
ccs 19
cts 48
cp 0.3958
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 36 1
A getControllerID() 0 4 1
A getControllerNamespace() 0 3 1
A getHints() 0 17 1
A getAttributeLabels() 0 10 1
A getAttributes() 0 10 1
A getControllerClass() 0 3 1
A getBaseClass() 0 3 1
A getViewsPath() 0 3 1
A getDirectory() 0 3 1
A getActions() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Generator\Controller;
6
7
use Yiisoft\Strings\Inflector;
8
use Yiisoft\Strings\StringHelper;
9
use Yiisoft\Validator\Rule\Each;
10
use Yiisoft\Validator\Rule\Regex;
11
use Yiisoft\Validator\Rule\Required;
12
use Yiisoft\Yii\Gii\Generator\AbstractGeneratorCommand;
13
use Yiisoft\Yii\Gii\Validator\NewClassRule;
14
use Yiisoft\Yii\Gii\Validator\TemplateRule;
15
16
final class Command extends AbstractGeneratorCommand
17
{
18
    #[Each([
19
        new Regex(
20
            pattern: '/^[a-z][a-z0-9]*$/',
21
            message: 'Only a-z, 0-9, dashes (-), spaces and commas are allowed.'
22
        ),
23
    ])]
24
    private readonly array $actions;
25
26 3
    public function __construct(
27
        #[Required]
28
        #[Regex(
29
            pattern: '/^(?:[a-z][a-z0-9]*)(?:\\\\[a-z][a-z0-9]*)*$/i',
30
            message: 'Invalid namespace'
31
        )]
32
        private readonly string $controllerNamespace = 'App\\Controller',
33
        #[Required]
34
        #[Regex(
35
            pattern: '/^[A-Z][a-zA-Z0-9]*Controller$/',
36
            message: 'Only word characters are allowed, and the class name must start with a capital letter and end with "Controller".'
37
        )]
38
        #[NewClassRule]
39
        private readonly string $controllerClass = 'IndexController',
40
        /**
41
         * @var string the controller path
42
         */
43
        private readonly string $controllerPath = '@src/Controller',
44
        /**
45
         * @var string the controller's views path
46
         */
47
        private readonly string $viewsPath = '@views/',
48
        #[Regex(
49
            pattern: '/^[a-z\\\\]*$/i',
50
            message: 'Only word characters and backslashes are allowed.',
51
            skipOnEmpty: true,
52
        )]
53
        private readonly string $baseClass = '',
54
        array $actions = ['index'],
55
        #[Required(message: 'A code template must be selected.')]
56
        #[TemplateRule]
57
        protected string $template = 'default',
58
    ) {
59 3
        parent::__construct($template);
60 3
        sort($actions);
61 3
        $this->actions = $actions;
0 ignored issues
show
Bug introduced by
The property actions is declared read-only in Yiisoft\Yii\Gii\Generator\Controller\Command.
Loading history...
62
    }
63
64
    /**
65
     * @return string the controller ID
66
     */
67 2
    public function getControllerID(): string
68
    {
69 2
        $name = StringHelper::baseName($this->controllerClass);
70 2
        return (new Inflector())->pascalCaseToId(substr($name, 0, -10));
71
    }
72
73 2
    public function getControllerClass(): string
74
    {
75 2
        return $this->controllerClass;
76
    }
77
78 2
    public function getActions(): array
79
    {
80 2
        return $this->actions;
81
    }
82
83 2
    public function getViewsPath(): string
84
    {
85 2
        return $this->viewsPath;
86
    }
87
88 1
    public function getControllerNamespace(): string
89
    {
90 1
        return $this->controllerNamespace;
91
    }
92
93 1
    public function getBaseClass(): string
94
    {
95 1
        return $this->baseClass;
96
    }
97
98 2
    public function getDirectory(): string
99
    {
100 2
        return $this->controllerPath;
101
    }
102
103
    public static function getAttributeLabels(): array
104
    {
105
        return [
106
            'controllerNamespace' => 'Controller Namespace',
107
            'controllerClass' => 'Controller Class',
108
            'baseClass' => 'Base Class',
109
            'controllerPath' => 'Controller Path',
110
            'actions' => 'Action IDs',
111
            'viewsPath' => 'Views Path',
112
            'template' => 'Action IDs',
113
        ];
114
    }
115
116
    public static function getHints(): array
117
    {
118
        return [
119
            'controllerClass' => 'This is the name of the controller class to be generated. You should
120
                provide a fully qualified namespaced class (e.g. <code>App\Controller\PostController</code>),
121
                and class name should be in CamelCase ending with the word <code>Controller</code>. Make sure the class
122
                is using the same namespace as specified by your application\'s controllerNamespace property.',
123
            'actions' => 'Provide one or multiple action IDs to generate empty action method(s) in the controller. Separate multiple action IDs with commas or spaces.
124
                Action IDs should be in lower case. For example:
125
                <ul>
126
                    <li><code>index</code> generates <code>index()</code></li>
127
                    <li><code>create-order</code> generates <code>createOrder()</code></li>
128
                </ul>',
129
            'viewsPath' => 'Specify the directory for storing the view scripts for the controller. You may use path alias here, e.g.,
130
                <code>/var/www/app/controllers/views/order</code>, <code>@app/views/order</code>. If not set, it will default
131
                to <code>@app/views/ControllerID</code>',
132
            'baseClass' => 'This is the class that the new controller class will extend from. Please make sure the class exists and can be autoloaded.',
133
        ];
134
    }
135
136
    public static function getAttributes(): array
137
    {
138
        return [
139
            'controllerNamespace',
140
            'controllerClass',
141
            'baseClass',
142
            'viewsPath',
143
            'actions',
144
            'template',
145
            'controllerPath',
146
        ];
147
    }
148
}
149