Skip to content

Commit f5da382

Browse files
Added entity data mapper generator and test for generation
1 parent 5a68b4c commit f5da382

File tree

10 files changed

+585
-5
lines changed

10 files changed

+585
-5
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
<internalFileTemplate name="Magento Module Declarative Schema XML"/>
230230
<internalFileTemplate name="Magento Module Declarative Schema Whitelist JSON"/>
231231
<internalFileTemplate name="Magento Get List Query Model"/>
232+
<internalFileTemplate name="Magento Entity Data Mapper"/>
232233

233234
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
234235

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html lang="en">
2+
<body>
3+
<p face="verdana" size="-1">
4+
5+
</p>
6+
7+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
8+
<tr>
9+
<td colspan="3"><font face="verdana" size="-1">Template's predefined variables:</font></td>
10+
</tr>
11+
<tr>
12+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
13+
<td width="10">&nbsp;</td>
14+
<td width="100%" valign="top"><font face="verdana" size="-1"></font></td>
15+
</tr>
16+
</table>
17+
</body>
18+
</html>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
4+
namespace ${NAMESPACE};
5+
6+
#set($uses = ${USES})
7+
#foreach ($use in $uses.split(","))
8+
use $use;
9+
#end
10+
11+
/**
12+
* ${ENTITY_NAME} magento model collection to entity data transfer object array mapper.
13+
*/
14+
class ${CLASS_NAME}
15+
{
16+
/**
17+
* @var ${DTO_FACTORY}
18+
*/
19+
private $entityDtoFactory;
20+
21+
/**
22+
* @param ${DTO_FACTORY} $entityDtoFactory
23+
*/
24+
public function __construct(
25+
${DTO_FACTORY} $entityDtoFactory
26+
) {
27+
$this->entityDtoFactory = $entityDtoFactory;
28+
}
29+
30+
/**
31+
* Map magento models to DTO array.
32+
*
33+
* @param ${ABSTRACT_COLLECTION} $collection
34+
*
35+
* @return array|${DTO_TYPE}[]
36+
*/
37+
public function map(${ABSTRACT_COLLECTION} $collection): array
38+
{
39+
$results = [];
40+
/** @var ${MAGENTO_MODEL_TYPE} $item */
41+
foreach ($collection->getItems() as $item) {
42+
/** @var ${DTO_TYPE}|${DATA_OBJECT} $entityDto */
43+
$entityDto = $this->entityDtoFactory->create();
44+
$entityDto->addData($item->getData());
45+
46+
#set($brackets = "[]")
47+
$results$brackets = $entityDto;
48+
}
49+
50+
return $results;
51+
}
52+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.data;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class EntityDataMapperData {
11+
private final String moduleName;
12+
private final String entityName;
13+
private final String namespace;
14+
private final String classFqn;
15+
private final String modelClassFqn;
16+
private final String dataModelClassFqn;
17+
18+
/**
19+
* Magento entity data mapper data constructor.
20+
*
21+
* @param moduleName String
22+
* @param entityName String
23+
* @param namespace String
24+
* @param modelClassFqn String
25+
* @param dataModelClassFqn String
26+
*/
27+
public EntityDataMapperData(
28+
final @NotNull String moduleName,
29+
final @NotNull String entityName,
30+
final @NotNull String namespace,
31+
final @NotNull String classFqn,
32+
final @NotNull String modelClassFqn,
33+
final @NotNull String dataModelClassFqn
34+
) {
35+
this.moduleName = moduleName;
36+
this.entityName = entityName;
37+
this.namespace = namespace;
38+
this.classFqn = classFqn;
39+
this.modelClassFqn = modelClassFqn;
40+
this.dataModelClassFqn = dataModelClassFqn;
41+
}
42+
43+
/**
44+
* Get module name.
45+
*
46+
* @return String
47+
*/
48+
public String getModuleName() {
49+
return moduleName;
50+
}
51+
52+
/**
53+
* Get entity name.
54+
*
55+
* @return String
56+
*/
57+
public String getEntityName() {
58+
return entityName;
59+
}
60+
61+
/**
62+
* Get namespace.
63+
*
64+
* @return String
65+
*/
66+
public String getNamespace() {
67+
return namespace;
68+
}
69+
70+
/**
71+
* Get class fqn.
72+
*
73+
* @return String
74+
*/
75+
public String getClassFqn() {
76+
return classFqn;
77+
}
78+
79+
/**
80+
* Get model class fqn.
81+
*
82+
* @return String
83+
*/
84+
public String getModelClassFqn() {
85+
return modelClassFqn;
86+
}
87+
88+
/**
89+
* Get data model class fqn.
90+
*
91+
* @return String
92+
*/
93+
public String getDataModelClassFqn() {
94+
return dataModelClassFqn;
95+
}
96+
}

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

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import com.magento.idea.magento2plugin.actions.generation.data.DataModelData;
1919
import com.magento.idea.magento2plugin.actions.generation.data.DataModelInterfaceData;
2020
import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData;
21+
import com.magento.idea.magento2plugin.actions.generation.data.EntityDataMapperData;
22+
import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData;
2123
import com.magento.idea.magento2plugin.actions.generation.data.LayoutXmlData;
2224
import com.magento.idea.magento2plugin.actions.generation.data.MenuXmlData;
2325
import com.magento.idea.magento2plugin.actions.generation.data.ModelData;
24-
import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData;
2526
import com.magento.idea.magento2plugin.actions.generation.data.PreferenceDiXmFileData;
2627
import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData;
2728
import com.magento.idea.magento2plugin.actions.generation.data.RoutesXmlData;
@@ -39,13 +40,14 @@
3940
import com.magento.idea.magento2plugin.actions.generation.generator.DataModelInterfaceGenerator;
4041
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaWhitelistJsonGenerator;
4142
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator;
43+
import com.magento.idea.magento2plugin.actions.generation.generator.EntityDataMapperGenerator;
44+
import com.magento.idea.magento2plugin.actions.generation.generator.GetListQueryModelGenerator;
4245
import com.magento.idea.magento2plugin.actions.generation.generator.LayoutXmlGenerator;
4346
import com.magento.idea.magento2plugin.actions.generation.generator.MenuXmlGenerator;
4447
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleCollectionGenerator;
4548
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleControllerClassGenerator;
4649
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleModelGenerator;
4750
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleResourceModelGenerator;
48-
import com.magento.idea.magento2plugin.actions.generation.generator.GetListQueryModelGenerator;
4951
import com.magento.idea.magento2plugin.actions.generation.generator.PreferenceDiXmlGenerator;
5052
import com.magento.idea.magento2plugin.actions.generation.generator.RoutesXmlGenerator;
5153
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentDataProviderGenerator;
@@ -56,6 +58,7 @@
5658
import com.magento.idea.magento2plugin.magento.files.ControllerBackendPhp;
5759
import com.magento.idea.magento2plugin.magento.files.DataModel;
5860
import com.magento.idea.magento2plugin.magento.files.DataModelInterface;
61+
import com.magento.idea.magento2plugin.magento.files.EntityDataMapperFile;
5962
import com.magento.idea.magento2plugin.magento.files.ModelPhp;
6063
import com.magento.idea.magento2plugin.magento.files.ModuleMenuXml;
6164
import com.magento.idea.magento2plugin.magento.files.ResourceModelPhp;
@@ -107,7 +110,8 @@
107110
"PMD.ExcessiveImports",
108111
"PMD.GodClass",
109112
"PMD.TooManyMethods",
110-
"PMD.CyclomaticComplexity"
113+
"PMD.CyclomaticComplexity",
114+
"PMD.ExcessiveClassLength",
111115
})
112116
public class NewEntityDialog extends AbstractDialog {
113117
@NotNull
@@ -280,6 +284,7 @@ private void onOK() {
280284
generateRoutesXmlFile();
281285
generateViewControllerFile();
282286
generateSubmitControllerFile();
287+
generateEntityDataMapperFile();
283288
generateModelGetListQueryFile();
284289
generateDataProviderFile();
285290
generateLayoutFile();
@@ -1009,6 +1014,41 @@ private void generateWhitelistJsonFile(final @NotNull DbSchemaXmlData dbSchemaXm
10091014
).generate(ACTION_NAME, false);
10101015
}
10111016

1017+
/**
1018+
* Generate entity data mapper type.
1019+
*/
1020+
private void generateEntityDataMapperFile() {
1021+
final EntityDataMapperFile entityDataMapperFile =
1022+
EntityDataMapperFile.getInstance(getEntityName());
1023+
1024+
final String namespace = entityDataMapperFile.getNamespace(getModuleName());
1025+
final String classFqn = entityDataMapperFile.getClassFqn(getModuleName());
1026+
1027+
final NamespaceBuilder modelNamespace = getModelNamespace();
1028+
final NamespaceBuilder dtoModelNamespace = getDataModelNamespace();
1029+
final NamespaceBuilder dtoInterfaceModelNamespace = getDataModelInterfaceNamespace();
1030+
1031+
final String dtoType;
1032+
1033+
if (createInterface.isSelected()) {
1034+
dtoType = dtoInterfaceModelNamespace.getClassFqn();
1035+
} else {
1036+
dtoType = dtoModelNamespace.getClassFqn();
1037+
}
1038+
1039+
new EntityDataMapperGenerator(
1040+
new EntityDataMapperData(
1041+
getModuleName(),
1042+
getEntityName(),
1043+
namespace,
1044+
classFqn,
1045+
modelNamespace.getClassFqn(),
1046+
dtoType
1047+
),
1048+
project
1049+
).generate(ACTION_NAME, false);
1050+
}
1051+
10121052
/**
10131053
* Run GetListQuery.php file generator.
10141054
*/
@@ -1032,8 +1072,10 @@ private void generateModelGetListQueryFile() {
10321072
* @return String
10331073
*/
10341074
private String getEntityDataMapperType() {
1035-
// TODO: implement with entity data mapper generation.
1036-
return "Test\\Test\\Mapper\\" + getEntityName() + "DataMapper";
1075+
final EntityDataMapperFile entityDataMapperFile =
1076+
EntityDataMapperFile.getInstance(getEntityName());
1077+
1078+
return entityDataMapperFile.getClassFqn(getModuleName());
10371079
}
10381080

10391081
/**

0 commit comments

Comments
 (0)