18
18
import com .magento .idea .magento2plugin .magento .files .commands .SaveEntityCommandFile ;
19
19
import com .magento .idea .magento2plugin .magento .packages .code .ExceptionType ;
20
20
import com .magento .idea .magento2plugin .magento .packages .code .FrameworkLibraryType ;
21
- import com .magento .idea .magento2plugin .magento .packages .code .PhpCoreType ;
22
21
import com .magento .idea .magento2plugin .util .GetPhpClassByFQN ;
22
+ import java .util .ArrayList ;
23
23
import java .util .LinkedList ;
24
24
import java .util .List ;
25
25
import java .util .Properties ;
@@ -33,6 +33,7 @@ public class SaveEntityCommandGenerator extends FileGenerator {
33
33
private final DirectoryGenerator directoryGenerator ;
34
34
private final ModuleIndex moduleIndex ;
35
35
private final boolean checkFileAlreadyExists ;
36
+ private final List <String > uses ;
36
37
37
38
/**
38
39
* Save entity command generator constructor.
@@ -66,6 +67,7 @@ public SaveEntityCommandGenerator(
66
67
fileFromTemplateGenerator = FileFromTemplateGenerator .getInstance (project );
67
68
directoryGenerator = DirectoryGenerator .getInstance ();
68
69
moduleIndex = ModuleIndex .getInstance (project );
70
+ uses = new ArrayList <>();
69
71
}
70
72
71
73
/**
@@ -94,7 +96,7 @@ public PsiFile generate(final @NotNull String actionName) {
94
96
);
95
97
96
98
return fileFromTemplateGenerator .generate (
97
- SaveEntityCommandFile . getInstance (),
99
+ new SaveEntityCommandFile (),
98
100
getAttributes (),
99
101
saveCommandFileBaseDir ,
100
102
actionName
@@ -108,44 +110,48 @@ public PsiFile generate(final @NotNull String actionName) {
108
110
*/
109
111
@ Override
110
112
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 ());
116
121
117
122
final String dtoType = saveEntityCommandData .getDataModelClassFqn ();
118
- uses .add (dtoType );
119
- attributes .setProperty ("DTO" , PhpClassGeneratorUtil .getNameFromFqn (dtoType ));
123
+ addProperty (attributes , "DTO" , dtoType );
120
124
121
125
final String dtoProperty = CaseFormat .UPPER_CAMEL .to (
122
126
CaseFormat .LOWER_CAMEL , saveEntityCommandData .getEntityName ()
123
127
);
124
128
attributes .setProperty ("DTO_PROPERTY" , dtoProperty );
125
129
126
130
final String modelType = saveEntityCommandData .getModelClassFqn ();
127
- uses .add (modelType );
128
- attributes .setProperty ("MODEL" , PhpClassGeneratorUtil .getNameFromFqn (modelType ));
131
+ addProperty (attributes , "MODEL" , modelType );
129
132
130
133
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 );
135
135
136
136
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 );
148
138
149
139
attributes .setProperty ("USES" , PhpClassGeneratorUtil .formatUses (uses ));
150
140
}
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
+ }
151
157
}
0 commit comments