Skip to content

Commit 13c22e1

Browse files
authored
Merge pull request #1781 from sivaschenko/model-view-responsibility-separation
[Refactoring] Model view responsibility separation
2 parents 89f9ccc + 9f63d21 commit 13c22e1

File tree

11 files changed

+116
-92
lines changed

11 files changed

+116
-92
lines changed

AdobeStockAdminUi/Controller/Adminhtml/System/Config/TestConnection.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212
use Magento\AdobeStockClientApi\Api\ClientInterface;
1313
use Magento\Backend\App\Action;
1414
use Magento\Backend\App\Action\Context;
15-
use Magento\Framework\App\Action\HttpGetActionInterface;
16-
use Magento\Framework\Controller\Result\Json;
15+
use Magento\Framework\App\Action\HttpPostActionInterface;
1716
use Magento\Framework\Controller\Result\JsonFactory;
1817
use Magento\Framework\Controller\ResultInterface;
1918

2019
/**
2120
* Controller used for testing connection to Adobe Stock API from stores configuration
2221
*/
23-
class TestConnection extends Action implements HttpGetActionInterface
22+
class TestConnection extends Action implements HttpPostActionInterface
2423
{
2524
/**
2625
* Authorization level of a basic admin session.
@@ -74,7 +73,7 @@ public function __construct(
7473
*
7574
* @return ResultInterface
7675
*/
77-
public function execute() : ResultInterface
76+
public function execute(): ResultInterface
7877
{
7978
try {
8079
$params = $this->getRequest()->getParams();
@@ -89,8 +88,7 @@ public function execute() : ResultInterface
8988
$isConnectionEstablished = false;
9089
}
9190

92-
$resultJson = $this->resultJsonFactory->create();
93-
return $resultJson->setData(
91+
return $this->resultJsonFactory->create()->setData(
9492
[
9593
'success' => $isConnectionEstablished,
9694
'message' => $message->render(),

AdobeStockAdminUi/view/adminhtml/web/js/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ define([
7575
this.visible(false);
7676

7777
$.ajax({
78-
type: 'GET',
78+
type: 'POST',
7979
url: this.url,
8080
dataType: 'json',
8181
data: {

AdobeStockAsset/Model/GetAssetById.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public function execute(int $adobeId): Document
6363

6464
$items = $this->getAssetList->execute($searchCriteria)->getItems();
6565
if (empty($items) || 1 < count($items)) {
66-
$message = __('Requested image doesn\'t exists');
67-
throw new NoSuchEntityException($message);
66+
throw new NoSuchEntityException(__('Requested asset does not exist.'));
6867
}
6968

7069
return reset($items);

AdobeStockAsset/Model/GetAssetList.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Magento\Framework\Api\Search\SearchResultInterface;
1515
use Magento\Framework\Exception\AuthenticationException;
1616
use Magento\Framework\Exception\LocalizedException;
17-
use Magento\Framework\UrlInterface;
1817
use Psr\Log\LoggerInterface;
1918

2019
/**
@@ -32,11 +31,6 @@ class GetAssetList implements GetAssetListInterface
3231
*/
3332
private $client;
3433

35-
/**
36-
* @var UrlInterface
37-
*/
38-
private $url;
39-
4034
/**
4135
* @var LoggerInterface
4236
*/
@@ -45,18 +39,15 @@ class GetAssetList implements GetAssetListInterface
4539
/**
4640
* GetAssetList constructor.
4741
* @param ClientInterface $client
48-
* @param UrlInterface $url
4942
* @param LoggerInterface $log
5043
* @param AppendAttributes $appendAttributes
5144
*/
5245
public function __construct(
5346
ClientInterface $client,
54-
UrlInterface $url,
5547
LoggerInterface $log,
5648
AppendAttributes $appendAttributes
5749
) {
5850
$this->client = $client;
59-
$this->url = $url;
6051
$this->log = $log;
6152
$this->appendAttributes = $appendAttributes;
6253
}
@@ -72,20 +63,7 @@ public function execute(SearchCriteriaInterface $searchCriteria): SearchResultIn
7263

7364
return $searchResult;
7465
} catch (AuthenticationException $exception) {
75-
throw new LocalizedException(
76-
__(
77-
'Failed to authenticate to Adobe Stock API. <br> Please correct the API credentials in '
78-
. '<a href="%1">Configuration → System → Adobe Stock Integration.</a>',
79-
$this->url->getUrl(
80-
'adminhtml/system_config/edit',
81-
[
82-
'section' => 'system',
83-
'_fragment' => 'system_adobe_stock_integration-link'
84-
]
85-
)
86-
),
87-
$exception
88-
);
66+
throw $exception;
8967
} catch (\Exception $exception) {
9068
$this->log->critical($exception);
9169
throw new LocalizedException(

AdobeStockImage/Model/GetRelatedImages.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Api\FilterBuilder;
1414
use Magento\Framework\Api\Search\Document;
1515
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
16+
use Magento\Framework\Exception\AuthenticationException;
1617
use Magento\Framework\Exception\LocalizedException;
1718
use Psr\Log\LoggerInterface;
1819

@@ -92,6 +93,8 @@ public function execute(int $imageId, int $limit): array
9293
);
9394
}
9495
return $relatedImageGroups;
96+
} catch (AuthenticationException $exception) {
97+
throw $exception;
9598
} catch (\Exception $exception) {
9699
$this->logger->critical($exception);
97100
throw new LocalizedException(

AdobeStockImageAdminUi/Controller/Adminhtml/License/License.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\App\Action\HttpPostActionInterface;
1515
use Magento\Framework\Controller\Result\Json;
1616
use Magento\Framework\Controller\ResultFactory;
17+
use Magento\Framework\Exception\AuthenticationException;
1718
use Magento\Framework\Exception\LocalizedException;
1819
use Psr\Log\LoggerInterface;
1920

@@ -90,6 +91,25 @@ public function execute()
9091
'success' => true,
9192
'message' => __('The image was licensed and saved successfully.'),
9293
];
94+
} catch (AuthenticationException $exception) {
95+
$responseCode = self::HTTP_BAD_REQUEST;
96+
$responseContent = [
97+
'success' => false,
98+
'is_licensed' => $isLicensed,
99+
'message' => __(
100+
'Failed to authenticate to Adobe Stock API. <br> Please correct the API credentials in '
101+
. '<a href="%url">Configuration → System → Adobe Stock Integration.</a>',
102+
[
103+
'url' => $this->getUrl(
104+
'adminhtml/system_config/edit',
105+
[
106+
'section' => 'system',
107+
'_fragment' => 'system_adobe_stock_integration-link'
108+
]
109+
)
110+
]
111+
)
112+
];
93113
} catch (LocalizedException $exception) {
94114
$responseCode = self::HTTP_BAD_REQUEST;
95115
$responseContent = [

AdobeStockImageAdminUi/Controller/Adminhtml/License/SaveLicensed.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\App\Action\HttpPostActionInterface;
1414
use Magento\Framework\Controller\Result\Json;
1515
use Magento\Framework\Controller\ResultFactory;
16+
use Magento\Framework\Exception\AuthenticationException;
1617
use Magento\Framework\Exception\LocalizedException;
1718
use Psr\Log\LoggerInterface;
1819

@@ -74,6 +75,24 @@ public function execute()
7475
'success' => true,
7576
'message' => __('You have successfully downloaded the licensed image.'),
7677
];
78+
} catch (AuthenticationException $exception) {
79+
$responseCode = self::HTTP_BAD_REQUEST;
80+
$responseContent = [
81+
'success' => false,
82+
'message' => __(
83+
'Failed to authenticate to Adobe Stock API. <br> Please correct the API credentials in '
84+
. '<a href="%url">Configuration → System → Adobe Stock Integration.</a>',
85+
[
86+
'url' => $this->getUrl(
87+
'adminhtml/system_config/edit',
88+
[
89+
'section' => 'system',
90+
'_fragment' => 'system_adobe_stock_integration-link'
91+
]
92+
)
93+
]
94+
),
95+
];
7796
} catch (LocalizedException $exception) {
7897
$responseCode = self::HTTP_BAD_REQUEST;
7998
$responseContent = [

AdobeStockImageAdminUi/Controller/Adminhtml/Preview/Download.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\App\Action\HttpPostActionInterface;
1515
use Magento\Framework\Controller\Result\Json;
1616
use Magento\Framework\Controller\ResultFactory;
17+
use Magento\Framework\Exception\AuthenticationException;
1718
use Magento\Framework\Exception\LocalizedException;
1819
use Psr\Log\LoggerInterface;
1920

@@ -87,6 +88,24 @@ public function execute()
8788
'success' => true,
8889
'message' => __('You have successfully downloaded the image.'),
8990
];
91+
} catch (AuthenticationException $exception) {
92+
$responseCode = self::HTTP_BAD_REQUEST;
93+
$responseContent = [
94+
'success' => false,
95+
'message' => __(
96+
'Failed to authenticate to Adobe Stock API. <br> Please correct the API credentials in '
97+
. '<a href="%url">Configuration → System → Adobe Stock Integration.</a>',
98+
[
99+
'url' => $this->getUrl(
100+
'adminhtml/system_config/edit',
101+
[
102+
'section' => 'system',
103+
'_fragment' => 'system_adobe_stock_integration-link'
104+
]
105+
)
106+
]
107+
),
108+
];
90109
} catch (LocalizedException $exception) {
91110
$responseCode = self::HTTP_BAD_REQUEST;
92111
$responseContent = [

AdobeStockImageAdminUi/Controller/Adminhtml/Preview/RelatedImages.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\Framework\App\Action\HttpGetActionInterface;
1313
use Magento\Framework\Controller\Result\Json;
1414
use Magento\Framework\Controller\ResultFactory;
15-
use Psr\Log\LoggerInterface;
15+
use Magento\Framework\Exception\AuthenticationException;
1616

1717
/**
1818
* Controller providing related images (same model and same series) for the provided Adobe Stock asset id
@@ -34,26 +34,18 @@ class RelatedImages extends Action implements HttpGetActionInterface
3434
*/
3535
private $getRelatedImages;
3636

37-
/**
38-
* @var LoggerInterface
39-
*/
40-
private $logger;
41-
4237
/**
4338
* RelatedImages constructor.
4439
*
4540
* @param Action\Context $context
4641
* @param GetRelatedImagesInterface $getRelatedImages
47-
* @param LoggerInterface $logger
4842
*/
4943
public function __construct(
5044
Action\Context $context,
51-
GetRelatedImagesInterface $getRelatedImages,
52-
LoggerInterface $logger
45+
GetRelatedImagesInterface $getRelatedImages
5346
) {
5447
parent::__construct($context);
5548
$this->getRelatedImages = $getRelatedImages;
56-
$this->logger = $logger;
5749
}
5850
/**
5951
* @inheritdoc
@@ -72,9 +64,26 @@ public function execute()
7264
'message' => __('Get related images finished successfully'),
7365
'result' => $relatedImages
7466
];
67+
} catch (AuthenticationException $exception) {
68+
$responseCode = self::HTTP_INTERNAL_ERROR;
69+
$responseContent = [
70+
'success' => false,
71+
'message' => __(
72+
'Failed to authenticate to Adobe Stock API. <br> Please correct the API credentials in '
73+
. '<a href="%url">Configuration → System → Adobe Stock Integration.</a>',
74+
[
75+
'url' => $this->getUrl(
76+
'adminhtml/system_config/edit',
77+
[
78+
'section' => 'system',
79+
'_fragment' => 'system_adobe_stock_integration-link'
80+
]
81+
)
82+
]
83+
)
84+
];
7585
} catch (\Exception $exception) {
7686
$responseCode = self::HTTP_INTERNAL_ERROR;
77-
$this->logger->critical($exception);
7887
$responseContent = [
7988
'success' => false,
8089
'message' => __('An error occurred on attempt to fetch related images.'),

AdobeStockImageAdminUi/Model/Listing/DataProvider.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
1515
use Magento\Framework\Api\Search\SearchResultInterface;
1616
use Magento\Framework\App\RequestInterface;
17+
use Magento\Framework\Exception\AuthenticationException;
18+
use Magento\Framework\UrlInterface;
1719
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider as UiComponentDataProvider;
1820

1921
/**
@@ -27,7 +29,11 @@ class DataProvider extends UiComponentDataProvider
2729
private $getImageList;
2830

2931
/**
30-
* DataProvider constructor.
32+
* @var UrlInterface
33+
*/
34+
private $url;
35+
36+
/**
3137
* @param string $name
3238
* @param string $primaryFieldName
3339
* @param string $requestFieldName
@@ -36,6 +42,7 @@ class DataProvider extends UiComponentDataProvider
3642
* @param RequestInterface $request
3743
* @param FilterBuilder $filterBuilder
3844
* @param GetImageListInterface $getImageList
45+
* @param UrlInterface $url
3946
* @param array $meta
4047
* @param array $data
4148
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -49,6 +56,7 @@ public function __construct(
4956
RequestInterface $request,
5057
FilterBuilder $filterBuilder,
5158
GetImageListInterface $getImageList,
59+
UrlInterface $url,
5260
array $meta = [],
5361
array $data = []
5462
) {
@@ -64,6 +72,7 @@ public function __construct(
6472
$data
6573
);
6674
$this->getImageList = $getImageList;
75+
$this->url = $url;
6776
}
6877

6978
/**
@@ -73,6 +82,24 @@ public function getData()
7382
{
7483
try {
7584
return $this->searchResultToOutput($this->getSearchResult());
85+
} catch (AuthenticationException $exception) {
86+
return [
87+
'items' => [],
88+
'totalRecords' => 0,
89+
'errorMessage' => __(
90+
'Failed to authenticate to Adobe Stock API. <br> Please correct the API credentials in '
91+
. '<a href="%url">Configuration → System → Adobe Stock Integration.</a>',
92+
[
93+
'url' => $this->url->getUrl(
94+
'adminhtml/system_config/edit',
95+
[
96+
'section' => 'system',
97+
'_fragment' => 'system_adobe_stock_integration-link'
98+
]
99+
)
100+
]
101+
)
102+
];
76103
} catch (\Exception $exception) {
77104
return [
78105
'items' => [],

0 commit comments

Comments
 (0)