Skip to content

Commit 1714449

Browse files
Refactored eav attribute generator
1 parent c75f0d4 commit 1714449

File tree

5 files changed

+90
-218
lines changed

5 files changed

+90
-218
lines changed

resources/fileTemplates/internal/Magento Eav Attribute Data Patch Class.php.ft

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ${NAMESPACE};
1010
use $use;
1111
#end
1212

13-
class ${NAME} implements ${IMPLEMENTS} {
13+
class ${CLASS_NAME} implements ${IMPLEMENTS} {
1414

1515
/**
1616
* @var ${MODULE_DATA_SETUP_INTERFACE}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ private void generateSourceModelFile() {
300300
private void generateDataPatchFile() {
301301
new EavAttributeSetupPatchGenerator(
302302
populateProductEntityData(new ProductEntityData()),
303-
project
303+
project,
304+
true
304305
).generate(NewEavAttributeAction.ACTION_NAME, true);
305306
}
306307

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

Lines changed: 51 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -6,206 +6,84 @@
66
package com.magento.idea.magento2plugin.actions.generation.generator;
77

88
import com.intellij.openapi.project.Project;
9-
import com.intellij.psi.PsiDirectory;
10-
import com.intellij.psi.PsiFile;
11-
import com.jetbrains.php.lang.psi.PhpFile;
12-
import com.jetbrains.php.lang.psi.elements.PhpClass;
139
import com.magento.idea.magento2plugin.actions.generation.data.EavEntityDataInterface;
14-
import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
15-
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
1610
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
11+
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder;
1712
import com.magento.idea.magento2plugin.actions.generation.generator.util.eav.AttributeMapperFactory;
1813
import com.magento.idea.magento2plugin.actions.generation.generator.util.eav.AttributeMapperInterface;
19-
import com.magento.idea.magento2plugin.bundles.CommonBundle;
20-
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
21-
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
22-
import com.magento.idea.magento2plugin.magento.files.EavAttributeDataPatchPhp;
23-
import com.magento.idea.magento2plugin.magento.packages.File;
24-
import com.magento.idea.magento2plugin.magento.packages.Package;
14+
import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile;
15+
import com.magento.idea.magento2plugin.magento.files.EavAttributeDataPatchFile;
2516
import com.magento.idea.magento2plugin.magento.packages.eav.DataPatchDependency;
26-
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
27-
import com.sun.istack.NotNull;
28-
import java.util.ArrayList;
2917
import java.util.List;
3018
import java.util.Properties;
31-
import javax.swing.JOptionPane;
32-
import org.jetbrains.annotations.Nullable;
19+
import org.jetbrains.annotations.NotNull;
3320

34-
public class EavAttributeSetupPatchGenerator extends FileGenerator {
35-
private final EavEntityDataInterface eavEntityData;
36-
private final Project project;
37-
private final DirectoryGenerator directoryGenerator;
38-
private final FileFromTemplateGenerator fileFromTemplateGenerator;
39-
private final ValidatorBundle validatorBundle;
40-
private final CommonBundle commonBundle;
21+
public class EavAttributeSetupPatchGenerator extends PhpFileGenerator {
22+
private final EavEntityDataInterface data;
4123

4224
/**
4325
* Constructor.
4426
*
45-
* @param eavEntityData EavEntityDataInterface
46-
* @param project Project
27+
* @param data EavEntityDataInterface
28+
* @param project Project
4729
*/
4830
public EavAttributeSetupPatchGenerator(
49-
final @NotNull EavEntityDataInterface eavEntityData,
31+
final @NotNull EavEntityDataInterface data,
5032
final Project project
5133
) {
52-
super(project);
53-
54-
this.eavEntityData = eavEntityData;
55-
this.project = project;
56-
this.directoryGenerator = new DirectoryGenerator();
57-
this.fileFromTemplateGenerator = new FileFromTemplateGenerator(project);
58-
this.validatorBundle = new ValidatorBundle();
59-
this.commonBundle = new CommonBundle();
34+
this(data, project, true);
6035
}
6136

62-
@Override
63-
public PsiFile generate(final String actionName) {
64-
final String errorTitle = commonBundle.message("common.error");
65-
final PhpClass dataPatchClass = GetPhpClassByFQN.getInstance(project)
66-
.execute(getDataPatchFqn());
67-
68-
if (validateIfFileAlreadyExist(dataPatchClass, errorTitle)) {
69-
return null;
70-
}
71-
72-
final PhpFile dataPathFile = createDataPathClass(actionName);
73-
74-
if (validateIfFileCanBeCreated(errorTitle, dataPathFile)) {
75-
return null;
76-
}
77-
78-
return dataPathFile;
79-
}
80-
81-
private String getDataPatchFqn() {
82-
return eavEntityData.getNamespace()
83-
+ Package.fqnSeparator
84-
+ eavEntityData.getDataPatchName();
85-
}
86-
87-
private boolean validateIfFileAlreadyExist(
88-
final PhpClass dataPatchClass,
89-
final String errorTitle
37+
/**
38+
* Php file generator constructor.
39+
*
40+
* @param project Project
41+
* @param checkFileAlreadyExists boolean
42+
*/
43+
public EavAttributeSetupPatchGenerator(
44+
final @NotNull EavEntityDataInterface data,
45+
final @NotNull Project project,
46+
final boolean checkFileAlreadyExists
9047
) {
91-
if (dataPatchClass != null) {
92-
final String errorMessage = validatorBundle.message(
93-
"validator.file.alreadyExists",
94-
"Data Patch Class"
95-
);
96-
JOptionPane.showMessageDialog(
97-
null,
98-
errorMessage,
99-
errorTitle,
100-
JOptionPane.ERROR_MESSAGE
101-
);
102-
103-
return true;
104-
}
105-
106-
return false;
48+
super(project, checkFileAlreadyExists);
49+
this.data = data;
10750
}
10851

109-
@Nullable
110-
private PhpFile createDataPathClass(final String actionName) {
111-
final PsiDirectory parentDirectory = getDataPatchDirectory();
112-
final Properties attributes = getAttributes();
113-
final PsiFile dataPatchFile = fileFromTemplateGenerator.generate(
114-
new EavAttributeDataPatchPhp(eavEntityData.getDataPatchName()),
115-
attributes,
116-
parentDirectory,
117-
actionName
118-
);
119-
120-
if (dataPatchFile == null) {
121-
return null;
122-
}
123-
124-
return (PhpFile) dataPatchFile;
125-
}
126-
127-
private PsiDirectory getDataPatchDirectory() {
128-
PsiDirectory parentDirectory = new ModuleIndex(project)
129-
.getModuleDirectoryByModuleName(eavEntityData.getModuleName());
130-
final String[] dataPatchDirectories = eavEntityData.getDirectory().split(File.separator);
131-
132-
for (final String dataPatchDirectory : dataPatchDirectories) {
133-
parentDirectory = directoryGenerator.findOrCreateSubdirectory(
134-
parentDirectory,
135-
dataPatchDirectory
136-
);
137-
}
138-
139-
return parentDirectory;
140-
}
141-
142-
private boolean validateIfFileCanBeCreated(
143-
final String errorTitle,
144-
final PhpFile dataPathFile
145-
) {
146-
if (dataPathFile == null) {
147-
final String errorMessage = validatorBundle.message(
148-
"validator.file.cantBeCreated",
149-
"Data Patch Class"
150-
);
151-
JOptionPane.showMessageDialog(
152-
null,
153-
errorMessage,
154-
errorTitle,
155-
JOptionPane.ERROR_MESSAGE
156-
);
157-
158-
return true;
159-
}
160-
return false;
52+
@Override
53+
protected AbstractPhpFile initFile() {
54+
return new EavAttributeDataPatchFile(data.getModuleName(), data.getDataPatchName());
16155
}
16256

16357
@Override
16458
protected void fillAttributes(final Properties attributes) {
165-
attributes.setProperty("NAME", eavEntityData.getDataPatchName());
166-
attributes.setProperty("NAMESPACE", eavEntityData.getNamespace());
167-
168-
final String dataPatchInterface = DataPatchDependency.DATA_PATCH_INTERFACE.getClassPatch();
169-
attributes.setProperty(
170-
"IMPLEMENTS",
171-
PhpClassGeneratorUtil.getNameFromFqn(dataPatchInterface)
172-
);
173-
174-
final String eavSetup = DataPatchDependency.ENV_SETUP.getClassPatch();
175-
final String eavSetupFactory = DataPatchDependency.EAV_SETUP_FACTORY.getClassPatch();
176-
final String moduleDataSetupInterface =
177-
DataPatchDependency.MODULE_DATA_SETUP_INTERFACE.getClassPatch();
178-
final String entityClass = eavEntityData.getEntityClass();
179-
59+
final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder();
60+
61+
phpClassTypesBuilder
62+
.appendProperty("CLASS_NAME", data.getDataPatchName())
63+
.appendProperty("NAMESPACE", data.getNamespace())
64+
.appendProperty("ENTITY_CLASS", data.getEntityClass())
65+
.appendProperty("ATTRIBUTE_CODE", data.getCode())
66+
.appendProperty("ATTRIBUTE_SET", String.join(",", getAttributesList(data)))
67+
.append(
68+
"IMPLEMENTS",
69+
DataPatchDependency.DATA_PATCH_INTERFACE.getClassPatch()
70+
)
71+
.append(
72+
"MODULE_DATA_SETUP_INTERFACE",
73+
DataPatchDependency.MODULE_DATA_SETUP_INTERFACE.getClassPatch())
74+
.append(
75+
"EAV_SETUP_FACTORY",
76+
DataPatchDependency.EAV_SETUP_FACTORY.getClassPatch()
77+
)
78+
.append(
79+
"EAV_SETUP",
80+
DataPatchDependency.ENV_SETUP.getClassPatch()
81+
)
82+
.mergeProperties(attributes);
18083
attributes.setProperty(
181-
"MODULE_DATA_SETUP_INTERFACE",
182-
PhpClassGeneratorUtil.getNameFromFqn(moduleDataSetupInterface)
84+
"USES",
85+
PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses())
18386
);
184-
attributes.setProperty(
185-
"EAV_SETUP_FACTORY",
186-
PhpClassGeneratorUtil.getNameFromFqn(eavSetupFactory)
187-
);
188-
attributes.setProperty(
189-
"EAV_SETUP",
190-
PhpClassGeneratorUtil.getNameFromFqn(eavSetup)
191-
);
192-
attributes.setProperty(
193-
"ENTITY_CLASS",
194-
entityClass
195-
);
196-
197-
final List<String> uses = new ArrayList<>();
198-
uses.add(dataPatchInterface);
199-
uses.add(eavSetup);
200-
uses.add(eavSetupFactory);
201-
uses.add(moduleDataSetupInterface);
202-
203-
attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses));
204-
205-
attributes.setProperty("ATTRIBUTE_CODE", eavEntityData.getCode());
206-
207-
final List<String> entityAttributes = getAttributesList(eavEntityData);
208-
attributes.setProperty("ATTRIBUTE_SET", String.join(",", entityAttributes));
20987
}
21088

21189
private List<String> getAttributesList(final EavEntityDataInterface eavEntityData) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.magento.files;
7+
8+
public class EavAttributeDataPatchFile extends AbstractPhpFile {
9+
public static final String HUMAN_READABLE_NAME = "Eav Attribute Data Patch Class";
10+
public static final String TEMPLATE = "Magento Eav Attribute Data Patch Class";
11+
public static final String DEFAULT_DIR = "Setup/Patch/Data";
12+
13+
/**
14+
* Constructor.
15+
*
16+
* @param className String
17+
*/
18+
public EavAttributeDataPatchFile(final String moduleName, final String className) {
19+
super(moduleName, className);
20+
}
21+
22+
@Override
23+
public String getDirectory() {
24+
return DEFAULT_DIR;
25+
}
26+
27+
@Override
28+
public String getHumanReadableName() {
29+
return HUMAN_READABLE_NAME;
30+
}
31+
32+
@Override
33+
public String getTemplate() {
34+
return TEMPLATE;
35+
}
36+
}

src/com/magento/idea/magento2plugin/magento/files/EavAttributeDataPatchPhp.java

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

0 commit comments

Comments
 (0)