Skip to content

Commit 07bcd41

Browse files
committed
Validator logic changes and style adjustments
1 parent 5aa70e6 commit 07bcd41

File tree

5 files changed

+222
-172
lines changed

5 files changed

+222
-172
lines changed

resources/magento2/common.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ common.cliCommandName=CLI Command Name
3232
common.cli.create.new.cli.command.title=Create a new Magento 2 CLI Command
3333
common.cli.generate.error=New CLI Command Generation Error
3434
common.cli.class.title=CLI Command Class
35+
common.validationErrorTitle=Validation Error

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewCLICommandDialog.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,38 @@
66

77
import com.intellij.openapi.project.Project;
88
import com.intellij.psi.PsiDirectory;
9-
import com.jetbrains.php.lang.psi.elements.PhpClass;
109
import com.magento.idea.magento2plugin.actions.generation.NewCLICommandAction;
1110
import com.magento.idea.magento2plugin.actions.generation.data.CLICommandClassData;
1211
import com.magento.idea.magento2plugin.actions.generation.data.CLICommandXmlData;
1312
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.NewCLICommandValidator;
1413
import com.magento.idea.magento2plugin.actions.generation.generator.CLICommandClassGenerator;
1514
import com.magento.idea.magento2plugin.actions.generation.generator.CLICommandDiXmlGenerator;
1615
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
17-
import com.magento.idea.magento2plugin.magento.packages.File;
18-
import com.magento.idea.magento2plugin.magento.packages.Package;
1916
import com.magento.idea.magento2plugin.util.CamelCaseToSnakeCase;
20-
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
2117
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectory;
22-
2318
import javax.swing.*;
2419
import java.awt.event.*;
2520

2621
public class NewCLICommandDialog extends AbstractDialog {
2722
private JPanel contentPane;
28-
private JTextField cliCommandClassNameField;
29-
private JTextField cliCommandParentDirectoryField;
30-
private JTextField cliCommandNameField;
31-
private JTextArea cliCommandDescriptionField;
23+
private JTextField cliCommandClassNameField;//NOPMD
24+
private JTextField cliCommandParentDirectoryField;//NOPMD
25+
private JTextField cliCommandNameField;//NOPMD
26+
private JTextArea cliCommandDescriptionField;//NOPMD
3227
private JButton buttonCancel;
3328
private JButton buttonOK;
3429

3530
private final Project project;
3631
private final String moduleName;
3732
private final NewCLICommandValidator validator;
38-
private final CamelCaseToSnakeCase camelCaseToSnakeCase;
33+
private final CamelCaseToSnakeCase toSnakeCase;
3934

40-
public NewCLICommandDialog(Project project, PsiDirectory directory) {
35+
public NewCLICommandDialog(final Project project, final PsiDirectory directory) {
36+
super();
4137
this.project = project;
4238
this.moduleName = GetModuleNameByDirectory.getInstance(project).execute(directory);
43-
this.validator = NewCLICommandValidator.getInstance();
44-
this.camelCaseToSnakeCase = CamelCaseToSnakeCase.getInstance();
39+
this.validator = new NewCLICommandValidator();
40+
this.toSnakeCase = CamelCaseToSnakeCase.getInstance();
4541

4642
setContentPane(contentPane);
4743
setModal(true);
@@ -53,20 +49,28 @@ public NewCLICommandDialog(Project project, PsiDirectory directory) {
5349

5450
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
5551
addWindowListener(new WindowAdapter() {
56-
public void windowClosing(WindowEvent e) {
52+
@Override
53+
public void windowClosing(final WindowEvent event) {
5754
onCancel();
5855
}
5956
});
6057

6158
contentPane.registerKeyboardAction(new ActionListener() {
62-
public void actionPerformed(ActionEvent e) {
59+
@Override
60+
public void actionPerformed(final ActionEvent event) {
6361
onCancel();
6462
}
6563
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
6664
}
6765

68-
public static void open(Project project, PsiDirectory directory) {
69-
NewCLICommandDialog dialog = new NewCLICommandDialog(project, directory);
66+
/**
67+
* Open a new CLI command dialog
68+
*
69+
* @param project Project
70+
* @param directory Directory
71+
*/
72+
public static void open(final Project project, final PsiDirectory directory) {
73+
final NewCLICommandDialog dialog = new NewCLICommandDialog(project, directory);
7074

7175
dialog.pack();
7276
dialog.centerDialog(dialog);
@@ -90,17 +94,17 @@ public String getCLICommandDescription() {
9094
}
9195

9296
public String getDIXmlItemName() {
93-
String cliCommandClassNameToSnakeCase = this.camelCaseToSnakeCase.convert(this.getCLICommandClassName());
97+
final String diItemName = this.toSnakeCase.convert(this.getCLICommandClassName());
9498

95-
return this.moduleName.toLowerCase() + "_" + cliCommandClassNameToSnakeCase;
99+
return this.moduleName.toLowerCase() + "_" + diItemName;
96100
}
97101

98102
public String getCLICommandModule() {
99103
return this.moduleName;
100104
}
101105

102106
public String getCLICommandClassFqn() {
103-
NamespaceBuilder namespaceBuilder = new NamespaceBuilder(
107+
final NamespaceBuilder namespaceBuilder = new NamespaceBuilder(
104108
moduleName,
105109
this.getCLICommandClassName(),
106110
this.getCLICommandParentDirectory()
@@ -131,15 +135,15 @@ private void generate() {
131135
}
132136

133137
private void generateCLICommandClass() {
134-
CLICommandClassData cliCommandClassData = this.initializeCLICommandClassData();
135-
CLICommandClassGenerator cliCommandClassGenerator = new CLICommandClassGenerator(project, cliCommandClassData);
136-
cliCommandClassGenerator.generate(NewCLICommandAction.ACTION_NAME, true);
138+
final CLICommandClassData dataClass = this.initializeCLICommandClassData();
139+
final CLICommandClassGenerator classGenerator = new CLICommandClassGenerator(project, dataClass);
140+
classGenerator.generate(NewCLICommandAction.ACTION_NAME, true);
137141
}
138142

139143
private void generateCLICommandDiXmlRegistration() {
140-
CLICommandXmlData cliCommandXmlData = this.initializeCLICommandDIXmlData();
141-
CLICommandDiXmlGenerator cliCommandDiXmlGenerator = new CLICommandDiXmlGenerator(project, cliCommandXmlData);
142-
cliCommandDiXmlGenerator.generate(NewCLICommandAction.ACTION_NAME);
144+
final CLICommandXmlData cliCommandXmlData = this.initializeCLICommandDIXmlData();
145+
final CLICommandDiXmlGenerator diGenerator = new CLICommandDiXmlGenerator(project, cliCommandXmlData);
146+
diGenerator.generate(NewCLICommandAction.ACTION_NAME);
143147
}
144148

145149
private CLICommandXmlData initializeCLICommandDIXmlData() {
@@ -162,7 +166,7 @@ private CLICommandClassData initializeCLICommandClassData() {
162166
}
163167

164168
private String getCLICommandNamespace() {
165-
NamespaceBuilder namespaceBuilder = new NamespaceBuilder(
169+
final NamespaceBuilder namespaceBuilder = new NamespaceBuilder(
166170
this.getCLICommandModule(),
167171
this.getCLICommandClassName(),
168172
this.getCLICommandParentDirectory()

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

Lines changed: 93 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import com.magento.idea.magento2plugin.util.RegExUtil;
1616
import 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

Comments
 (0)