Skip to content

Commit 31cf3a2

Browse files
Code refactoring after CR
1 parent 42f855b commit 31cf3a2

File tree

4 files changed

+33
-72
lines changed

4 files changed

+33
-72
lines changed

src/com/magento/idea/magento2plugin/actions/generation/generator/SaveEntityCommandGenerator.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import com.magento.idea.magento2plugin.magento.files.commands.SaveEntityCommandFile;
1919
import com.magento.idea.magento2plugin.magento.packages.code.ExceptionType;
2020
import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType;
21-
import com.magento.idea.magento2plugin.magento.packages.code.PhpCoreType;
2221
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
22+
import java.util.ArrayList;
2323
import java.util.LinkedList;
2424
import java.util.List;
2525
import java.util.Properties;
@@ -33,6 +33,7 @@ public class SaveEntityCommandGenerator extends FileGenerator {
3333
private final DirectoryGenerator directoryGenerator;
3434
private final ModuleIndex moduleIndex;
3535
private final boolean checkFileAlreadyExists;
36+
private final List<String> uses;
3637

3738
/**
3839
* Save entity command generator constructor.
@@ -66,6 +67,7 @@ public SaveEntityCommandGenerator(
6667
fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project);
6768
directoryGenerator = DirectoryGenerator.getInstance();
6869
moduleIndex = ModuleIndex.getInstance(project);
70+
uses = new ArrayList<>();
6971
}
7072

7173
/**
@@ -94,7 +96,7 @@ public PsiFile generate(final @NotNull String actionName) {
9496
);
9597

9698
return fileFromTemplateGenerator.generate(
97-
SaveEntityCommandFile.getInstance(),
99+
new SaveEntityCommandFile(),
98100
getAttributes(),
99101
saveCommandFileBaseDir,
100102
actionName
@@ -108,44 +110,48 @@ public PsiFile generate(final @NotNull String actionName) {
108110
*/
109111
@Override
110112
protected void fillAttributes(final @NotNull Properties attributes) {
111-
final List<String> uses = new LinkedList<>();
112-
uses.add(PhpCoreType.EXCEPTION.getType());
113-
uses.add(FrameworkLibraryType.DATA_OBJECT.getType());
114-
uses.add(ExceptionType.COULD_NOT_SAVE.getType());
115-
uses.add(FrameworkLibraryType.LOGGER.getType());
113+
attributes.setProperty("ENTITY_NAME", saveEntityCommandData.getEntityName());
114+
attributes.setProperty("NAMESPACE", saveEntityCommandData.getNamespace());
115+
attributes.setProperty("CLASS_NAME", SaveEntityCommandFile.CLASS_NAME);
116+
attributes.setProperty("EXCEPTION", "Exception");
117+
uses.add("Exception");
118+
addProperty(attributes, "DATA_OBJECT", FrameworkLibraryType.DATA_OBJECT.getType());
119+
addProperty(attributes, "COULD_NOT_SAVE", ExceptionType.COULD_NOT_SAVE.getType());
120+
addProperty(attributes, "LOGGER", FrameworkLibraryType.LOGGER.getType());
116121

117122
final String dtoType = saveEntityCommandData.getDataModelClassFqn();
118-
uses.add(dtoType);
119-
attributes.setProperty("DTO", PhpClassGeneratorUtil.getNameFromFqn(dtoType));
123+
addProperty(attributes, "DTO", dtoType);
120124

121125
final String dtoProperty = CaseFormat.UPPER_CAMEL.to(
122126
CaseFormat.LOWER_CAMEL, saveEntityCommandData.getEntityName()
123127
);
124128
attributes.setProperty("DTO_PROPERTY", dtoProperty);
125129

126130
final String modelType = saveEntityCommandData.getModelClassFqn();
127-
uses.add(modelType);
128-
attributes.setProperty("MODEL", PhpClassGeneratorUtil.getNameFromFqn(modelType));
131+
addProperty(attributes, "MODEL", modelType);
129132

130133
final String modelFactoryType = modelType.concat("Factory");
131-
uses.add(modelFactoryType);
132-
attributes.setProperty("MODEL_FACTORY",
133-
PhpClassGeneratorUtil.getNameFromFqn(modelFactoryType)
134-
);
134+
addProperty(attributes, "MODEL_FACTORY", modelFactoryType);
135135

136136
final String resourceType = saveEntityCommandData.getResourceModelClassFqn();
137-
uses.add(resourceType);
138-
attributes.setProperty("RESOURCE", PhpClassGeneratorUtil.getNameFromFqn(resourceType));
139-
140-
attributes.setProperty("ENTITY_NAME", saveEntityCommandData.getEntityName());
141-
attributes.setProperty("NAMESPACE", saveEntityCommandData.getNamespace());
142-
attributes.setProperty("CLASS_NAME", SaveEntityCommandFile.CLASS_NAME);
143-
144-
attributes.setProperty("EXCEPTION", PhpCoreType.EXCEPTION.getType());
145-
attributes.setProperty("DATA_OBJECT", FrameworkLibraryType.DATA_OBJECT.getTypeName());
146-
attributes.setProperty("COULD_NOT_SAVE", ExceptionType.COULD_NOT_SAVE.getTypeName());
147-
attributes.setProperty("LOGGER", FrameworkLibraryType.LOGGER.getTypeName());
137+
addProperty(attributes, "RESOURCE", resourceType);
148138

149139
attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses));
150140
}
141+
142+
/**
143+
* Add type to property list.
144+
*
145+
* @param properties Properties
146+
* @param propertyName String
147+
* @param type String
148+
*/
149+
protected void addProperty(
150+
final @NotNull Properties properties,
151+
final String propertyName,
152+
final String type
153+
) {
154+
uses.add(type);
155+
properties.setProperty(propertyName, PhpClassGeneratorUtil.getNameFromFqn(type));
156+
}
151157
}

src/com/magento/idea/magento2plugin/magento/files/commands/SaveEntityCommandFile.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ public class SaveEntityCommandFile implements ModuleFileInterface {
1717
public static final String FILE_EXTENSION = "php";
1818
public static final String TEMPLATE = "Magento Save Entity Command Model";
1919
private static final String DIRECTORY = "Command";
20-
private static final SaveEntityCommandFile INSTANCE = new SaveEntityCommandFile();
21-
22-
/**
23-
* Get singleton object of save command file.
24-
*
25-
* @return SaveEntityCommandFile
26-
*/
27-
public static SaveEntityCommandFile getInstance() {
28-
return INSTANCE;
29-
}
3020

3121
/**
3222
* Get namespace.

src/com/magento/idea/magento2plugin/magento/packages/code/PhpCoreType.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

tests/com/magento/idea/magento2plugin/actions/generation/generator/SaveEntityCommandGeneratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void testGenerateSaveEntityCommandFile() {
3939
false
4040
);
4141
final String filePath = this.getFixturePath(
42-
SaveEntityCommandFile.getInstance().getFileName()
42+
new SaveEntityCommandFile().getFileName()
4343
);
4444

4545
assertGeneratedFileIsCorrect(

0 commit comments

Comments
 (0)