1818import com .magento .idea .magento2plugin .magento .files .commands .SaveEntityCommandFile ;
1919import com .magento .idea .magento2plugin .magento .packages .code .ExceptionType ;
2020import com .magento .idea .magento2plugin .magento .packages .code .FrameworkLibraryType ;
21- import com .magento .idea .magento2plugin .magento .packages .code .PhpCoreType ;
2221import com .magento .idea .magento2plugin .util .GetPhpClassByFQN ;
22+ import java .util .ArrayList ;
2323import java .util .LinkedList ;
2424import java .util .List ;
2525import 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}
0 commit comments