Skip to content

Commit 9eedbff

Browse files
Merge branch 'mainline-entity-manager' of github.com:magento/magento2-phpstorm-plugin into entity-manager-bug-fixing
2 parents 1c3c94d + 87b1022 commit 9eedbff

File tree

29 files changed

+2243
-202
lines changed

29 files changed

+2243
-202
lines changed

resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@
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 Save Controller Class"/>
233+
<internalFileTemplate name="Magento Entity Data Mapper"/>
234+
<internalFileTemplate name="Magento Save Entity Command Model"/>
232235

233236
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
234237

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+
* Converts a collection of ${ENTITY_NAME} entities to an array of data transfer objects.
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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
* Save ${ENTITY_NAME} controller action.
13+
*/
14+
class ${CLASS_NAME} extends ${EXTENDS} implements ${IMPLEMENTS}
15+
{
16+
/**
17+
* Authorization level of a basic admin session
18+
*
19+
* @see _isAllowed()
20+
*/
21+
const ADMIN_RESOURCE = '${ADMIN_RESOURCE}';
22+
23+
/**
24+
* @var ${DATA_PERSISTOR}
25+
*/
26+
private $dataPersistor;
27+
28+
/**
29+
* @var ${SAVE_COMMAND}
30+
*/
31+
private $saveCommand;
32+
33+
/**
34+
* @var ${ENTITY_DTO_FACTORY}
35+
*/
36+
private $entityDataFactory;
37+
38+
/**
39+
* @param Context $context
40+
* @param ${DATA_PERSISTOR} $dataPersistor
41+
* @param ${SAVE_COMMAND} $saveCommand
42+
* @param ${ENTITY_DTO_FACTORY} $entityDataFactory
43+
*/
44+
public function __construct(
45+
Context $context,
46+
${DATA_PERSISTOR} $dataPersistor,
47+
${SAVE_COMMAND} $saveCommand,
48+
${ENTITY_DTO_FACTORY} $entityDataFactory
49+
) {
50+
parent::__construct($context);
51+
$this->dataPersistor = $dataPersistor;
52+
$this->saveCommand = $saveCommand;
53+
$this->entityDataFactory = $entityDataFactory;
54+
}
55+
56+
/**
57+
* Save ${ENTITY_NAME} Action.
58+
*
59+
* @return ResponseInterface|ResultInterface
60+
*/
61+
public function execute()
62+
{
63+
$resultRedirect = $this->resultRedirectFactory->create();
64+
$params = $this->getRequest()->getParams();
65+
66+
try {
67+
/** @var ${ENTITY_DTO}|${DATA_OBJECT} $entityModel */
68+
$entityModel = $this->entityDataFactory->create();
69+
$entityModel->addData($params);
70+
$this->saveCommand->execute($entityModel);
71+
$this->messageManager->addSuccessMessage(
72+
__('The ${ENTITY_NAME} data was saved successfully')
73+
);
74+
$this->dataPersistor->clear('entity');
75+
} catch (${COULD_NOT_SAVE} $exception) {
76+
$this->messageManager->addErrorMessage($exception->getMessage());
77+
$this->dataPersistor->set('entity', $params);
78+
79+
return $resultRedirect->setPath('*/*/edit', [
80+
'${ENTITY_ID}'=> $this->getRequest()->getParam('${ENTITY_ID}')
81+
]);
82+
}
83+
84+
return $resultRedirect->setPath('*/*/');
85+
}
86+
}
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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
* Save ${ENTITY_NAME} Command.
13+
*/
14+
class ${CLASS_NAME}
15+
{
16+
/**
17+
* @var ${LOGGER}
18+
*/
19+
private $logger;
20+
21+
/**
22+
* @var ${MODEL_FACTORY}
23+
*/
24+
private $modelFactory;
25+
26+
/**
27+
* @var ${RESOURCE}
28+
*/
29+
private $resource;
30+
31+
/**
32+
* @param ${LOGGER} $logger
33+
* @param ${MODEL_FACTORY} $modelFactory
34+
* @param ${RESOURCE} $resource
35+
*/
36+
public function __construct(
37+
${LOGGER} $logger,
38+
${MODEL_FACTORY} $modelFactory,
39+
${RESOURCE} $resource
40+
) {
41+
$this->logger = $logger;
42+
$this->modelFactory = $modelFactory;
43+
$this->resource = $resource;
44+
}
45+
46+
/**
47+
* Save ${ENTITY_NAME}.
48+
*
49+
* @param ${DTO}|${DATA_OBJECT} $${DTO_PROPERTY}
50+
*
51+
* @return int
52+
* @throws ${COULD_NOT_SAVE}
53+
*/
54+
public function execute(${DTO} $${DTO_PROPERTY}): int
55+
{
56+
try {
57+
/** @var ${MODEL} $model */
58+
$model = $this->modelFactory->create();
59+
$model->addData($${DTO_PROPERTY}->getData());
60+
$this->resource->save($model);
61+
} catch (${EXCEPTION} $exception) {
62+
$this->logger->error(
63+
__('Could not save ${ENTITY_NAME}. Original message: {message}'),
64+
[
65+
'message' => $exception->getMessage(),
66+
'exception' => $exception
67+
]
68+
);
69+
throw new ${COULD_NOT_SAVE}(__('Could not save ${ENTITY_NAME}.'));
70+
}
71+
72+
return (int) $model->getEntityId();
73+
}
74+
}
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: 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+
}

0 commit comments

Comments
 (0)