Skip to content

Commit 7923212

Browse files
committed
Merge branch '2.1-develop' of github.com:magento/adobe-stock-integration into generate-renditions--task-1476
2 parents c2479db + 0b0b4aa commit 7923212

File tree

487 files changed

+288
-25044
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

487 files changed

+288
-25044
lines changed

AdobeStockImage/Model/SaveImageFile.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\AdobeStockImage\Model;
99

10-
use Magento\AdobeStockImage\Model\Storage\Delete as StorageDelete;
1110
use Magento\AdobeStockImage\Model\Storage\Save as StorageSave;
1211
use Magento\Framework\Api\Search\Document;
1312
use Magento\Framework\Exception\CouldNotSaveException;
@@ -23,23 +22,15 @@ class SaveImageFile
2322
*/
2423
private $storageSave;
2524

26-
/**
27-
* @var StorageDelete
28-
*/
29-
private $storageDelete;
30-
3125
/**
3226
* SaveImageFile constructor.
3327
*
3428
* @param StorageSave $storageSave
35-
* @param StorageDelete $storageDelete
3629
*/
3730
public function __construct(
38-
StorageSave $storageSave,
39-
StorageDelete $storageDelete
31+
StorageSave $storageSave
4032
) {
4133
$this->storageSave = $storageSave;
42-
$this->storageDelete = $storageDelete;
4334
}
4435

4536
/**
@@ -55,12 +46,15 @@ public function __construct(
5546
public function execute(Document $document, string $url, string $destinationPath): void
5647
{
5748
try {
49+
$allowOverwrite = false;
5850
$pathAttribute = $document->getCustomAttribute('path');
5951
$pathValue = $pathAttribute->getValue();
60-
if (null !== $pathAttribute && $pathValue) {
61-
$this->storageDelete->execute($pathValue);
52+
53+
if (null !== $pathAttribute && $pathValue && $pathValue === $destinationPath) {
54+
$allowOverwrite = true;
6255
}
63-
$this->storageSave->execute($url, $destinationPath);
56+
57+
$this->storageSave->execute($url, $destinationPath, $allowOverwrite);
6458
} catch (LocalizedException $localizedException) {
6559
throw $localizedException;
6660
} catch (\Exception $exception) {

AdobeStockImage/Model/Storage/Save.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ public function __construct(
5959
*
6060
* @param string $imageUrl
6161
* @param string $destinationPath
62+
* @param bool $allowOverwrite
6263
* @throws AlreadyExistsException
6364
* @throws FileSystemException
6465
* @throws LocalizedException
6566
*/
66-
public function execute(string $imageUrl, string $destinationPath): void
67+
public function execute(string $imageUrl, string $destinationPath, bool $allowOverwrite = false): void
6768
{
6869
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
6970

@@ -75,7 +76,7 @@ public function execute(string $imageUrl, string $destinationPath): void
7576
throw new LocalizedException(__('Could not save image: unsupported file type.'));
7677
}
7778

78-
if ($mediaDirectory->isExist($destinationPath)) {
79+
if (!$allowOverwrite && $mediaDirectory->isExist($destinationPath)) {
7980
throw new AlreadyExistsException(__('Image with the same file name already exits.'));
8081
}
8182

AdobeStockImage/Test/Unit/Model/SaveImageFileTest.php

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Magento\AdobeStockImage\Test\Unit\Model;
99

1010
use Magento\AdobeStockImage\Model\SaveImageFile;
11-
use Magento\AdobeStockImage\Model\Storage\Delete as StorageDelete;
1211
use Magento\AdobeStockImage\Model\Storage\Save as StorageSave;
1312
use Magento\Framework\Api\AttributeInterface;
1413
use Magento\Framework\Api\Search\Document;
@@ -27,11 +26,6 @@ class SaveImageFileTest extends TestCase
2726
*/
2827
private $storageSave;
2928

30-
/**
31-
* @var MockObject|StorageDelete
32-
*/
33-
private $storageDelete;
34-
3529
/**
3630
* @var SaveImageFile
3731
*/
@@ -43,13 +37,11 @@ class SaveImageFileTest extends TestCase
4337
protected function setUp(): void
4438
{
4539
$this->storageSave = $this->createMock(StorageSave::class);
46-
$this->storageDelete = $this->createMock(StorageDelete::class);
4740

4841
$this->saveImageFile = (new ObjectManager($this))->getObject(
4942
SaveImageFile::class,
5043
[
51-
'storageSave' => $this->storageSave,
52-
'storageDelete' => $this->storageDelete
44+
'storageSave' => $this->storageSave
5345
]
5446
);
5547
}
@@ -60,18 +52,12 @@ protected function setUp(): void
6052
* @param Document $document
6153
* @param string $url
6254
* @param string $destinationPath
63-
* @param bool $delete
6455
* @dataProvider assetProvider
6556
*/
66-
public function testExecute(Document $document, string $url, string $destinationPath, bool $delete): void
57+
public function testExecute(Document $document, string $url, string $destinationPath): void
6758
{
68-
if ($delete) {
69-
$this->storageDelete->expects($this->once())
70-
->method('execute');
71-
} else {
72-
$this->storageSave->expects($this->once())
73-
->method('execute');
74-
}
59+
$this->storageSave->expects($this->once())
60+
->method('execute');
7561

7662
$this->storageSave->expects($this->once())
7763
->method('execute')
@@ -86,24 +72,16 @@ public function testExecute(Document $document, string $url, string $destination
8672
* @param Document $document
8773
* @param string $url
8874
* @param string $destinationPath
89-
* @param bool $delete
9075
* @dataProvider assetProvider
9176
*/
9277
public function testExecuteWithException(
9378
Document $document,
9479
string $url,
95-
string $destinationPath,
96-
bool $delete
80+
string $destinationPath
9781
): void {
98-
if ($delete) {
99-
$this->storageDelete->expects($this->once())
100-
->method('execute')
101-
->willThrowException(new \Exception('Some Exception'));
102-
} else {
103-
$this->storageSave->expects($this->once())
104-
->method('execute')
105-
->willThrowException(new \Exception('Some Exception'));
106-
}
82+
$this->storageSave->expects($this->once())
83+
->method('execute')
84+
->willThrowException(new \Exception('Some Exception'));
10785

10886
$this->expectException(CouldNotSaveException::class);
10987

@@ -121,14 +99,12 @@ public function assetProvider(): array
12199
[
122100
'document' => $this->getDocument(),
123101
'url' => 'https://as2.ftcdn.net/jpg/500_FemVonDcttCeKiOXFk.jpg',
124-
'destinationPath' => 'path',
125-
'delete' => false
102+
'destinationPath' => 'path'
126103
],
127104
[
128105
'document' => $this->getDocument('filepath.jpg'),
129106
'url' => 'https://as2.ftcdn.net/jpg/500_FemVonDcttCeKiOXFk.jpg',
130107
'destinationPath' => 'path',
131-
'delete' => true
132108
],
133109
];
134110
}

MediaGalleryUi/Controller/Adminhtml/Directories/GetTree.php renamed to AdobeStockImageAdminUi/Controller/Adminhtml/Asset/GetMediaGalleryAsset.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,90 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
declare(strict_types=1);
78

8-
namespace Magento\MediaGalleryUi\Controller\Adminhtml\Directories;
9+
namespace Magento\AdobeStockImageAdminUi\Controller\Adminhtml\Asset;
910

1011
use Magento\Backend\App\Action;
11-
use Magento\Framework\App\Action\HttpGetActionInterface;
12-
use Magento\Framework\Controller\Result\Json;
12+
use Magento\Framework\App\Action\HttpPostActionInterface;
1313
use Magento\Framework\Controller\ResultFactory;
14-
use Magento\MediaGalleryUi\Model\Directories\FolderTree;
1514
use Psr\Log\LoggerInterface;
15+
use Magento\AdobeStockImageAdminUi\Model\Asset\GetMediaGalleryAssetByAdobeId;
1616

1717
/**
18-
* Returns all available directories
18+
* Backend controller for retrieving asset information by adobeId
1919
*/
20-
class GetTree extends Action implements HttpGetActionInterface
20+
class GetMediaGalleryAsset extends Action implements HttpPostActionInterface
2121
{
2222
private const HTTP_OK = 200;
2323
private const HTTP_INTERNAL_ERROR = 500;
24+
private const HTTP_BAD_REQUEST = 400;
2425

2526
/**
2627
* @see _isAllowed()
2728
*/
2829
public const ADMIN_RESOURCE = 'Magento_Cms::media_gallery';
2930

3031
/**
31-
* @var LoggerInterface
32+
* @var GetMediaGalleryAssetByAdobeId
3233
*/
33-
private $logger;
34+
private $getAssetByAdobeId;
3435

3536
/**
36-
* @var FolderTree
37+
* @var LoggerInterface
3738
*/
38-
private $folderTree;
39+
private $logger;
3940

4041
/**
41-
* Constructor
42-
*
4342
* @param Action\Context $context
4443
* @param LoggerInterface $logger
45-
* @param FolderTree $folderTree
44+
* @param GetMediaGalleryAssetByAdobeId $getAssetByAdobeId
4645
*/
4746
public function __construct(
4847
Action\Context $context,
4948
LoggerInterface $logger,
50-
FolderTree $folderTree
49+
GetMediaGalleryAssetByAdobeId $getAssetByAdobeId
5150
) {
5251
parent::__construct($context);
52+
5353
$this->logger = $logger;
54-
$this->folderTree = $folderTree;
54+
$this->getAssetByAdobeId = $getAssetByAdobeId;
5555
}
56+
5657
/**
5758
* @inheritdoc
5859
*/
5960
public function execute()
6061
{
62+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
6163
try {
62-
$responseContent[] = $this->folderTree->buildTree();
64+
65+
$params = $this->getRequest()->getParams();
66+
$adobeId = isset($params['adobe_id']) ? $params['adobe_id'] : null;
67+
68+
if (empty($adobeId)) {
69+
$responseContent = [
70+
'success' => false,
71+
'message' => __('Adobe id is required.'),
72+
];
73+
$resultJson->setHttpResponseCode(self::HTTP_BAD_REQUEST);
74+
$resultJson->setData($responseContent);
75+
76+
return $resultJson;
77+
}
78+
6379
$responseCode = self::HTTP_OK;
80+
$responseContent = $this->getAssetByAdobeId->execute((int) $adobeId);
6481
} catch (\Exception $exception) {
65-
$this->logger->critical($exception);
6682
$responseCode = self::HTTP_INTERNAL_ERROR;
83+
$this->logger->critical($exception);
6784
$responseContent = [
6885
'success' => false,
69-
'message' => __('Retrieving directories list failed.'),
86+
'message' => __('An error occurred on attempt to retrieve asset information.'),
7087
];
7188
}
7289

73-
/** @var Json $resultJson */
74-
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
7590
$resultJson->setHttpResponseCode($responseCode);
7691
$resultJson->setData($responseContent);
7792

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AdobeStockImageAdminUi\Model\Asset;
10+
11+
use Magento\AdobeStockAssetApi\Model\Asset\Command\LoadByIdsInterface;
12+
use Magento\MediaGalleryApi\Api\GetAssetsByIdsInterface;
13+
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
14+
use Magento\Framework\Reflection\DataObjectProcessor;
15+
16+
/**
17+
* Return media gallery asset by adobe id
18+
*/
19+
class GetMediaGalleryAssetByAdobeId
20+
{
21+
/**
22+
* @var LoadByIdsInterface
23+
*/
24+
private $getAssetByAdobeId;
25+
26+
/**
27+
* @var GetAssetsByIdsInterface
28+
*/
29+
private $getMediaGalleryAssetsById;
30+
31+
/**
32+
* @var DataObjectProcessor
33+
*/
34+
private $objectProcessor;
35+
36+
/**
37+
* Constructor
38+
*
39+
* @param LoadByIdsInterface $getAssetByAdobeId
40+
* @param GetAssetsByIdsInterface $getAssetById
41+
* @param DataObjectProcessor $objectProcessor
42+
*/
43+
public function __construct(
44+
LoadByIdsInterface $getAssetByAdobeId,
45+
GetAssetsByIdsInterface $getAssetById,
46+
DataObjectProcessor $objectProcessor
47+
) {
48+
$this->getAssetByAdobeId = $getAssetByAdobeId;
49+
$this->getMediaGalleryAssetsById = $getAssetById;
50+
$this->objectProcessor = $objectProcessor;
51+
}
52+
53+
/**
54+
* Return media gallery asset by adobe id
55+
*
56+
* @param int $adobeId
57+
* @return array
58+
*/
59+
public function execute(int $adobeId): array
60+
{
61+
$mediaGalleryId = $this->getAssetByAdobeId->execute([$adobeId])[$adobeId]->getMediaGalleryId();
62+
$asset = $this->getMediaGalleryAssetsById->execute([$mediaGalleryId]);
63+
64+
return $this->objectProcessor->buildOutputDataArray(current($asset), AssetInterface::class);
65+
}
66+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AdobeStockImageAdminUi\Plugin;
10+
11+
use Magento\MediaGalleryUi\Ui\Component\Listing\Columns\Source\Options;
12+
13+
/**
14+
* Plugin which adds an Adobe Stock option to surce filter in media gallery
15+
*/
16+
class AddAdobeStockSourceOptionPlugin
17+
{
18+
/**
19+
* Add Adobe Stock source option
20+
*
21+
* @param Options $subject
22+
* @param array $options
23+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
24+
*/
25+
public function aftertoOptionArray(Options $subject, array $options): array
26+
{
27+
$options[] = [
28+
'value' => 'Adobe Stock',
29+
'label' => __('Adobe Stock'),
30+
];
31+
32+
return $options;
33+
}
34+
}

0 commit comments

Comments
 (0)