Skip to content

Commit b6cd7a9

Browse files
Refactored generators
1 parent 1ad8406 commit b6cd7a9

File tree

10 files changed

+83
-81
lines changed

10 files changed

+83
-81
lines changed

src/com/magento/idea/magento2plugin/actions/generation/data/ProductEntityData.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
package com.magento.idea.magento2plugin.actions.generation.data;
77

8-
import com.magento.idea.magento2plugin.magento.packages.File;
9-
import com.magento.idea.magento2plugin.magento.packages.Package;
8+
import com.magento.idea.magento2plugin.magento.files.EavAttributeDataPatchFile;
109
import com.magento.idea.magento2plugin.magento.packages.eav.EavEntity;
1110

1211
@SuppressWarnings({"PMD.TooManyFields"})
@@ -144,25 +143,9 @@ public String getInput() {
144143

145144
@Override
146145
public String getNamespace() {
147-
if (namespace == null) {
148-
namespace = getDataPathNamespace();
149-
}
150-
151146
return namespace;
152147
}
153148

154-
private String getDataPathNamespace() {
155-
final String[] parts = moduleName.split(Package.vendorModuleNameSeparator);
156-
if (parts[0] == null || parts[1] == null || parts.length > 2) {
157-
return null;
158-
}
159-
final String directoryPart = getDirectory().replace(
160-
File.separator,
161-
Package.fqnSeparator
162-
);
163-
return parts[0] + Package.fqnSeparator + parts[1] + Package.fqnSeparator + directoryPart;
164-
}
165-
166149
@Override
167150
public String getModuleName() {
168151
return moduleName;
@@ -171,7 +154,7 @@ public String getModuleName() {
171154
@Override
172155
public String getDirectory() {
173156
if (directory == null) {
174-
directory = "Setup/Patch/Data";
157+
directory = EavAttributeDataPatchFile.DEFAULT_DIR;
175158
}
176159

177160
return directory;

src/com/magento/idea/magento2plugin/actions/generation/data/SourceModelData.java

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,16 @@
66
package com.magento.idea.magento2plugin.actions.generation.data;
77

88
import com.magento.idea.magento2plugin.magento.files.SourceModelFile;
9-
import com.magento.idea.magento2plugin.magento.packages.File;
10-
import com.magento.idea.magento2plugin.magento.packages.Package;
119

1210
public class SourceModelData {
1311
private String className;
14-
private String namespace;
1512
private String moduleName;
1613
private String directory;
1714

1815
public String getClassName() {
1916
return className;
2017
}
2118

22-
/**
23-
* Constructor.
24-
*/
25-
public String getNamespace() {
26-
if (namespace == null) {
27-
namespace = getDefaultSourceModelNamespace();
28-
}
29-
30-
return namespace;
31-
}
32-
33-
/**
34-
* Provides default namespace.
35-
*
36-
* @return String
37-
*/
38-
public String getDefaultSourceModelNamespace() {
39-
final String[] parts = moduleName.split(Package.vendorModuleNameSeparator);
40-
if (parts[0] == null || parts[1] == null || parts.length > 2) {
41-
return null;
42-
}
43-
final String directoryPart = getDirectory().replace(
44-
File.separator,
45-
Package.fqnSeparator
46-
);
47-
return parts[0] + Package.fqnSeparator + parts[1] + Package.fqnSeparator + directoryPart;
48-
}
49-
5019
/**
5120
* Get default namespace.
5221
*
@@ -73,10 +42,6 @@ public void setClassName(final String className) {
7342
this.className = className;
7443
}
7544

76-
public void setNamespace(final String namespace) {
77-
this.namespace = namespace;
78-
}
79-
8045
public void setModuleName(final String moduleName) {
8146
this.moduleName = moduleName;
8247
}

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
2020
import com.magento.idea.magento2plugin.actions.generation.generator.EavAttributeSetupPatchGenerator;
2121
import com.magento.idea.magento2plugin.actions.generation.generator.SourceModelGenerator;
22-
import com.magento.idea.magento2plugin.magento.packages.Package;
23-
import com.magento.idea.magento2plugin.magento.packages.eav.*;
22+
import com.magento.idea.magento2plugin.magento.files.SourceModelFile;
23+
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeInput;
24+
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeScope;
25+
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeSourceModel;
26+
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeType;
27+
import com.magento.idea.magento2plugin.magento.packages.eav.EavEntity;
2428
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
2529
import java.awt.event.ItemEvent;
2630
import java.awt.event.ItemListener;
@@ -38,7 +42,12 @@
3842
import org.codehaus.plexus.util.StringUtils;
3943
import org.jetbrains.annotations.NotNull;
4044

41-
@SuppressWarnings({"PMD.TooManyFields", "PMD.ExcessiveImports", "PMD.TooManyMethods", "PMD.UnusedPrivateField"})
45+
@SuppressWarnings({
46+
"PMD.TooManyFields",
47+
"PMD.ExcessiveImports",
48+
"PMD.TooManyMethods",
49+
"PMD.UnusedPrivateField"
50+
})
4251
public class NewEavAttributeDialog extends AbstractDialog {
4352
private final String moduleName;
4453
private JPanel contentPanel;
@@ -285,7 +294,9 @@ private void generateSourceModelFile() {
285294
final ComboBoxItemData selectedSource = (ComboBoxItemData) sourceComboBox.getSelectedItem();
286295

287296
if (selectedSource == null
288-
|| !selectedSource.getText().equals(AttributeSourceModel.GENERATE_SOURCE.getSource())) {
297+
|| !selectedSource.getText().equals(
298+
AttributeSourceModel.GENERATE_SOURCE.getSource()
299+
)) {
289300
return;
290301
}
291302

@@ -332,19 +343,20 @@ private String getAttributeSource() {
332343
final ComboBoxItemData selectedItem = (ComboBoxItemData) sourceComboBox.getSelectedItem();
333344

334345
if (selectedItem == null
335-
|| selectedItem.getText().equals(AttributeSourceModel.NULLABLE_SOURCE.getSource())) {
346+
|| selectedItem.getText().equals(
347+
AttributeSourceModel.NULLABLE_SOURCE.getSource()
348+
)) {
336349
return null;
337350
}
338351

339352
if (selectedItem.getText().equals(AttributeSourceModel.GENERATE_SOURCE.getSource())
340353
&& sourceModelData != null) {
341354

342-
return String.join(
343-
Package.fqnSeparator,
344-
"",
345-
sourceModelData.getNamespace(),
346-
sourceModelData.getClassName()
347-
);
355+
return "\\" + new SourceModelFile(
356+
sourceModelData.getModuleName(),
357+
sourceModelData.getClassName(),
358+
sourceModelData.getDirectory()
359+
).getClassFqn();
348360
}
349361

350362
return sourceComboBox.getSelectedItem().toString();
@@ -361,7 +373,8 @@ private String getAttributeScope() {
361373
}
362374

363375
private String getAttributeInput() {
364-
final ComboBoxItemData selectedAttributeInput = (ComboBoxItemData) inputComboBox.getSelectedItem();
376+
final ComboBoxItemData selectedAttributeInput =
377+
(ComboBoxItemData) inputComboBox.getSelectedItem();
365378

366379
if (selectedAttributeInput != null) {
367380
return selectedAttributeInput.getText().trim();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected void fillAttributes(final Properties attributes) {
6060

6161
phpClassTypesBuilder
6262
.appendProperty("CLASS_NAME", data.getDataPatchName())
63-
.appendProperty("NAMESPACE", data.getNamespace())
63+
.appendProperty("NAMESPACE", this.getFile().getNamespace())
6464
.appendProperty("ENTITY_CLASS", data.getEntityClass())
6565
.appendProperty("ATTRIBUTE_CODE", data.getCode())
6666
.appendProperty("ATTRIBUTE_SET", String.join(",", getAttributesList(data)))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected void fillAttributes(final Properties attributes) {
6464
uses.add(abstractSourceClass);
6565

6666
attributes.setProperty("NAME", data.getClassName());
67-
attributes.setProperty("NAMESPACE", data.getNamespace());
67+
attributes.setProperty("NAMESPACE", this.getFile().getNamespace());
6868
attributes.setProperty(
6969
"EXTENDS",
7070
PhpClassGeneratorUtil.getNameFromFqn(abstractSourceClass)

src/com/magento/idea/magento2plugin/actions/generation/generator/util/eav/ProductAttributeMapper.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,8 @@ private Map<String, String> getMappedAttributes(final ProductEntityData eavEntit
5050
wrapStringValueForTemplate(eavEntityData.getLabel()));
5151
mappedAttributes.put(EavAttribute.INPUT.getAttribute(),
5252
wrapStringValueForTemplate(eavEntityData.getInput()));
53-
54-
final String eavSource = eavEntityData.getSource();
5553
mappedAttributes.put(EavAttribute.SOURCE.getAttribute(),
56-
eavSource == null || eavSource.equals(AttributeSourceModel.NULLABLE_SOURCE.getSource())
57-
? null : eavSource + "::class");
58-
54+
getEntitySource(eavEntityData));
5955
mappedAttributes.put(EavAttribute.REQUIRED.getAttribute(),
6056
Boolean.toString(eavEntityData.isRequired()));
6157
mappedAttributes.put(EavAttribute.SORT_ORDER.getAttribute(),
@@ -81,4 +77,13 @@ private Map<String, String> getMappedAttributes(final ProductEntityData eavEntit
8177
private String wrapStringValueForTemplate(final String value) {
8278
return "'" + value + "'";
8379
}
80+
81+
82+
private String getEntitySource(final ProductEntityData eavEntityData) {
83+
final String eavSource = eavEntityData.getSource();
84+
85+
return eavSource == null
86+
|| eavSource.equals(AttributeSourceModel.NULLABLE_SOURCE.getSource())
87+
? null : eavSource + "::class";
88+
}
8489
}

testData/actions/generation/generator/SourceModelGenerator/generateFile/CustomSourceModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Foo\Bar\Setup\Patch\Data;
3+
namespace Foo\Bar\Model\Source;
44

55
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
66

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Foo\Bar\Custom\Source\Directory;
4+
5+
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
6+
7+
class CustomSourceModel extends AbstractSource
8+
{
9+
/**
10+
* Retrieve All options
11+
*
12+
* @return array
13+
*/
14+
public function getAllOptions(): array
15+
{
16+
// TODO: Implement getAllOptions() method.
17+
}
18+
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
public class EavAttributeSetupPatchGeneratorTest extends BaseGeneratorTestCase {
1414
private final static String MODULE_NAME = "Foo_Bar";
15-
private final static String DATA_PATCH_NAMESPACE = "Foo\\Bar\\Setup\\Patch\\Data";
1615

1716
/**
1817
* Test Data patch for product's eav attribute generator.
@@ -37,7 +36,6 @@ public void testGenerateFile() {
3736
productEntityData.setGroup("General");
3837

3938
productEntityData.setDataPatchName("AddTestAttribute");
40-
productEntityData.setNamespace(DATA_PATCH_NAMESPACE);
4139
productEntityData.setModuleName(MODULE_NAME);
4240

4341
final EavAttributeSetupPatchGenerator setupPatchGenerator =
@@ -71,7 +69,6 @@ public void testGenerateFileWithBooleanSourceModel() {
7169
productEntityData.setGroup("General");
7270

7371
productEntityData.setDataPatchName("AddBooleanInputAttributeAttribute");
74-
productEntityData.setNamespace(DATA_PATCH_NAMESPACE);
7572
productEntityData.setModuleName(MODULE_NAME);
7673

7774
final EavAttributeSetupPatchGenerator setupPatchGenerator =
@@ -105,7 +102,6 @@ public void testGenerateFileWithGeneratedSourceModel() {
105102
productEntityData.setGroup("General");
106103

107104
productEntityData.setDataPatchName("AddAttributeWithCustomSourceAttribute");
108-
productEntityData.setNamespace(DATA_PATCH_NAMESPACE);
109105
productEntityData.setModuleName(MODULE_NAME);
110106

111107
final EavAttributeSetupPatchGenerator setupPatchGenerator =

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@
1111

1212
public class SourceModelGeneratorTest extends BaseGeneratorTestCase {
1313
private final static String MODULE_NAME = "Foo_Bar";
14-
private final static String SOURCE_MODEL_NAMESPACE = "Foo\\Bar\\Setup\\Patch\\Data";
1514

1615
/**
17-
* Test Data patch for product's eav attribute generator.
16+
* Test source model generation
1817
*/
1918
public void testGenerateFile() {
2019
final Project project = myFixture.getProject();
2120

2221
final SourceModelData sourceModelData = new SourceModelData();
2322
sourceModelData.setClassName("CustomSourceModel");
2423
sourceModelData.setModuleName(MODULE_NAME);
25-
sourceModelData.setNamespace(SOURCE_MODEL_NAMESPACE);
2624

2725
final SourceModelGenerator sourceModelGeneratorGenerator =
2826
new SourceModelGenerator(sourceModelData, project);
@@ -36,4 +34,28 @@ public void testGenerateFile() {
3634
dataPatchFile
3735
);
3836
}
37+
38+
/**
39+
* Test source model in custom directory generation
40+
*/
41+
public void testGenerateFileInCustomDirectory() {
42+
final Project project = myFixture.getProject();
43+
44+
final SourceModelData sourceModelData = new SourceModelData();
45+
sourceModelData.setClassName("CustomSourceModel");
46+
sourceModelData.setModuleName(MODULE_NAME);
47+
sourceModelData.setDirectory("Custom/Source/Directory");
48+
49+
final SourceModelGenerator sourceModelGeneratorGenerator =
50+
new SourceModelGenerator(sourceModelData, project);
51+
final PsiFile dataPatchFile = sourceModelGeneratorGenerator.generate("test custom dir");
52+
final String filePatch = this.getFixturePath("CustomSourceModel.php");
53+
final PsiFile expectedFile = myFixture.configureByFile(filePatch);
54+
55+
assertGeneratedFileIsCorrect(
56+
expectedFile,
57+
"src/app/code/Foo/Bar/Custom/Source/Directory",
58+
dataPatchFile
59+
);
60+
}
3961
}

0 commit comments

Comments
 (0)