Skip to content

Commit 570320b

Browse files
authored
Merge pull request #1 from mage-os/initial-implementation
Initial implementation
2 parents fc11d80 + 7dafc4b commit 570320b

File tree

25 files changed

+886
-2
lines changed

25 files changed

+886
-2
lines changed

Block/Form/AsyncEvent/Back.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\AsyncEventsAdminUi\Block\Form\AsyncEvent;
5+
6+
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
7+
8+
/**
9+
* Back to list button.
10+
*/
11+
class Back extends GenericButton implements ButtonProviderInterface
12+
{
13+
/**
14+
* Retrieve Back To Grid button settings.
15+
*/
16+
public function getButtonData(): array
17+
{
18+
return $this->wrapButtonSettings(
19+
__('Back To Grid')->getText(),
20+
'back',
21+
sprintf("location.href = '%s';", $this->getUrl('*/*/')),
22+
[],
23+
10
24+
);
25+
}
26+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\AsyncEventsAdminUi\Block\Form\AsyncEvent;
5+
6+
use Magento\Backend\Block\Widget\Context;
7+
use Magento\Framework\App\RequestInterface;
8+
use Magento\Framework\UrlInterface;
9+
10+
/**
11+
* Generic (form) button for Asynchronous Event Subscriber entity.
12+
*/
13+
class GenericButton
14+
{
15+
private UrlInterface $urlBuilder;
16+
private RequestInterface $request;
17+
18+
public function __construct(
19+
Context $context
20+
) {
21+
$this->urlBuilder = $context->getUrlBuilder();
22+
$this->request = $context->getRequest();
23+
}
24+
25+
public function getSubscriptionId(): int
26+
{
27+
return (int)$this->request->getParam('subscription_id');
28+
}
29+
30+
/**
31+
* Wrap button specific options to settings array
32+
*/
33+
protected function wrapButtonSettings(
34+
string $label,
35+
string $class,
36+
string $onclick = '',
37+
array $dataAttribute = [],
38+
int $sortOrder = 0
39+
): array {
40+
return [
41+
'label' => $label,
42+
'on_click' => $onclick,
43+
'data_attribute' => $dataAttribute,
44+
'class' => $class,
45+
'sort_order' => $sortOrder
46+
];
47+
}
48+
49+
protected function getUrl(string $route, array $params = []): string
50+
{
51+
return $this->urlBuilder->getUrl($route, $params);
52+
}
53+
}

Block/Form/AsyncEvent/Save.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\AsyncEventsAdminUi\Block\Form\AsyncEvent;
5+
6+
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
7+
8+
/**
9+
* Save entity button.
10+
*/
11+
class Save extends GenericButton implements ButtonProviderInterface
12+
{
13+
/**
14+
* Retrieve Save button settings.
15+
*/
16+
public function getButtonData(): array
17+
{
18+
return $this->wrapButtonSettings(
19+
__('Save')->getText(),
20+
'save primary',
21+
'',
22+
[
23+
'mage-init' => ['button' => ['event' => 'save']],
24+
'form-role' => 'save'
25+
],
26+
30
27+
);
28+
}
29+
}

Command/AsyncEvent/SaveCommand.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\AsyncEventsAdminUi\Command\AsyncEvent;
5+
6+
use Exception;
7+
use Magento\Framework\Encryption\EncryptorInterface;
8+
use MageOS\AsyncEvents\Api\Data\AsyncEventInterface;
9+
use MageOS\AsyncEvents\Model\ResourceModel\AsyncEvent as AsyncEventResource;
10+
use Magento\Framework\Exception\CouldNotSaveException;
11+
use Psr\Log\LoggerInterface;
12+
13+
/**
14+
* Save AsyncEvent Command.
15+
*/
16+
class SaveCommand
17+
{
18+
public function __construct(
19+
private readonly LoggerInterface $logger,
20+
private readonly AsyncEventResource $resource,
21+
private readonly EncryptorInterface $encryptor
22+
) {
23+
}
24+
25+
/**
26+
* Save AsyncEvent.
27+
*
28+
* @throws CouldNotSaveException
29+
*/
30+
public function execute(AsyncEventInterface $asyncEvent): int
31+
{
32+
try {
33+
$asyncEvent->setHasDataChanges(true);
34+
35+
if (!$asyncEvent->getSubscriptionId()) {
36+
$asyncEvent->isObjectNew(true);
37+
$asyncEvent->setSubscribedAt((new \DateTime())->format(\DateTimeInterface::ATOM));
38+
$secretVerificationToken = $this->encryptor->encrypt($asyncEvent->getVerificationToken());
39+
$asyncEvent->setVerificationToken($secretVerificationToken);
40+
}
41+
$this->resource->save($asyncEvent);
42+
} catch (Exception $exception) {
43+
$this->logger->error(
44+
__('Could not save Asynchronous Event Subscriber. Original message: {message}'),
45+
[
46+
'message' => $exception->getMessage(),
47+
'exception' => $exception
48+
]
49+
);
50+
throw new CouldNotSaveException(__('Could not save Asynchronous Event Subscriber.'));
51+
}
52+
53+
return (int)$asyncEvent->getSubscriptionId();
54+
}
55+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\AsyncEventsAdminUi\Controller\Adminhtml\Events;
5+
6+
use Magento\Backend\App\Action;
7+
use Magento\Backend\Model\View\Result\Page;
8+
use Magento\Framework\App\Action\HttpGetActionInterface;
9+
use Magento\Framework\Controller\ResultFactory;
10+
use Magento\Framework\Controller\ResultInterface;
11+
12+
/**
13+
* Edit AsyncEvent entity backend controller.
14+
*/
15+
class Edit extends Action implements HttpGetActionInterface
16+
{
17+
/**
18+
* Authorization level of a basic admin session.
19+
*
20+
* @see _isAllowed()
21+
*/
22+
public const ADMIN_RESOURCE = 'MageOS_AsyncEvents::async_events_create';
23+
24+
/**
25+
* Edit AsyncEvent action.
26+
*
27+
* @return Page|ResultInterface
28+
*/
29+
public function execute()
30+
{
31+
/** @var Page $resultPage */
32+
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
33+
$resultPage->setActiveMenu('MageOS_AsyncEvents::index');
34+
$resultPage->getConfig()->getTitle()->prepend(__('Edit Asynchronous Event Subscriber'));
35+
36+
return $resultPage;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\AsyncEventsAdminUi\Controller\Adminhtml\Events;
5+
6+
use Magento\Backend\App\Action;
7+
use Magento\Backend\Model\View\Result\Page;
8+
use Magento\Framework\App\Action\HttpGetActionInterface;
9+
use Magento\Framework\Controller\ResultFactory;
10+
use Magento\Framework\Controller\ResultInterface;
11+
12+
/**
13+
* New action AsyncEvent controller.
14+
*/
15+
class NewAction extends Action implements HttpGetActionInterface
16+
{
17+
/**
18+
* Authorization level of a basic admin session.
19+
*
20+
* @see _isAllowed()
21+
*/
22+
public const ADMIN_RESOURCE = 'MageOS_AsyncEvents::async_events_create';
23+
24+
/**
25+
* Create new AsyncEvent action.
26+
*
27+
* @return Page|ResultInterface
28+
*/
29+
public function execute()
30+
{
31+
/** @var Page $resultPage */
32+
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
33+
$resultPage->setActiveMenu('MageOS_AsyncEvents::index');
34+
$resultPage->getConfig()->getTitle()->prepend(__('New Asynchronous Event Subscriber'));
35+
36+
return $resultPage;
37+
}
38+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\AsyncEventsAdminUi\Controller\Adminhtml\Events;
5+
6+
use MageOS\AsyncEvents\Api\Data\AsyncEventInterface;
7+
use MageOS\AsyncEvents\Api\Data\AsyncEventInterfaceFactory;
8+
use MageOS\AsyncEventsAdminUi\Command\AsyncEvent\SaveCommand;
9+
use Magento\Backend\App\Action;
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Framework\App\Action\HttpPostActionInterface;
12+
use Magento\Framework\App\Request\DataPersistorInterface;
13+
use Magento\Framework\App\ResponseInterface;
14+
use Magento\Framework\Controller\ResultInterface;
15+
use Magento\Framework\Exception\CouldNotSaveException;
16+
17+
/**
18+
* Save AsyncEvent controller action.
19+
*/
20+
class Save extends Action implements HttpPostActionInterface
21+
{
22+
/**
23+
* Authorization level of a basic admin session
24+
*
25+
* @see _isAllowed()
26+
*/
27+
public const ADMIN_RESOURCE = 'MageOS_AsyncEvents::async_events_create';
28+
29+
private DataPersistorInterface $dataPersistor;
30+
private SaveCommand $saveCommand;
31+
private AsyncEventInterfaceFactory $asyncEventFactory;
32+
33+
public function __construct(
34+
Context $context,
35+
DataPersistorInterface $dataPersistor,
36+
SaveCommand $saveCommand,
37+
AsyncEventInterfaceFactory $entityDataFactory
38+
) {
39+
parent::__construct($context);
40+
$this->dataPersistor = $dataPersistor;
41+
$this->saveCommand = $saveCommand;
42+
$this->asyncEventFactory = $entityDataFactory;
43+
}
44+
45+
/**
46+
* Save AsyncEvent Action.
47+
*
48+
* @return ResponseInterface|ResultInterface
49+
*/
50+
public function execute()
51+
{
52+
$resultRedirect = $this->resultRedirectFactory->create();
53+
$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+
}
60+
61+
try {
62+
/** @var AsyncEventInterface $entityModel */
63+
$entityModel = $this->asyncEventFactory->create();
64+
$entityModel->addData($params);
65+
$this->saveCommand->execute($entityModel);
66+
$this->messageManager->addSuccessMessage(
67+
__('The Asynchronous Event Subscriber data was saved successfully')
68+
);
69+
$this->dataPersistor->clear('entity');
70+
} catch (CouldNotSaveException $exception) {
71+
$this->messageManager->addErrorMessage($exception->getMessage());
72+
$this->dataPersistor->set('entity', $params);
73+
74+
return $resultRedirect->setPath('*/*/edit', [
75+
'subscription_id' => $this->getRequest()->getParam('subscription_id')
76+
]);
77+
}
78+
79+
return $resultRedirect->setPath('*/*/');
80+
}
81+
}

Model/Config.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\AsyncEventsAdminUi\Model;
5+
6+
use Magento\Framework\Config\DataInterface;
7+
8+
class Config
9+
{
10+
public function __construct(
11+
private readonly DataInterface $dataStorage
12+
) {
13+
}
14+
15+
/**
16+
* Retrieve the content of all async_events.xml files
17+
*/
18+
public function get(?string $key = null): array
19+
{
20+
return $this->dataStorage->get($key, []);
21+
}
22+
}

Query/AsyncEvent/GetListQuery.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\AsyncEventsAdminUi\Query\AsyncEvent;
5+
6+
use MageOS\AsyncEvents\Model\ResourceModel\AsyncEvent\Collection as AsyncEventCollection;
7+
use MageOS\AsyncEvents\Model\ResourceModel\AsyncEvent\CollectionFactory as AsyncEventCollectionFactory;
8+
use Magento\Framework\Api\SearchCriteriaBuilder;
9+
use Magento\Framework\Api\SearchCriteriaInterface;
10+
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
11+
use Magento\Framework\Api\SearchResultsInterface;
12+
use Magento\Framework\Api\SearchResultsInterfaceFactory;
13+
14+
/**
15+
* Get AsyncEvent list by search criteria query.
16+
*/
17+
class GetListQuery
18+
{
19+
public function __construct(
20+
private readonly CollectionProcessorInterface $collectionProcessor,
21+
private readonly AsyncEventCollectionFactory $entityCollectionFactory,
22+
private readonly SearchCriteriaBuilder $searchCriteriaBuilder,
23+
private readonly SearchResultsInterfaceFactory $searchResultFactory
24+
) {
25+
}
26+
27+
/**
28+
* Get AsyncEvent list by search criteria.
29+
*/
30+
public function execute(?SearchCriteriaInterface $searchCriteria = null): SearchResultsInterface
31+
{
32+
/** @var AsyncEventCollection $collection */
33+
$collection = $this->entityCollectionFactory->create();
34+
35+
if ($searchCriteria === null) {
36+
$searchCriteria = $this->searchCriteriaBuilder->create();
37+
} else {
38+
$this->collectionProcessor->process($searchCriteria, $collection);
39+
}
40+
41+
/** @var SearchResultsInterface $searchResult */
42+
$searchResult = $this->searchResultFactory->create();
43+
$searchResult->setItems($collection->getItems());
44+
$searchResult->setTotalCount($collection->getSize());
45+
$searchResult->setSearchCriteria($searchCriteria);
46+
47+
return $searchResult;
48+
}
49+
}

0 commit comments

Comments
 (0)