Skip to content

Commit 0a5b571

Browse files
ENGCOM-6293: REFACTORING: adjust commands to the common style in the MediaGallery and change exception handle logic #25614
- Merge Pull Request #25614 from coderimus/magento2:refactoring/exception-handling - Merged commits: 1. 3a82b4a 2. e694954 3. 8f7085f 4. 20a6a37 5. d282c10
2 parents 8ebf827 + d282c10 commit 0a5b571

File tree

12 files changed

+104
-31
lines changed

12 files changed

+104
-31
lines changed

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

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

88
namespace Magento\MediaGallery\Model\Asset\Command;
99

10-
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface;
1110
use Magento\Framework\App\ResourceConnection;
12-
use Magento\Framework\Exception\CouldNotDeleteException;
1311
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\Exception\CouldNotDeleteException;
13+
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface;
1414
use Psr\Log\LoggerInterface;
1515

1616
/**
@@ -62,11 +62,11 @@ public function execute(string $mediaAssetPath): void
6262
$tableName = $this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET);
6363
$connection->delete($tableName, [self::MEDIA_GALLERY_ASSET_PATH . ' = ?' => $mediaAssetPath]);
6464
} catch (\Exception $exception) {
65+
$this->logger->critical($exception);
6566
$message = __(
6667
'Could not delete media asset with path %path: %error',
6768
['path' => $mediaAssetPath, 'error' => $exception->getMessage()]
6869
);
69-
$this->logger->critical($message);
7070
throw new CouldNotDeleteException($message, $exception);
7171
}
7272
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,23 @@ public function execute(int $mediaAssetId): AssetInterface
7373
->where('amg.id = ?', $mediaAssetId);
7474
$mediaAssetData = $connection->query($select)->fetch();
7575
} catch (\Exception $exception) {
76+
$this->logger->critical($exception);
7677
$message = __(
7778
'En error occurred during get media asset data by id %id: %error',
7879
['id' => $mediaAssetId, 'error' => $exception->getMessage()]
7980
);
80-
$this->logger->critical($message);
8181
throw new IntegrationException($message, $exception);
8282
}
8383

8484
if (empty($mediaAssetData)) {
85-
$message = __('There is no such media asset with id "%1"', $mediaAssetId);
85+
$message = __('There is no such media asset with id %id', ['id' => $mediaAssetId]);
8686
throw new NoSuchEntityException($message);
8787
}
8888

8989
try {
9090
return $this->assetFactory->create(['data' => $mediaAssetData]);
9191
} catch (\Exception $exception) {
92-
$this->logger->critical($exception->getMessage());
92+
$this->logger->critical($exception);
9393
$message = __(
9494
'En error occurred during initialize media asset with id %id: %error',
9595
['id' => $mediaAssetId, 'error' => $exception->getMessage()]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public function execute(string $mediaFilePath): AssetInterface
8282

8383
return $mediaAssets;
8484
} catch (\Exception $exception) {
85+
$this->logger->critical($exception);
8586
$message = __('An error occurred during get media asset list: %1', $exception->getMessage());
86-
$this->logger->critical($message);
8787
throw new IntegrationException($message, $exception);
8888
}
8989
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public function execute(AssetInterface $mediaAsset): int
7171
$connection->insertOnDuplicate($tableName, $this->extractor->extract($mediaAsset, AssetInterface::class));
7272
return (int) $connection->lastInsertId($tableName);
7373
} catch (\Exception $exception) {
74+
$this->logger->critical($exception);
7475
$message = __('An error occurred during media asset save: %1', $exception->getMessage());
75-
$this->logger->critical($message);
7676
throw new CouldNotSaveException($message, $exception);
7777
}
7878
}

app/code/Magento/MediaGallery/Model/DataExtractor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
class DataExtractor implements DataExtractorInterface
1616
{
1717
/**
18-
* @inheritdoc
18+
* Extract data from an object using available getters (does not process extension attributes)
19+
*
20+
* @param object $object
21+
* @param string|null $interface
22+
*
23+
* @return array
24+
* @throws \ReflectionException
1925
*/
2026
public function extract($object, string $interface = null): array
2127
{

app/code/Magento/MediaGallery/Model/Keyword/Command/GetAssetKeywords.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
namespace Magento\MediaGallery\Model\Keyword\Command;
99

10+
use Magento\Framework\Exception\IntegrationException;
1011
use Magento\MediaGalleryApi\Api\Data\KeywordInterface;
1112
use Magento\MediaGalleryApi\Api\Data\KeywordInterfaceFactory;
1213
use Magento\MediaGalleryApi\Model\Keyword\Command\GetAssetKeywordsInterface;
1314
use Magento\Framework\App\ResourceConnection;
14-
use Magento\Framework\Exception\NotFoundException;
15+
use Psr\Log\LoggerInterface;
1516

1617
/**
1718
* ClassGetAssetKeywords
@@ -31,27 +32,35 @@ class GetAssetKeywords implements GetAssetKeywordsInterface
3132
*/
3233
private $assetKeywordFactory;
3334

35+
/**
36+
* @var LoggerInterface
37+
*/
38+
private $logger;
39+
3440
/**
3541
* GetAssetKeywords constructor.
3642
*
3743
* @param ResourceConnection $resourceConnection
3844
* @param KeywordInterfaceFactory $assetKeywordFactory
45+
* @param LoggerInterface $logger
3946
*/
4047
public function __construct(
4148
ResourceConnection $resourceConnection,
42-
KeywordInterfaceFactory $assetKeywordFactory
49+
KeywordInterfaceFactory $assetKeywordFactory,
50+
LoggerInterface $logger
4351
) {
4452
$this->resourceConnection = $resourceConnection;
4553
$this->assetKeywordFactory = $assetKeywordFactory;
54+
$this->logger = $logger;
4655
}
4756

4857
/**
4958
* Get asset related keywords.
5059
*
5160
* @param int $assetId
5261
*
53-
* @return KeywordInterface[]
54-
* @throws NotFoundException
62+
* @return KeywordInterface[]|[]
63+
* @throws IntegrationException
5564
*/
5665
public function execute(int $assetId): array
5766
{
@@ -71,8 +80,9 @@ public function execute(int $assetId): array
7180

7281
return $keywords;
7382
} catch (\Exception $exception) {
83+
$this->logger->critical($exception);
7484
$message = __('An error occurred during get asset keywords: %1', $exception->getMessage());
75-
throw new NotFoundException($message, $exception);
85+
throw new IntegrationException($message, $exception);
7686
}
7787
}
7888
}

app/code/Magento/MediaGallery/Model/Keyword/Command/SaveAssetKeywords.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\DB\Adapter\AdapterInterface;
1414
use Magento\Framework\DB\Adapter\Pdo\Mysql;
1515
use Magento\Framework\Exception\CouldNotSaveException;
16+
use Psr\Log\LoggerInterface;
1617

1718
/**
1819
* Class SaveAssetKeywords
@@ -33,22 +34,34 @@ class SaveAssetKeywords implements SaveAssetKeywordsInterface
3334
*/
3435
private $saveAssetLinks;
3536

37+
/**
38+
* @var LoggerInterface
39+
*/
40+
private $logger;
41+
3642
/**
3743
* SaveAssetKeywords constructor.
3844
*
3945
* @param ResourceConnection $resourceConnection
4046
* @param SaveAssetLinks $saveAssetLinks
47+
* @param LoggerInterface $logger
4148
*/
4249
public function __construct(
4350
ResourceConnection $resourceConnection,
44-
SaveAssetLinks $saveAssetLinks
51+
SaveAssetLinks $saveAssetLinks,
52+
LoggerInterface $logger
4553
) {
4654
$this->resourceConnection = $resourceConnection;
4755
$this->saveAssetLinks = $saveAssetLinks;
56+
$this->logger = $logger;
4857
}
4958

5059
/**
51-
* @inheritdoc
60+
* Save asset keywords.
61+
*
62+
* @param KeywordInterface[] $keywords
63+
* @param int $assetId
64+
* @throws CouldNotSaveException
5265
*/
5366
public function execute(array $keywords, int $assetId): void
5467
{
@@ -72,6 +85,7 @@ public function execute(array $keywords, int $assetId): void
7285
$this->saveAssetLinks->execute($assetId, $this->getKeywordIds($data));
7386
}
7487
} catch (\Exception $exception) {
88+
$this->logger->critical($exception);
7589
$message = __('An error occurred during save asset keyword: %1', $exception->getMessage());
7690
throw new CouldNotSaveException($message, $exception);
7791
}

app/code/Magento/MediaGallery/Model/Keyword/Command/SaveAssetLinks.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\DB\Adapter\AdapterInterface;
1414
use Magento\Framework\DB\Adapter\Pdo\Mysql;
1515
use Magento\Framework\Exception\CouldNotSaveException;
16+
use Psr\Log\LoggerInterface;
1617

1718
/**
1819
* Class SaveAssetLinks
@@ -29,14 +30,22 @@ class SaveAssetLinks
2930
private $resourceConnection;
3031

3132
/**
32-
* SaveAssetKeywords constructor.
33+
* @var LoggerInterface
34+
*/
35+
private $logger;
36+
37+
/**
38+
* SaveAssetLinks constructor.
3339
*
3440
* @param ResourceConnection $resourceConnection
41+
* @param LoggerInterface $logger
3542
*/
3643
public function __construct(
37-
ResourceConnection $resourceConnection
44+
ResourceConnection $resourceConnection,
45+
LoggerInterface $logger
3846
) {
3947
$this->resourceConnection = $resourceConnection;
48+
$this->logger = $logger;
4049
}
4150

4251
/**
@@ -66,6 +75,7 @@ public function execute(int $assetId, array $keywordIds): void
6675
);
6776
}
6877
} catch (\Exception $exception) {
78+
$this->logger->critical($exception);
6979
$message = __('An error occurred during save asset keyword links: %1', $exception->getMessage());
7080
throw new CouldNotSaveException($message, $exception);
7181
}

app/code/Magento/MediaGallery/Plugin/Product/Gallery/Processor.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public function afterRemoveImage(
6767
try {
6868
$this->deleteMediaAssetByPath->execute($file);
6969
} catch (\Exception $exception) {
70-
$message = __('An error occurred during media asset delete at media processor: %1', $exception->getMessage());
71-
$this->logger->critical($message->render());
70+
$this->logger->critical($exception);
7271
}
7372

7473
return $result;

app/code/Magento/MediaGallery/Test/Unit/Model/Keyword/Command/GetAssetKeywordsTest.php

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

88
namespace Magento\MediaGallery\Test\Unit\Model\Keyword\Command;
99

10+
use Magento\Framework\Exception\IntegrationException;
1011
use Magento\MediaGallery\Model\Keyword\Command\GetAssetKeywords;
1112
use Magento\MediaGalleryApi\Api\Data\KeywordInterface;
1213
use Magento\MediaGalleryApi\Api\Data\KeywordInterfaceFactory;
1314
use Magento\Framework\App\ResourceConnection;
1415
use Magento\Framework\DB\Adapter\AdapterInterface;
1516
use Magento\Framework\DB\Select;
16-
use Magento\Framework\Exception\NotFoundException;
1717
use PHPUnit\Framework\MockObject\MockObject;
1818
use PHPUnit\Framework\TestCase;
19+
use Psr\Log\LoggerInterface;
1920

21+
/**
22+
* GetAssetKeywordsTest
23+
*/
2024
class GetAssetKeywordsTest extends TestCase
2125
{
2226
/**
@@ -34,14 +38,21 @@ class GetAssetKeywordsTest extends TestCase
3438
*/
3539
private $assetKeywordFactoryStub;
3640

41+
/**
42+
* @var LoggerInterface|MockObject
43+
*/
44+
private $loggerMock;
45+
3746
protected function setUp(): void
3847
{
3948
$this->resourceConnectionStub = $this->createMock(ResourceConnection::class);
4049
$this->assetKeywordFactoryStub = $this->createMock(KeywordInterfaceFactory::class);
50+
$this->loggerMock = $this->createMock(LoggerInterface::class);
4151

4252
$this->sut = new GetAssetKeywords(
4353
$this->resourceConnectionStub,
44-
$this->assetKeywordFactoryStub
54+
$this->assetKeywordFactoryStub,
55+
$this->loggerMock
4556
);
4657
}
4758

@@ -51,7 +62,6 @@ protected function setUp(): void
5162
* @dataProvider casesProvider()
5263
* @param array $databaseQueryResult
5364
* @param int $expectedNumberOfFoundKeywords
54-
* @throws NotFoundException
5565
*/
5666
public function testFind(array $databaseQueryResult, int $expectedNumberOfFoundKeywords): void
5767
{
@@ -80,9 +90,9 @@ public function casesProvider(): array
8090
}
8191

8292
/**
83-
* Negative test
93+
* Test case when an error occured during get data request.
8494
*
85-
* @throws NotFoundException
95+
* @throws IntegrationException
8696
*/
8797
public function testNotFoundBecauseOfError(): void
8898
{
@@ -92,7 +102,10 @@ public function testNotFoundBecauseOfError(): void
92102
->method('getConnection')
93103
->willThrowException((new \Exception()));
94104

95-
$this->expectException(NotFoundException::class);
105+
$this->expectException(IntegrationException::class);
106+
$this->loggerMock->expects($this->once())
107+
->method('critical')
108+
->willReturnSelf();
96109

97110
$this->sut->execute($randomAssetId);
98111
}

app/code/Magento/MediaGallery/Test/Unit/Model/Keyword/Command/SaveAssetKeywordsTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77

88
namespace Magento\MediaGallery\Test\Unit\Model\Keyword\Command;
99

10-
use Magento\MediaGallery\Model\Keyword\Command\SaveAssetKeywords;
11-
use Magento\MediaGallery\Model\Keyword\Command\SaveAssetLinks;
1210
use Magento\Framework\App\ResourceConnection;
1311
use Magento\Framework\DataObject;
1412
use Magento\Framework\DB\Adapter\Pdo\Mysql;
1513
use Magento\Framework\DB\Select;
1614
use Magento\Framework\Exception\CouldNotSaveException;
15+
use Magento\MediaGallery\Model\Keyword\Command\SaveAssetKeywords;
16+
use Magento\MediaGallery\Model\Keyword\Command\SaveAssetLinks;
1717
use PHPUnit\Framework\MockObject\MockObject;
1818
use PHPUnit\Framework\TestCase;
19+
use Psr\Log\LoggerInterface;
1920

2021
/**
2122
* SaveAssetKeywordsTest.
@@ -47,6 +48,11 @@ class SaveAssetKeywordsTest extends TestCase
4748
*/
4849
private $selectMock;
4950

51+
/**
52+
* @var LoggerInterface|MockObject
53+
*/
54+
private $loggerMock;
55+
5056
/**
5157
* SetUp
5258
*/
@@ -58,10 +64,12 @@ public function setUp(): void
5864
->disableOriginalConstructor()
5965
->getMock();
6066
$this->selectMock = $this->createMock(Select::class);
67+
$this->loggerMock = $this->createMock(LoggerInterface::class);
6168

6269
$this->sut = new SaveAssetKeywords(
6370
$this->resourceConnectionMock,
64-
$this->saveAssetLinksMock
71+
$this->saveAssetLinksMock,
72+
$this->loggerMock
6573
);
6674
}
6775

@@ -106,6 +114,9 @@ public function testAssetNotSavingCausedByError(): void
106114
->method('getConnection')
107115
->willThrowException((new \Exception()));
108116
$this->expectException(CouldNotSaveException::class);
117+
$this->loggerMock->expects($this->once())
118+
->method('critical')
119+
->willReturnSelf();
109120

110121
$this->sut->execute([$keyword], 1);
111122
}

0 commit comments

Comments
 (0)