15
15
import com .magento .idea .magento2plugin .util .RegExUtil ;
16
16
import javax .swing .JOptionPane ;
17
17
18
- public class CLICommandValidator {
19
- private static CLICommandValidator INSTANCE = null ;
18
+ public class NewCLICommandValidator {
19
+ private static NewCLICommandValidator INSTANCE = null ;//NOPMD
20
20
private final ValidatorBundle validatorBundle ;
21
21
private final CommonBundle commonBundle ;
22
22
@@ -25,15 +25,14 @@ public class CLICommandValidator {
25
25
*
26
26
* @return NewCLICommandValidator
27
27
*/
28
- public static CLICommandValidator getInstance () {
29
- if (null == INSTANCE ) {
30
- INSTANCE = new CLICommandValidator ();
31
- }
28
+ public static NewCLICommandValidator getInstance () {
29
+ if (null != INSTANCE ) return INSTANCE ;//NOPMD
30
+ INSTANCE = new NewCLICommandValidator ();
32
31
33
32
return INSTANCE ;
34
33
}
35
34
36
- public CLICommandValidator () {
35
+ public NewCLICommandValidator () {
37
36
this .validatorBundle = new ValidatorBundle ();
38
37
this .commonBundle = new CommonBundle ();
39
38
}
@@ -46,108 +45,125 @@ public CLICommandValidator() {
46
45
* @return boolen
47
46
*/
48
47
public boolean validate (final Project project , final NewCLICommandDialog dialog ) {
49
- this .validateClassName (dialog );
50
- this .validateParentDirectory (dialog );
51
- this .validateCommandName (dialog );
52
- this .validateCommandDescription (dialog );
53
- this .validatePHPClassName (project , dialog );
54
-
55
- return true ;
56
- }
57
-
58
- private void validatePHPClassName (final Project project , final NewCLICommandDialog dialog ) {
59
- final String moduleName = dialog .getCLICommandModule ();
60
- final NamespaceBuilder namespaceBuilder = new NamespaceBuilder (
61
- moduleName ,
62
- dialog .getCLICommandClassName (),
63
- dialog .getCLICommandParentDirectory ()
64
- );
65
- final String namespace = namespaceBuilder .getClassFqn ();
66
- final PhpClass phpClass = GetPhpClassByFQN .getInstance (project ).execute (namespace );
67
- if (phpClass != null ) {
68
- final String errorMessage = validatorBundle .message (
69
- "validator.file.alreadyExists" ,
70
- this .commonBundle .message ("common.cli.class.title" )
71
- );
72
- this .showOptionPane (errorMessage );
48
+ if (this .isClassNameValid (dialog )
49
+ && this .isParentDirectoryValid (dialog )
50
+ && this .isCommandNameValid (dialog )
51
+ && this .isCommandDescriptionValid (dialog )
52
+ && this .isPHPClassValid (project , dialog )
53
+ ) {
54
+ return true ;
73
55
}
56
+
57
+ return false ;
74
58
}
75
59
76
- private void validateCommandDescription (final NewCLICommandDialog dialog ) {
77
- final String description = dialog .getCLICommandDescription ();
78
- if (description .length () == 0 ) {
79
- final String errorMessage = validatorBundle . message (
60
+ private Boolean isClassNameValid (final NewCLICommandDialog dialog ) {
61
+ final String className = dialog .getCLICommandClassName ();
62
+ if (className .length () == 0 ) {
63
+ this . showOptionPane (
80
64
"validator.notEmpty" ,
81
- this . commonBundle . message ( "common.description" )
65
+ "common.className"
82
66
);
83
- this . showOptionPane ( errorMessage ) ;
67
+ return false ;
84
68
}
85
- }
86
-
87
- private void validateCommandName (final NewCLICommandDialog dialog ) {
88
- final String cliCommandName = dialog .getCLICommandName ();
89
- if (cliCommandName .length () == 0 ) {
90
- final String errorMessage = validatorBundle .message (
91
- "validator.notEmpty" ,
92
- this .commonBundle .message ("common.cliCommandName" )
69
+ if (!className .matches (RegExUtil .ALPHANUMERIC )) {
70
+ this .showOptionPane (
71
+ "validator.alphaNumericCharacters" ,
72
+ "common.className"
93
73
);
94
- this . showOptionPane ( errorMessage ) ;
74
+ return false ;
95
75
}
96
- if (!cliCommandName .matches (RegExUtil .CLI_COMMAND_NAME )) {
97
- final String errorMessage = validatorBundle .message (
98
- "validator.directory.isNotValid" ,
99
- this .commonBundle .message ("common.cliCommandName" )
76
+ if (!Character .isUpperCase (className .charAt (0 ))
77
+ && !Character .isDigit (className .charAt (0 ))
78
+ ) {
79
+ this .showOptionPane (
80
+ "validator.startWithNumberOrCapitalLetter" ,
81
+ "common.className"
100
82
);
101
- this . showOptionPane ( errorMessage ) ;
83
+ return false ;
102
84
}
85
+
86
+ return true ;
103
87
}
104
88
105
- private void validateParentDirectory (final NewCLICommandDialog dialog ) {
89
+ private Boolean isParentDirectoryValid (final NewCLICommandDialog dialog ) {
106
90
final String directory = dialog .getCLICommandParentDirectory ();
107
91
if (directory .length () == 0 ) {
108
- final String errorMessage = validatorBundle . message (
92
+ this . showOptionPane (
109
93
"validator.notEmpty" ,
110
- this . commonBundle . message ( "common.parentDirectory" )
94
+ "common.parentDirectory"
111
95
);
112
- this . showOptionPane ( errorMessage ) ;
96
+ return false ;
113
97
}
114
98
if (!directory .matches (RegExUtil .DIRECTORY )) {
115
- final String errorMessage = validatorBundle . message (
99
+ this . showOptionPane (
116
100
"validator.directory.isNotValid" ,
117
- this . commonBundle . message ( "common.parentDirectory" )
101
+ "common.parentDirectory"
118
102
);
119
- this . showOptionPane ( errorMessage ) ;
103
+ return false ;
120
104
}
105
+
106
+ return true ;
121
107
}
122
108
123
- private void validateClassName (final NewCLICommandDialog dialog ) {
124
- final String className = dialog .getCLICommandClassName ();
125
- if (className .length () == 0 ) {
126
- final String errorMessage = validatorBundle . message (
109
+ private Boolean isCommandNameValid (final NewCLICommandDialog dialog ) {
110
+ final String cliCommandName = dialog .getCLICommandName ();
111
+ if (cliCommandName .length () == 0 ) {
112
+ this . showOptionPane (
127
113
"validator.notEmpty" ,
128
- this . commonBundle . message ( "common.className" )
114
+ "common.cliCommandName"
129
115
);
130
- this . showOptionPane ( errorMessage ) ;
116
+ return false ;
131
117
}
132
- if (!className .matches (RegExUtil .ALPHANUMERIC )) {
133
- final String errorMessage = validatorBundle . message (
134
- "validator.alphaNumericCharacters " ,
135
- this . commonBundle . message ( "common.className" )
118
+ if (!cliCommandName .matches (RegExUtil .CLI_COMMAND_NAME )) {
119
+ this . showOptionPane (
120
+ "validator.directory.isNotValid " ,
121
+ "common.cliCommandName"
136
122
);
137
- this . showOptionPane ( errorMessage ) ;
123
+ return false ;
138
124
}
139
- if (!Character .isUpperCase (className .charAt (0 ))
140
- && !Character .isDigit (className .charAt (0 ))
141
- ) {
142
- final String errorMessage = validatorBundle .message (
143
- "validator.startWithNumberOrCapitalLetter" ,
144
- this .commonBundle .message ("common.className" )
125
+
126
+ return true ;
127
+ }
128
+
129
+ private Boolean isCommandDescriptionValid (final NewCLICommandDialog dialog ) {
130
+ final String description = dialog .getCLICommandDescription ();
131
+ if (description .length () == 0 ) {
132
+ this .showOptionPane (
133
+ "validator.notEmpty" ,//NOPMD
134
+ "common.description"
145
135
);
146
- this . showOptionPane ( errorMessage ) ;
136
+ return false ;
147
137
}
138
+
139
+ return true ;
148
140
}
149
141
150
- private void showOptionPane (final String errorMessage ) {
142
+ private Boolean isPHPClassValid (final Project project , final NewCLICommandDialog dialog ) {
143
+ final String moduleName = dialog .getCLICommandModule ();
144
+ final NamespaceBuilder namespaceBuilder = new NamespaceBuilder (
145
+ moduleName ,
146
+ dialog .getCLICommandClassName (),
147
+ dialog .getCLICommandParentDirectory ()
148
+ );
149
+ final String namespace = namespaceBuilder .getClassFqn ();
150
+ final PhpClass phpClass = GetPhpClassByFQN .getInstance (project ).execute (namespace );
151
+ if (phpClass != null ) {
152
+ this .showOptionPane (
153
+ "validator.file.alreadyExists" ,
154
+ "common.cli.class.title"
155
+ );
156
+ return false ;
157
+ }
158
+
159
+ return true ;
160
+ }
161
+
162
+ private void showOptionPane (final String key , final String resourceKey ) {
163
+ final String errorMessage = validatorBundle .message (
164
+ key ,
165
+ this .commonBundle .message (resourceKey )
166
+ );
151
167
JOptionPane .showMessageDialog (
152
168
null ,
153
169
errorMessage ,
0 commit comments