Skip to content

Commit e66c9a9

Browse files
author
Stanislav Idolov
committed
#27536: Remove dependency to the MediaGallery module
1 parent ad24d87 commit e66c9a9

File tree

8 files changed

+181
-3
lines changed

8 files changed

+181
-3
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\MediaContent\Model\Plugin;
8+
9+
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\DB\Adapter\AdapterInterface;
11+
use Magento\Framework\Exception\CouldNotDeleteException;
12+
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByDirectoryPathInterface;
13+
use Psr\Log\LoggerInterface;
14+
15+
/**
16+
* Remove media content record after media gallery asset removal.
17+
*/
18+
class MediaGalleryAssetDeleteByDirectoryPath
19+
{
20+
/**
21+
* @var ResourceConnection
22+
*/
23+
private $resourceConnection;
24+
25+
/**
26+
* @var LoggerInterface
27+
*/
28+
private $logger;
29+
30+
/**
31+
* DeleteById constructor.
32+
*
33+
* @param ResourceConnection $resourceConnection
34+
* @param LoggerInterface $logger
35+
*/
36+
public function __construct(
37+
ResourceConnection $resourceConnection,
38+
LoggerInterface $logger
39+
) {
40+
$this->resourceConnection = $resourceConnection;
41+
$this->logger = $logger;
42+
}
43+
44+
/**
45+
* @param DeleteByDirectoryPathInterface $subject
46+
* @param \Closure $proceed
47+
* @param string $directoryPath
48+
* @throws CouldNotDeleteException
49+
* @return void
50+
*
51+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
52+
*/
53+
public function aroundExecute(
54+
DeleteByDirectoryPathInterface $subject,
55+
\Closure $proceed,
56+
string $directoryPath
57+
) : void {
58+
/** @var AdapterInterface $connection */
59+
$connection = $this->resourceConnection->getConnection();
60+
$galleryAssetTableName = $this->resourceConnection->getTableName('media_gallery_asset');
61+
$mediaContentAssetTableName = $this->resourceConnection->getTableName('media_content_asset');
62+
63+
$select = $connection->select();
64+
$select->from($galleryAssetTableName, ['id']);
65+
$select->where('path LIKE ?', $directoryPath);
66+
$galleryAssetIds = $connection->fetchCol($select);
67+
68+
$proceed();
69+
70+
try {
71+
$connection->delete(
72+
$mediaContentAssetTableName,
73+
['asset_id IN(?)' => implode(', ', $galleryAssetIds)]
74+
);
75+
} catch (\Exception $exception) {
76+
$this->logger->critical($exception);
77+
$message = __(
78+
'Could not delete media content assets for media gallery asset with path %path: %error',
79+
['path' => $directoryPath, 'error' => $exception->getMessage()]
80+
);
81+
throw new CouldNotDeleteException($message, $exception);
82+
}
83+
}
84+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\MediaContent\Model\Plugin;
8+
9+
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\DB\Adapter\AdapterInterface;
11+
use Magento\Framework\Exception\CouldNotDeleteException;
12+
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface;
13+
use Psr\Log\LoggerInterface;
14+
15+
/**
16+
* Remove media content record after media gallery asset removal.
17+
*/
18+
class MediaGalleryAssetDeleteByPath
19+
{
20+
/**
21+
* @var ResourceConnection
22+
*/
23+
private $resourceConnection;
24+
25+
/**
26+
* @var LoggerInterface
27+
*/
28+
private $logger;
29+
30+
/**
31+
* DeleteById constructor.
32+
*
33+
* @param ResourceConnection $resourceConnection
34+
* @param LoggerInterface $logger
35+
*/
36+
public function __construct(
37+
ResourceConnection $resourceConnection,
38+
LoggerInterface $logger
39+
) {
40+
$this->resourceConnection = $resourceConnection;
41+
$this->logger = $logger;
42+
}
43+
44+
/**
45+
* @param DeleteByPathInterface $subject
46+
* @param \Closure $proceed
47+
* @param string $mediaAssetPath
48+
* @throws CouldNotDeleteException
49+
* @return void
50+
*
51+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
52+
*/
53+
public function aroundExecute(
54+
DeleteByPathInterface $subject,
55+
\Closure $proceed,
56+
string $mediaAssetPath
57+
) : void {
58+
/** @var AdapterInterface $connection */
59+
$connection = $this->resourceConnection->getConnection();
60+
$galleryAssetTableName = $this->resourceConnection->getTableName('media_gallery_asset');
61+
$mediaContentAssetTableName = $this->resourceConnection->getTableName('media_content_asset');
62+
63+
$select = $connection->select();
64+
$select->from($galleryAssetTableName, ['id']);
65+
$select->where('path = ?', $mediaAssetPath);
66+
$galleryAssetIds = $connection->fetchCol($select);
67+
68+
$proceed();
69+
70+
try {
71+
$connection->delete(
72+
$mediaContentAssetTableName,
73+
['asset_id IN(?)' => implode(', ', $galleryAssetIds)]
74+
);
75+
} catch (\Exception $exception) {
76+
$this->logger->critical($exception);
77+
$message = __(
78+
'Could not delete media content assets for media gallery asset with path %path: %error',
79+
['path' => $mediaAssetPath, 'error' => $exception->getMessage()]
80+
);
81+
throw new CouldNotDeleteException($message, $exception);
82+
}
83+
}
84+
}

app/code/Magento/MediaContent/composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"php": "~7.1.3||~7.2.0||~7.3.0",
66
"magento/framework": "*",
77
"magento/module-media-content-api": "*",
8-
"magento/module-media-gallery-api": "*",
9-
"magento/module-media-gallery": "*"
8+
"magento/module-media-gallery-api": "*"
109
},
1110
"type": "magento2-module",
1211
"license": [

app/code/Magento/MediaContent/etc/db_schema.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@
1616
<column name="type"/>
1717
<column name="field"/>
1818
</constraint>
19-
<constraint xsi:type="foreign" referenceId="MEDIA_CONTENT_ASSET_ID_MEDIA_GALLERY_ASSET_ID" table="media_content_asset" column="asset_id" referenceTable="media_gallery_asset" referenceColumn="id" onDelete="CASCADE"/>
2019
</table>
2120
</schema>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212
<preference for="Magento\MediaContentApi\Api\GetContentWithAssetInterface" type="Magento\MediaContent\Model\GetContentWithAsset"/>
1313
<preference for="Magento\MediaContentApi\Api\ExtractAssetFromContentInterface" type="Magento\MediaContentApi\Model\ExtractAssetFromContent"/>
1414
<preference for="Magento\MediaContentApi\Api\UpdateRelationsInterface" type="Magento\MediaContent\Model\UpdateRelations"/>
15+
<type name="Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface">
16+
<plugin name="remove_media_content_after_asset_is_removed_by_path" type="Magento\MediaContent\Model\Plugin\MediaGalleryAssetDeleteByPath" />
17+
</type>
18+
<type name="\Magento\MediaGalleryApi\Model\Asset\Command\DeleteByDirectoryPathInterface">
19+
<plugin name="remove_media_content_after_asset_is_removed_by_directory_path" type="Magento\MediaContent\Model\Plugin\MediaGalleryAssetDeleteByDirectoryPath" />
20+
</type>
1521
</config>

app/code/Magento/MediaContentApi/Api/AssignAssetInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
interface AssignAssetInterface
1616
{
1717
/**
18+
* Save relation between media asset and media content.
19+
*
1820
* @param int $assetId
1921
* @param string $contentType
2022
* @param string $contentEntityId

app/code/Magento/MediaContentApi/Api/GetContentWithAssetInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
interface GetContentWithAssetInterface
1616
{
1717
/**
18+
* Get media asset to content relation by media asset id.
19+
*
1820
* @param int $assetId
1921
*
2022
* @return array

app/code/Magento/MediaContentApi/Api/UnassignAssetInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
interface UnassignAssetInterface
1616
{
1717
/**
18+
* Remove relation between the media asset and media content.
19+
*
1820
* @param int $assetId
1921
* @param string $contentType
2022
* @param string $contentEntityId

0 commit comments

Comments
 (0)