1515import com .magento .idea .magento2plugin .util .RegExUtil ;
1616import javax .swing .JOptionPane ;
1717
18- public class CLICommandValidator {
19- private static CLICommandValidator INSTANCE = null ;
18+ public class NewCLICommandValidator {
19+ private static NewCLICommandValidator INSTANCE = null ;//NOPMD
2020 private final ValidatorBundle validatorBundle ;
2121 private final CommonBundle commonBundle ;
2222
@@ -25,15 +25,14 @@ public class CLICommandValidator {
2525 *
2626 * @return NewCLICommandValidator
2727 */
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 ();
3231
3332 return INSTANCE ;
3433 }
3534
36- public CLICommandValidator () {
35+ public NewCLICommandValidator () {
3736 this .validatorBundle = new ValidatorBundle ();
3837 this .commonBundle = new CommonBundle ();
3938 }
@@ -46,108 +45,125 @@ public CLICommandValidator() {
4645 * @return boolen
4746 */
4847 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 ;
7355 }
56+
57+ return false ;
7458 }
7559
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 (
8064 "validator.notEmpty" ,
81- this . commonBundle . message ( "common.description" )
65+ "common.className"
8266 );
83- this . showOptionPane ( errorMessage ) ;
67+ return false ;
8468 }
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"
9373 );
94- this . showOptionPane ( errorMessage ) ;
74+ return false ;
9575 }
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"
10082 );
101- this . showOptionPane ( errorMessage ) ;
83+ return false ;
10284 }
85+
86+ return true ;
10387 }
10488
105- private void validateParentDirectory (final NewCLICommandDialog dialog ) {
89+ private Boolean isParentDirectoryValid (final NewCLICommandDialog dialog ) {
10690 final String directory = dialog .getCLICommandParentDirectory ();
10791 if (directory .length () == 0 ) {
108- final String errorMessage = validatorBundle . message (
92+ this . showOptionPane (
10993 "validator.notEmpty" ,
110- this . commonBundle . message ( "common.parentDirectory" )
94+ "common.parentDirectory"
11195 );
112- this . showOptionPane ( errorMessage ) ;
96+ return false ;
11397 }
11498 if (!directory .matches (RegExUtil .DIRECTORY )) {
115- final String errorMessage = validatorBundle . message (
99+ this . showOptionPane (
116100 "validator.directory.isNotValid" ,
117- this . commonBundle . message ( "common.parentDirectory" )
101+ "common.parentDirectory"
118102 );
119- this . showOptionPane ( errorMessage ) ;
103+ return false ;
120104 }
105+
106+ return true ;
121107 }
122108
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 (
127113 "validator.notEmpty" ,
128- this . commonBundle . message ( "common.className" )
114+ "common.cliCommandName"
129115 );
130- this . showOptionPane ( errorMessage ) ;
116+ return false ;
131117 }
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"
136122 );
137- this . showOptionPane ( errorMessage ) ;
123+ return false ;
138124 }
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"
145135 );
146- this . showOptionPane ( errorMessage ) ;
136+ return false ;
147137 }
138+
139+ return true ;
148140 }
149141
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+ );
151167 JOptionPane .showMessageDialog (
152168 null ,
153169 errorMessage ,
0 commit comments