|
2 | 2 | * Copyright © Magento, Inc. All rights reserved.
|
3 | 3 | * See COPYING.txt for license details.
|
4 | 4 | */
|
| 5 | + |
5 | 6 | package com.magento.idea.magento2plugin.actions.generation.dialog.validator;
|
6 | 7 |
|
7 | 8 | import com.intellij.openapi.project.Project;
|
8 | 9 | import com.jetbrains.php.lang.psi.elements.PhpClass;
|
9 | 10 | import com.magento.idea.magento2plugin.actions.generation.dialog.NewCLICommandDialog;
|
10 | 11 | import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
|
11 |
| -import com.magento.idea.magento2plugin.bundles.ValidatorBundle; |
12 | 12 | import com.magento.idea.magento2plugin.bundles.CommonBundle;
|
| 13 | +import com.magento.idea.magento2plugin.bundles.ValidatorBundle; |
13 | 14 | import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
|
14 | 15 | import com.magento.idea.magento2plugin.util.RegExUtil;
|
| 16 | +import javax.swing.JOptionPane; |
15 | 17 |
|
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; |
20 | 20 | private final ValidatorBundle validatorBundle;
|
21 | 21 | private final CommonBundle commonBundle;
|
22 | 22 |
|
23 |
| - public static NewCLICommandValidator getInstance() { |
| 23 | + /** |
| 24 | + * Returns a new instance of a class. |
| 25 | + * |
| 26 | + * @return NewCLICommandValidator |
| 27 | + */ |
| 28 | + public static CLICommandValidator getInstance() { |
24 | 29 | if (null == INSTANCE) {
|
25 |
| - INSTANCE = new NewCLICommandValidator(); |
| 30 | + INSTANCE = new CLICommandValidator(); |
26 | 31 | }
|
27 | 32 |
|
28 | 33 | return INSTANCE;
|
29 | 34 | }
|
30 | 35 |
|
31 |
| - public NewCLICommandValidator() { |
| 36 | + public CLICommandValidator() { |
32 | 37 | this.validatorBundle = new ValidatorBundle();
|
33 | 38 | this.commonBundle = new CommonBundle();
|
34 | 39 | }
|
35 | 40 |
|
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); |
50 | 54 |
|
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 | + } |
59 | 57 |
|
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") |
66 | 71 | );
|
67 |
| - JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE); |
68 |
| - |
69 |
| - return false; |
| 72 | + this.showOptionPane(errorMessage); |
70 | 73 | }
|
| 74 | + } |
71 | 75 |
|
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( |
74 | 80 | "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") |
85 | 82 | );
|
86 |
| - JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE); |
87 |
| - |
88 |
| - return false; |
| 83 | + this.showOptionPane(errorMessage); |
89 | 84 | }
|
| 85 | + } |
90 | 86 |
|
| 87 | + private void validateCommandName(final NewCLICommandDialog dialog) { |
| 88 | + final String cliCommandName = dialog.getCLICommandName(); |
91 | 89 | if (cliCommandName.length() == 0) {
|
92 |
| - String errorMessage = validatorBundle.message( |
| 90 | + final String errorMessage = validatorBundle.message( |
93 | 91 | "validator.notEmpty",
|
94 | 92 | this.commonBundle.message("common.cliCommandName")
|
95 | 93 | );
|
96 |
| - JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE); |
97 |
| - |
98 |
| - return false; |
| 94 | + this.showOptionPane(errorMessage); |
99 | 95 | }
|
100 | 96 | if (!cliCommandName.matches(RegExUtil.CLI_COMMAND_NAME)) {
|
101 |
| - String errorMessage = validatorBundle.message( |
| 97 | + final String errorMessage = validatorBundle.message( |
102 | 98 | "validator.directory.isNotValid",
|
103 | 99 | this.commonBundle.message("common.cliCommandName")
|
104 | 100 | );
|
105 |
| - JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE); |
106 |
| - |
107 |
| - return false; |
| 101 | + this.showOptionPane(errorMessage); |
108 | 102 | }
|
| 103 | + } |
109 | 104 |
|
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( |
112 | 109 | "validator.notEmpty",
|
113 |
| - this.commonBundle.message("common.description") |
| 110 | + this.commonBundle.message("common.parentDirectory") |
114 | 111 | );
|
115 |
| - JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE); |
116 |
| - |
117 |
| - return false; |
| 112 | + this.showOptionPane(errorMessage); |
118 | 113 | }
|
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") |
130 | 118 | );
|
131 |
| - JOptionPane.showMessageDialog(null, errorMessage, errorTitle, JOptionPane.ERROR_MESSAGE); |
| 119 | + this.showOptionPane(errorMessage); |
| 120 | + } |
| 121 | + } |
132 | 122 |
|
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); |
134 | 138 | }
|
| 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 | + } |
135 | 149 |
|
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 | + ); |
137 | 157 | }
|
138 | 158 | }
|
0 commit comments