Skip to content

Commit 4fb1018

Browse files
authored
Merge pull request #1745 from magento/2.0-develop
Sync 2.0-master with 2.0-develop
2 parents 43db2fd + 18dcdf0 commit 4fb1018

File tree

35 files changed

+348
-519
lines changed

35 files changed

+348
-519
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
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\Controller\Adminhtml\Asset;
10+
11+
use Magento\Backend\App\Action;
12+
use Magento\Framework\App\Action\HttpPostActionInterface;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Psr\Log\LoggerInterface;
15+
use Magento\AdobeStockImageAdminUi\Model\Asset\GetMediaGalleryAssetByAdobeId;
16+
17+
/**
18+
* Backend controller for retrieving asset information by adobeId
19+
*/
20+
class GetMediaGalleryAsset extends Action implements HttpPostActionInterface
21+
{
22+
private const HTTP_OK = 200;
23+
private const HTTP_INTERNAL_ERROR = 500;
24+
private const HTTP_BAD_REQUEST = 400;
25+
26+
/**
27+
* @see _isAllowed()
28+
*/
29+
public const ADMIN_RESOURCE = 'Magento_Cms::media_gallery';
30+
31+
/**
32+
* @var GetMediaGalleryAssetByAdobeId
33+
*/
34+
private $getAssetByAdobeId;
35+
36+
/**
37+
* @var LoggerInterface
38+
*/
39+
private $logger;
40+
41+
/**
42+
* @param Action\Context $context
43+
* @param LoggerInterface $logger
44+
* @param GetMediaGalleryAssetByAdobeId $getAssetByAdobeId
45+
*/
46+
public function __construct(
47+
Action\Context $context,
48+
LoggerInterface $logger,
49+
GetMediaGalleryAssetByAdobeId $getAssetByAdobeId
50+
) {
51+
parent::__construct($context);
52+
53+
$this->logger = $logger;
54+
$this->getAssetByAdobeId = $getAssetByAdobeId;
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
public function execute()
61+
{
62+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
63+
try {
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+
79+
$responseCode = self::HTTP_OK;
80+
$responseContent = $this->getAssetByAdobeId->execute((int) $adobeId);
81+
} catch (\Exception $exception) {
82+
$responseCode = self::HTTP_INTERNAL_ERROR;
83+
$this->logger->critical($exception);
84+
$responseContent = [
85+
'success' => false,
86+
'message' => __('An error occurred on attempt to retrieve asset information.'),
87+
];
88+
}
89+
90+
$resultJson->setHttpResponseCode($responseCode);
91+
$resultJson->setData($responseContent);
92+
93+
return $resultJson;
94+
}
95+
}
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)