Skip to content

Commit 5aa70e6

Browse files
committed
Fix some style and logic
1 parent a28be65 commit 5aa70e6

File tree

1 file changed

+102
-82
lines changed

1 file changed

+102
-82
lines changed

src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/NewCLICommandValidator.java

Lines changed: 102 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,137 +2,157 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.actions.generation.dialog.validator;
67

78
import com.intellij.openapi.project.Project;
89
import com.jetbrains.php.lang.psi.elements.PhpClass;
910
import com.magento.idea.magento2plugin.actions.generation.dialog.NewCLICommandDialog;
1011
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
11-
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
1212
import com.magento.idea.magento2plugin.bundles.CommonBundle;
13+
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
1314
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
1415
import com.magento.idea.magento2plugin.util.RegExUtil;
16+
import javax.swing.JOptionPane;
1517

16-
import javax.swing.*;
17-
18-
public class NewCLICommandValidator {
19-
private static NewCLICommandValidator INSTANCE = null;
18+
public class CLICommandValidator {
19+
private static CLICommandValidator INSTANCE = null;
2020
private final ValidatorBundle validatorBundle;
2121
private final CommonBundle commonBundle;
2222

23-
public static NewCLICommandValidator getInstance() {
23+
/**
24+
* Returns a new instance of a class.
25+
*
26+
* @return NewCLICommandValidator
27+
*/
28+
public static CLICommandValidator getInstance() {
2429
if (null == INSTANCE) {
25-
INSTANCE = new NewCLICommandValidator();
30+
INSTANCE = new CLICommandValidator();
2631
}
2732

2833
return INSTANCE;
2934
}
3035

31-
public NewCLICommandValidator() {
36+
public CLICommandValidator() {
3237
this.validatorBundle = new ValidatorBundle();
3338
this.commonBundle = new CommonBundle();
3439
}
3540

36-
public boolean validate(Project project, NewCLICommandDialog dialog) {
37-
String errorTitle = "Validation Error";
38-
String cliCommandClassName = dialog.getCLICommandClassName();
39-
String cliCommandParentDirectory = dialog.getCLICommandParentDirectory();
40-
String cliCommandName = dialog.getCLICommandName();
41-
String cliCommandDescription = dialog.getCLICommandDescription();
42-
String moduleName = dialog.getCLICommandModule();
43-
44-
if (cliCommandClassName.length() == 0) {
45-
String errorMessage = validatorBundle.message(
46-
"validator.notEmpty",
47-
this.commonBundle.message("common.className")
48-
);
49-
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
41+
/**
42+
* Validate new CLI command form data.
43+
*
44+
* @param project Project
45+
* @param dialog Dialog
46+
* @return boolen
47+
*/
48+
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);
5054

51-
return false;
52-
}
53-
if (!cliCommandClassName.matches(RegExUtil.ALPHANUMERIC)) {
54-
String errorMessage = validatorBundle.message(
55-
"validator.alphaNumericCharacters",
56-
this.commonBundle.message("common.className")
57-
);
58-
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
55+
return true;
56+
}
5957

60-
return false;
61-
}
62-
if (!Character.isUpperCase(cliCommandClassName.charAt(0)) && !Character.isDigit(cliCommandClassName.charAt(0))) {
63-
String errorMessage = validatorBundle.message(
64-
"validator.startWithNumberOrCapitalLetter",
65-
this.commonBundle.message("common.className")
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")
6671
);
67-
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
68-
69-
return false;
72+
this.showOptionPane(errorMessage);
7073
}
74+
}
7175

72-
if (cliCommandParentDirectory.length() == 0) {
73-
String errorMessage = validatorBundle.message(
76+
private void validateCommandDescription(final NewCLICommandDialog dialog) {
77+
final String description = dialog.getCLICommandDescription();
78+
if (description.length() == 0) {
79+
final String errorMessage = validatorBundle.message(
7480
"validator.notEmpty",
75-
this.commonBundle.message("common.parentDirectory")
76-
);
77-
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
78-
79-
return false;
80-
}
81-
if (!cliCommandParentDirectory.matches(RegExUtil.DIRECTORY)) {
82-
String errorMessage = validatorBundle.message(
83-
"validator.directory.isNotValid",
84-
this.commonBundle.message("common.parentDirectory")
81+
this.commonBundle.message("common.description")
8582
);
86-
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
87-
88-
return false;
83+
this.showOptionPane(errorMessage);
8984
}
85+
}
9086

87+
private void validateCommandName(final NewCLICommandDialog dialog) {
88+
final String cliCommandName = dialog.getCLICommandName();
9189
if (cliCommandName.length() == 0) {
92-
String errorMessage = validatorBundle.message(
90+
final String errorMessage = validatorBundle.message(
9391
"validator.notEmpty",
9492
this.commonBundle.message("common.cliCommandName")
9593
);
96-
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
97-
98-
return false;
94+
this.showOptionPane(errorMessage);
9995
}
10096
if (!cliCommandName.matches(RegExUtil.CLI_COMMAND_NAME)) {
101-
String errorMessage = validatorBundle.message(
97+
final String errorMessage = validatorBundle.message(
10298
"validator.directory.isNotValid",
10399
this.commonBundle.message("common.cliCommandName")
104100
);
105-
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
106-
107-
return false;
101+
this.showOptionPane(errorMessage);
108102
}
103+
}
109104

110-
if (cliCommandDescription.length() == 0) {
111-
String errorMessage = validatorBundle.message(
105+
private void validateParentDirectory(final NewCLICommandDialog dialog) {
106+
final String directory = dialog.getCLICommandParentDirectory();
107+
if (directory.length() == 0) {
108+
final String errorMessage = validatorBundle.message(
112109
"validator.notEmpty",
113-
this.commonBundle.message("common.description")
110+
this.commonBundle.message("common.parentDirectory")
114111
);
115-
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
116-
117-
return false;
112+
this.showOptionPane(errorMessage);
118113
}
119-
120-
NamespaceBuilder namespaceBuilder = new NamespaceBuilder(
121-
moduleName,
122-
cliCommandClassName,
123-
cliCommandParentDirectory
124-
);
125-
PhpClass cliCommandPHPClass = GetPhpClassByFQN.getInstance(project).execute(namespaceBuilder.getClassFqn());
126-
if (cliCommandPHPClass != null) {
127-
String errorMessage = validatorBundle.message(
128-
"validator.file.alreadyExists",
129-
"CLI Command Class"
114+
if (!directory.matches(RegExUtil.DIRECTORY)) {
115+
final String errorMessage = validatorBundle.message(
116+
"validator.directory.isNotValid",
117+
this.commonBundle.message("common.parentDirectory")
130118
);
131-
JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE);
119+
this.showOptionPane(errorMessage);
120+
}
121+
}
132122

133-
return false;
123+
private void validateClassName(final NewCLICommandDialog dialog) {
124+
final String className = dialog.getCLICommandClassName();
125+
if (className.length() == 0) {
126+
final String errorMessage = validatorBundle.message(
127+
"validator.notEmpty",
128+
this.commonBundle.message("common.className")
129+
);
130+
this.showOptionPane(errorMessage);
131+
}
132+
if (!className.matches(RegExUtil.ALPHANUMERIC)) {
133+
final String errorMessage = validatorBundle.message(
134+
"validator.alphaNumericCharacters",
135+
this.commonBundle.message("common.className")
136+
);
137+
this.showOptionPane(errorMessage);
134138
}
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")
145+
);
146+
this.showOptionPane(errorMessage);
147+
}
148+
}
135149

136-
return true;
150+
private void showOptionPane(final String errorMessage) {
151+
JOptionPane.showMessageDialog(
152+
null,
153+
errorMessage,
154+
this.commonBundle.message("common.validationErrorTitle"),
155+
JOptionPane.ERROR_MESSAGE
156+
);
137157
}
138158
}

0 commit comments

Comments
 (0)