Skip to content

Commit da455b3

Browse files
committed
Fix saving entity
1 parent bf3076d commit da455b3

File tree

3 files changed

+22
-33
lines changed

3 files changed

+22
-33
lines changed

Command/AsyncEvent/SaveCommand.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
namespace MageOS\AsyncEventsAdminUi\Command\AsyncEvent;
55

66
use Exception;
7+
use Magento\Framework\Encryption\EncryptorInterface;
78
use MageOS\AsyncEvents\Api\Data\AsyncEventInterface;
8-
use MageOS\AsyncEvents\Model\AsyncEvent as AsyncEventModel;
9-
use MageOS\AsyncEvents\Model\AsyncEventFactory as AsyncEventModelFactory;
109
use MageOS\AsyncEvents\Model\ResourceModel\AsyncEvent as AsyncEventResource;
1110
use Magento\Framework\Exception\CouldNotSaveException;
1211
use Psr\Log\LoggerInterface;
@@ -17,16 +16,14 @@
1716
class SaveCommand
1817
{
1918
private LoggerInterface $logger;
20-
private AsyncEventModelFactory $modelFactory;
2119
private AsyncEventResource $resource;
2220

2321
public function __construct(
2422
LoggerInterface $logger,
25-
AsyncEventModelFactory $modelFactory,
26-
AsyncEventResource $resource
23+
AsyncEventResource $resource,
24+
private readonly EncryptorInterface $encryptor
2725
) {
2826
$this->logger = $logger;
29-
$this->modelFactory = $modelFactory;
3027
$this->resource = $resource;
3128
}
3229

@@ -38,15 +35,15 @@ public function __construct(
3835
public function execute(AsyncEventInterface $asyncEvent): int
3936
{
4037
try {
41-
/** @var AsyncEventModel $model */
42-
$model = $this->modelFactory->create();
43-
$model->addData($asyncEvent->getData());
44-
$model->setHasDataChanges(true);
38+
$asyncEvent->setHasDataChanges(true);
4539

46-
if (!$model->getSubscriptionId()) {
47-
$model->isObjectNew(true);
40+
if (!$asyncEvent->getSubscriptionId()) {
41+
$asyncEvent->isObjectNew(true);
42+
$asyncEvent->setSubscribedAt((new \DateTime())->format(\DateTimeInterface::ATOM));
43+
$secretVerificationToken = $this->encryptor->encrypt($asyncEvent->getVerificationToken());
44+
$asyncEvent->setVerificationToken($secretVerificationToken);
4845
}
49-
$this->resource->save($model);
46+
$this->resource->save($asyncEvent);
5047
} catch (Exception $exception) {
5148
$this->logger->error(
5249
__('Could not save Asynchronous Event Subscriber. Original message: {message}'),
@@ -58,6 +55,6 @@ public function execute(AsyncEventInterface $asyncEvent): int
5855
throw new CouldNotSaveException(__('Could not save Asynchronous Event Subscriber.'));
5956
}
6057

61-
return (int)$model->getSubscriptionId();
58+
return (int)$asyncEvent->getSubscriptionId();
6259
}
6360
}

Controller/Adminhtml/Events/Save.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Framework\App\Request\DataPersistorInterface;
1313
use Magento\Framework\App\ResponseInterface;
1414
use Magento\Framework\Controller\ResultInterface;
15-
use Magento\Framework\DataObject;
1615
use Magento\Framework\Exception\CouldNotSaveException;
1716

1817
/**
@@ -29,7 +28,7 @@ class Save extends Action implements HttpPostActionInterface
2928

3029
private DataPersistorInterface $dataPersistor;
3130
private SaveCommand $saveCommand;
32-
private AsyncEventInterfaceFactory $entityDataFactory;
31+
private AsyncEventInterfaceFactory $asyncEventFactory;
3332

3433
public function __construct(
3534
Context $context,
@@ -40,7 +39,7 @@ public function __construct(
4039
parent::__construct($context);
4140
$this->dataPersistor = $dataPersistor;
4241
$this->saveCommand = $saveCommand;
43-
$this->entityDataFactory = $entityDataFactory;
42+
$this->asyncEventFactory = $entityDataFactory;
4443
}
4544

4645
/**
@@ -52,11 +51,17 @@ public function execute()
5251
{
5352
$resultRedirect = $this->resultRedirectFactory->create();
5453
$params = $this->getRequest()->getParams();
54+
if (isset($params['general'])) {
55+
$params = $params['general'];
56+
}
57+
if ($params['subscription_id'] === '') {
58+
unset($params['subscription_id']);
59+
}
5560

5661
try {
57-
/** @var AsyncEventInterface|DataObject $entityModel */
58-
$entityModel = $this->entityDataFactory->create();
59-
$entityModel->addData($params['general']);
62+
/** @var AsyncEventInterface $entityModel */
63+
$entityModel = $this->asyncEventFactory->create();
64+
$entityModel->addData($params);
6065
$this->saveCommand->execute($entityModel);
6166
$this->messageManager->addSuccessMessage(
6267
__('The Asynchronous Event Subscriber data was saved successfully')

view/adminhtml/ui_component/async_events_events_form.xml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,6 @@
7272
<dataScope>recipient_url</dataScope>
7373
</settings>
7474
</field>
75-
<field name="verification_token" sortOrder="20" formElement="input">
76-
<argument name="data" xsi:type="array">
77-
<item name="config" xsi:type="array">
78-
<item name="source" xsi:type="string">verification_token</item>
79-
</item>
80-
</argument>
81-
<settings>
82-
<dataType>string</dataType>
83-
<label translate="true">Verification Token</label>
84-
<notice translate="true">Leave empty to auto-generate</notice>
85-
<dataScope>verification_token</dataScope>
86-
</settings>
87-
</field>
8875
<field name="status" sortOrder="30" formElement="checkbox">
8976
<argument name="data" xsi:type="array">
9077
<item name="config" xsi:type="array">

0 commit comments

Comments
 (0)