Skip to content

Commit 30ce748

Browse files
author
Stanislav Idolov
committed
#27499: Code review fixes
1 parent 8e2337f commit 30ce748

File tree

15 files changed

+74
-98
lines changed

15 files changed

+74
-98
lines changed

app/code/Magento/MediaGallery/Model/Asset/Command/Save.php

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ class Save implements SaveInterface
2828
*/
2929
private $resourceConnection;
3030

31-
/**
32-
* @var DataObjectProcessor
33-
*/
34-
private $objectProcessor;
35-
3631
/**
3732
* @var LoggerInterface
3833
*/
@@ -42,16 +37,13 @@ class Save implements SaveInterface
4237
* Save constructor.
4338
*
4439
* @param ResourceConnection $resourceConnection
45-
* @param DataObjectProcessor $objectProcessor
4640
* @param LoggerInterface $logger
4741
*/
4842
public function __construct(
4943
ResourceConnection $resourceConnection,
50-
DataObjectProcessor $objectProcessor,
5144
LoggerInterface $logger
5245
) {
5346
$this->resourceConnection = $resourceConnection;
54-
$this->objectProcessor = $objectProcessor;
5547
$this->logger = $logger;
5648
}
5749

@@ -72,37 +64,24 @@ public function execute(AssetInterface $mediaAsset): int
7264

7365
$connection->insertOnDuplicate(
7466
$tableName,
75-
$this->filterData($this->objectProcessor->buildOutputDataArray($mediaAsset, AssetInterface::class))
67+
[
68+
'id' => $mediaAsset->getId(),
69+
'path' => $mediaAsset->getPath(),
70+
'title' => $mediaAsset->getTitle(),
71+
'source' => $mediaAsset->getSource(),
72+
'content_type' => $mediaAsset->getContentType(),
73+
'width' => $mediaAsset->getWidth(),
74+
'height' => $mediaAsset->getHeight(),
75+
'size' => $mediaAsset->getSize(),
76+
'created_at' => $mediaAsset->getCreatedAt(),
77+
'updated_at' => $mediaAsset->getUpdatedAt(),
78+
]
7679
);
77-
return (int) $connection->lastInsertId($tableName);
80+
return (int)$connection->lastInsertId($tableName);
7881
} catch (\Exception $exception) {
7982
$this->logger->critical($exception);
8083
$message = __('An error occurred during media asset save: %1', $exception->getMessage());
8184
throw new CouldNotSaveException($message, $exception);
8285
}
8386
}
84-
85-
/**
86-
* Filter data to get flat array without null values
87-
*
88-
* @param array $data
89-
* @return array
90-
*/
91-
private function filterData(array $data): array
92-
{
93-
$filteredData = [];
94-
foreach ($data as $key => $value) {
95-
if ($value === null) {
96-
continue;
97-
}
98-
if (is_array($value)) {
99-
continue;
100-
}
101-
if (is_object($value)) {
102-
continue;
103-
}
104-
$filteredData[$key] = $value;
105-
}
106-
return $filteredData;
107-
}
10887
}

app/code/Magento/MediaGallery/Model/Directory/Config.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
namespace Magento\MediaGallery\Model\Directory;
99

1010
use Magento\Framework\Config\DataInterface;
11+
use Magento\MediaGalleryApi\Model\BlacklistPatternsConfigInterface;
1112

1213
/**
1314
* Media gallery directory config
1415
*/
15-
class Config
16+
class Config implements BlacklistPatternsConfigInterface
1617
{
1718
private const XML_PATH_BLACKLIST_PATTERNS = 'blacklist/patterns';
1819

@@ -29,25 +30,13 @@ public function __construct(DataInterface $data)
2930
$this->data = $data;
3031
}
3132

32-
/**
33-
* Get config value by key.
34-
*
35-
* @param string|null $key
36-
* @param string|null $default
37-
* @return array
38-
*/
39-
public function get($key = null, $default = null)
40-
{
41-
return $this->data->get($key, $default);
42-
}
43-
4433
/**
4534
* Returns list of blacklist regexp patterns
4635
*
4736
* @return array
4837
*/
49-
public function getBlacklistPatterns() : array
38+
public function get() : array
5039
{
51-
return $this->get(self::XML_PATH_BLACKLIST_PATTERNS);
40+
return $this->data->get(self::XML_PATH_BLACKLIST_PATTERNS);
5241
}
5342
}

app/code/Magento/MediaGallery/Model/Directory/IsBlacklisted.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@
88
namespace Magento\MediaGallery\Model\Directory;
99

1010
use Magento\MediaGalleryApi\Api\IsPathBlacklistedInterface;
11+
use Magento\MediaGalleryApi\Model\BlacklistPatternsConfigInterface;
1112

1213
/**
1314
* Check if the path is blacklisted for media gallery. Directory path may be blacklisted if it's reserved by the system
1415
*/
1516
class IsBlacklisted implements IsPathBlacklistedInterface
1617
{
1718
/**
18-
* @var Config
19+
* @var BlacklistPatternsConfigInterface
1920
*/
2021
private $config;
2122

22-
/**
23-
* @param Config $config
23+
/*
24+
* @param BlacklistPatternsConfigInterface $config
2425
*/
25-
public function __construct(Config $config)
26+
public function __construct(BlacklistPatternsConfigInterface $config)
2627
{
2728
$this->config = $config;
2829
}
@@ -35,7 +36,7 @@ public function __construct(Config $config)
3536
*/
3637
public function execute(string $path): bool
3738
{
38-
foreach ($this->config->getBlacklistPatterns() as $pattern) {
39+
foreach ($this->config->get() as $pattern) {
3940
if (empty($pattern)) {
4041
continue;
4142
}

app/code/Magento/MediaGallery/Model/ResourceModel/SaveAssets.php

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ class SaveAssets implements SaveAssetsInterface
2626
*/
2727
private $resourceConnection;
2828

29-
/**
30-
* @var DataObjectProcessor
31-
*/
32-
private $objectProcessor;
33-
3429
/**
3530
* @var LoggerInterface
3631
*/
@@ -40,16 +35,13 @@ class SaveAssets implements SaveAssetsInterface
4035
* Save constructor.
4136
*
4237
* @param ResourceConnection $resourceConnection
43-
* @param DataObjectProcessor $objectProcessor
4438
* @param LoggerInterface $logger
4539
*/
4640
public function __construct(
4741
ResourceConnection $resourceConnection,
48-
DataObjectProcessor $objectProcessor,
4942
LoggerInterface $logger
5043
) {
5144
$this->resourceConnection = $resourceConnection;
52-
$this->objectProcessor = $objectProcessor;
5345
$this->logger = $logger;
5446
}
5547

@@ -66,7 +58,18 @@ public function execute(array $assets): void
6658
try {
6759
$connection->insertOnDuplicate(
6860
$tableName,
69-
$this->filterData($this->objectProcessor->buildOutputDataArray($asset, AssetInterface::class))
61+
[
62+
'id' => $asset->getId(),
63+
'path' => $asset->getPath(),
64+
'title' => $asset->getTitle(),
65+
'source' => $asset->getSource(),
66+
'content_type' => $asset->getContentType(),
67+
'width' => $asset->getWidth(),
68+
'height' => $asset->getHeight(),
69+
'size' => $asset->getSize(),
70+
'created_at' => $asset->getCreatedAt(),
71+
'updated_at' => $asset->getUpdatedAt(),
72+
]
7073
);
7174
} catch (\Exception $exception) {
7275
$this->logger->critical($exception);
@@ -85,28 +88,4 @@ public function execute(array $assets): void
8588
);
8689
}
8790
}
88-
89-
/**
90-
* Filter data to get flat array without null values
91-
*
92-
* @param array $data
93-
* @return array
94-
*/
95-
private function filterData(array $data): array
96-
{
97-
$filteredData = [];
98-
foreach ($data as $key => $value) {
99-
if ($value === null) {
100-
continue;
101-
}
102-
if (is_array($value)) {
103-
continue;
104-
}
105-
if (is_object($value)) {
106-
continue;
107-
}
108-
$filteredData[$key] = $value;
109-
}
110-
return $filteredData;
111-
}
11291
}

app/code/Magento/MediaGallery/Test/Unit/Model/Directory/IsBlacklistedTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1111
use Magento\MediaGallery\Model\Directory\IsBlacklisted;
1212
use Magento\MediaGallery\Model\Directory\Config;
13+
use Magento\MediaGalleryApi\Model\BlacklistPatternsConfigInterface;
1314
use PHPUnit\Framework\MockObject\MockObject;
1415
use PHPUnit\Framework\TestCase;
1516

@@ -24,7 +25,7 @@ class IsBlacklistedTest extends TestCase
2425
private $object;
2526

2627
/**
27-
* @var Config|MockObject
28+
* @var BlacklistPatternsConfigInterface|MockObject
2829
*/
2930
private $config;
3031

@@ -33,8 +34,8 @@ class IsBlacklistedTest extends TestCase
3334
*/
3435
protected function setUp(): void
3536
{
36-
$this->config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
37-
$this->config->expects($this->at(0))->method('getBlacklistPatterns')->willReturn([
37+
$this->config = $this->getMockBuilder(BlacklistPatternsConfigInterface::class)->disableOriginalConstructor()->getMock();
38+
$this->config->expects($this->at(0))->method('get')->willReturn([
3839
'tmp' => '/pub\/media\/tmp/',
3940
'captcha' => '/pub\/media\/captcha/'
4041
]);

app/code/Magento/MediaGallery/etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@
5656
<argument name="data" xsi:type="object">Magento\MediaGallery\Model\Directory\Config\Data</argument>
5757
</arguments>
5858
</type>
59+
60+
<preference for="Magento\MediaGalleryApi\Model\BlacklistPatternsConfigInterface" type="Magento\MediaGallery\Model\Directory\Config"/>
5961
</config>

app/code/Magento/MediaGalleryApi/Api/CreateDirectoriesByPathsInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface CreateDirectoriesByPathsInterface
1717
* Create new directories by provided paths
1818
*
1919
* @param string[] $paths
20+
* @return void
2021
* @throws \Magento\Framework\Exception\CouldNotSaveException
2122
*/
2223
public function execute(array $paths): void;

app/code/Magento/MediaGalleryApi/Api/DeleteDirectoriesByPathsInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface DeleteDirectoriesByPathsInterface
1717
* Deletes the existing folders
1818
*
1919
* @param string[] $paths
20+
* @return void
2021
* @throws \Magento\Framework\Exception\CouldNotDeleteException
2122
*/
2223
public function execute(array $paths): void;

app/code/Magento/MediaGalleryApi/Api/IsPathBlacklistedInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
namespace Magento\MediaGalleryApi\Api;
99

1010
/**
11-
* Directory paths that are reserved by system and not be included in the media gallery
11+
* Check if the path is blacklisted for media gallery.
12+
*
13+
* Directory path may be blacklisted if it's reserved by the system.
1214
* @api
1315
*/
1416
interface IsPathBlacklistedInterface

app/code/Magento/MediaGalleryApi/Api/SaveAssetsInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface SaveAssetsInterface
1818
* Save media asset. The saved asset can later be retrieved by path
1919
*
2020
* @param \Magento\MediaGalleryApi\Api\Data\AssetInterface[] $assets
21+
* @return void
2122
* @throws \Magento\Framework\Exception\CouldNotSaveException
2223
*/
2324
public function execute(array $assets): void;

0 commit comments

Comments
 (0)