Skip to content

Commit 73fb878

Browse files
committed
Changes according to review
1 parent 8ad6be2 commit 73fb878

33 files changed

+408
-568
lines changed

app/code/Magento/MediaContent/Model/AssignAssets.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

app/code/Magento/MediaContent/Model/Content/Config.php renamed to app/code/Magento/MediaContent/Model/Content/SearchPatternConfig.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
namespace Magento\MediaContent\Model\Content;
99

1010
use Magento\Framework\Config\DataInterface;
11+
use Magento\MediaContentApi\Model\SearchPatternConfigInterface;
1112

1213
/**
1314
* Media content configuration
1415
*/
15-
class Config implements ConfigInterface
16+
class SearchPatternConfig implements SearchPatternConfigInterface
1617
{
1718
private const XML_PATH_SEARCH_PATTERNS = 'search/patterns';
1819

@@ -34,7 +35,7 @@ public function __construct(DataInterface $data)
3435
*
3536
* @return array
3637
*/
37-
public function getSearchPatterns(): array
38+
public function get(): array
3839
{
3940
return $this->data->get(self::XML_PATH_SEARCH_PATTERNS);
4041
}

app/code/Magento/MediaContent/Model/ContentAssetLink.php

Lines changed: 25 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,104 +7,68 @@
77

88
namespace Magento\MediaContent\Model;
99

10-
use Magento\Framework\Model\AbstractExtensibleModel;
11-
use Magento\Framework\Model\Context;
12-
use Magento\Framework\Registry;
13-
use Magento\Framework\Api\ExtensionAttributesFactory;
14-
use Magento\Framework\Api\AttributeValueFactory;
15-
use Magento\Framework\Model\ResourceModel\AbstractResource;
1610
use Magento\MediaContentApi\Api\Data\ContentAssetLinkInterface;
17-
use Magento\Framework\Data\Collection\AbstractDb;
18-
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
11+
use Magento\MediaContentApi\Api\Data\ContentAssetLinkExtensionInterface;
12+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
1913

2014
/**
2115
* Relation of the media asset to the media content
2216
*/
23-
class ContentAssetLink extends AbstractExtensibleModel implements ContentAssetLinkInterface
17+
class ContentAssetLink implements ContentAssetLinkInterface
2418
{
2519
private const ASSET_ID = 'asset_id';
26-
private const ENTITY_TYPE = 'entity_type';
27-
private const ENTITY_ID = 'entity_id';
28-
private const FIELD = 'field';
20+
private const CONTENT_IDENTITY = 'content_identity';
2921

3022
/**
31-
* @var ContentIdentityInterfaceFactory
23+
* @var ContentAssetLinkExtensionInterface|null
3224
*/
33-
private $contentIdentityFactory;
25+
private $extensionAttributes;
3426

3527
/**
36-
* @param \Magento\Framework\Model\Context $context
37-
* @param \Magento\Framework\Registry $registry
38-
* @param ExtensionAttributesFactory $extensionFactory
39-
* @param AttributeValueFactory $customAttributeFactory
40-
* @param ContentIdentityInterfaceFactory $contentIdentityFactory
41-
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
42-
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
43-
* @param array $data
28+
* @var ContentIdentityInterface
4429
*/
45-
public function __construct(
46-
Context $context,
47-
Registry $registry,
48-
ExtensionAttributesFactory $extensionFactory,
49-
AttributeValueFactory $customAttributeFactory,
50-
ContentIdentityInterfaceFactory $contentIdentityFactory,
51-
AbstractResource $resource = null,
52-
AbstractDb $resourceCollection = null,
53-
array $data = []
54-
) {
55-
$this->contentIdentityFactory = $contentIdentityFactory;
56-
parent::__construct(
57-
$context,
58-
$registry,
59-
$extensionFactory,
60-
$customAttributeFactory,
61-
$resource,
62-
$resourceCollection,
63-
$data
64-
);
65-
}
30+
private $contentIdentity;
6631

6732
/**
68-
* @inheritdoc
33+
* @var int
6934
*/
70-
public function getAssetId(): int
71-
{
72-
return (int) $this->getData(self::ASSET_ID);
73-
}
35+
private $assetId;
7436

7537
/**
76-
* @inheritdoc
38+
* @param ContentAssetLinkExtensionInterface|null $extensionAttributes
7739
*/
78-
public function getContentId(): ContentIdentityInterface
40+
public function __construct(
41+
int $assetId,
42+
ContentIdentityInterface $contentIdentity,
43+
?ContentAssetLinkExtensionInterface $extensionAttributes = null
44+
)
7945
{
80-
return $this->contentIdentityFactory->create(['data' => [
81-
self::ENTITY_TYPE => $this->getData(self::ENTITY_TYPE),
82-
self::ENTITY_ID => $this->getData(self::ENTITY_TYPE),
83-
self::FIELD => $this->getData(self::FIELD)
84-
]]);
46+
$this->assetId = $assetId;
47+
$this->contentIdentity = $contentIdentity;
48+
$this->extensionAttributes = $extensionAttributes;
8549
}
8650

8751
/**
8852
* @inheritdoc
8953
*/
90-
public function getField(): string
54+
public function getAssetId(): int
9155
{
92-
return (string) $this->getData(self::FIELD);
56+
return $this->assetId;
9357
}
9458

9559
/**
9660
* @inheritdoc
9761
*/
98-
public function getExtensionAttributes(): ContentAssetLinkExtensionInterface
62+
public function getContentId(): ContentIdentityInterface
9963
{
100-
return $this->_getExtensionAttributes();
64+
return $this->contentIdentity;
10165
}
10266

10367
/**
10468
* @inheritdoc
10569
*/
106-
public function setExtensionAttributes(ContentAssetLinkExtensionInterface $extensionAttributes): void
70+
public function getExtensionAttributes(): ?ContentAssetLinkExtensionInterface
10771
{
108-
$this->_setExtensionAttributes($extensionAttributes);
72+
return $this->extensionAttributes;
10973
}
11074
}

app/code/Magento/MediaContent/Model/ContentIdentity.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,67 @@
77

88
namespace Magento\MediaContent\Model;
99

10-
use Magento\Framework\Model\AbstractExtensibleModel;
1110
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
1211
use Magento\MediaContentApi\Api\Data\ContentIdentityExtensionInterface;
1312

1413
/**
1514
* @inheritdoc
1615
*/
17-
class ContentIdentity extends AbstractExtensibleModel implements ContentIdentityInterface
16+
class ContentIdentity implements ContentIdentityInterface
1817
{
19-
private const ENTITY_TYPE = 'entity_type';
20-
private const ENTITY_ID = 'entity_id';
21-
private const FIELD = 'field';
18+
private $entityType;
19+
private $entityId;
20+
private $field;
21+
private $extensionAttributes;
2222

2323
/**
24-
* @inheritdoc
24+
* ContentIdentity constructor.
25+
* @param string $entityType
26+
* @param string $entityId
27+
* @param string $field
28+
* @param ContentIdentityExtensionInterface|null $extensionAttributes
2529
*/
26-
public function getEntityType(): string
27-
{
28-
return (string) $this->getData(self::ENTITY_TYPE);
30+
public function __construct(
31+
string $entityType,
32+
string $entityId,
33+
string $field,
34+
?ContentIdentityExtensionInterface $extensionAttributes = null
35+
) {
36+
$this->entityType = $entityType;
37+
$this->entityId= $entityId;
38+
$this->field = $field;
39+
$this->extensionAttributes = $extensionAttributes;
2940
}
3041

3142
/**
3243
* @inheritdoc
3344
*/
34-
public function getEntityId(): string
45+
public function getEntityType(): string
3546
{
36-
return (string) $this->getData(self::ENTITY_ID);
47+
return $this->entityType;
3748
}
3849

3950
/**
4051
* @inheritdoc
4152
*/
42-
public function getField(): string
53+
public function getEntityId(): string
4354
{
44-
return (string) $this->getData(self::FIELD);
55+
return $this->entityId;
4556
}
4657

4758
/**
4859
* @inheritdoc
4960
*/
50-
public function getExtensionAttributes(): ContentIdentityExtensionInterface
61+
public function getField(): string
5162
{
52-
return $this->_getExtensionAttributes();
63+
return $this->field;
5364
}
5465

5566
/**
5667
* @inheritdoc
5768
*/
58-
public function setExtensionAttributes(ContentIdentityExtensionInterface $extensionAttributes): void
69+
public function getExtensionAttributes(): ContentIdentityExtensionInterface
5970
{
60-
$this->_setExtensionAttributes($extensionAttributes);
71+
return $this->extensionAttributes;
6172
}
6273
}

app/code/Magento/MediaContent/Model/DeleteContentAssetLinks.php

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Psr\Log\LoggerInterface;
1515

1616
/**
17-
* Used to unassign relation of the media asset to the media content where the media asset is used
17+
* Used to delete links of the media asset to the media content
1818
*/
1919
class DeleteContentAssetLinks implements DeleteContentAssetLinksInterface
2020
{
@@ -47,41 +47,45 @@ public function __construct(ResourceConnection $resourceConnection, LoggerInterf
4747
/**
4848
* Remove relation between the media asset and the content. I.e media asset no longer part of the content
4949
*
50-
* @param ContentAssetLinkInterface[] $contentAssetsLinks
50+
* @param ContentAssetLinkInterface[] $contentAssetLinks
5151
* @throws CouldNotDeleteException
5252
*/
53-
public function execute(array $contentAssetsLinks): void
53+
public function execute(array $contentAssetLinks): void
5454
{
55-
$failedLinks = [];
56-
foreach ($contentAssetsLinks as $contentAssetLink) {
57-
try {
58-
$connection = $this->resourceConnection->getConnection();
59-
$tableName = $this->resourceConnection->getTableName(self::MEDIA_CONTENT_ASSET_TABLE_NAME);
60-
$connection->delete(
61-
$tableName,
62-
[
63-
self::ASSET_ID . ' = ?' => $contentAssetLink->getAssetId(),
64-
self::ENTITY_TYPE . ' = ?' => $contentAssetLink->getContentId()->getEntityType(),
65-
self::ENTITY_ID . ' = ?' => $contentAssetLink->getContentId()->getEntityId(),
66-
self::FIELD . ' = ?' => $contentAssetLink->getField()
67-
]
68-
);
69-
} catch (\Exception $exception) {
70-
$this->logger->critical($exception);
71-
$failedLinks[] = self::ASSET_ID . '=' . $contentAssetLink->getAssetId() .
72-
self::ENTITY_TYPE . ' = ' . $contentAssetLink->getContentId()->getEntityType() .
73-
self::ENTITY_ID . ' = ' . $contentAssetLink->getContentId()->getEntityId() .
74-
self::FIELD . ' = ' . $contentAssetLink->getField();
75-
}
55+
try {
56+
$connection = $this->resourceConnection->getConnection();
57+
$tableName = $this->resourceConnection->getTableName(self::MEDIA_CONTENT_ASSET_TABLE_NAME);
58+
$whereSql = $this->buildWhereSqlPart($contentAssetLinks);
59+
$connection->delete($tableName, $whereSql);
60+
} catch (\Exception $exception) {
61+
$this->logger->critical($exception);
62+
throw new CouldNotDeleteException(
63+
__('An error occurred at deleting links between the media asset and media content.')
64+
);
7665
}
66+
}
7767

78-
if (!empty($failedLinks)) {
79-
throw new CouldNotDeleteException(
80-
__(
81-
'An error occurred at deleting link between the media asset and media content. Links: %links',
82-
implode(' ,', $failedLinks)
83-
)
68+
/**
69+
* @param ContentAssetLinkInterface[] $contentAssetLinks
70+
* @return string
71+
*/
72+
private function buildWhereSqlPart(array $contentAssetLinks): string
73+
{
74+
$connection = $this->resourceConnection->getConnection();
75+
$condition = [];
76+
foreach ($contentAssetLinks as $contentAssetLink) {
77+
$assetId = $connection->quoteInto(self::ASSET_ID . ' = ?', $contentAssetLink->getAssetId());
78+
$entityId = $connection->quoteInto(
79+
self::ENTITY_ID . ' = ?', $contentAssetLink->getContentId()->getEntityId()
80+
);
81+
$entityType = $connection->quoteInto(
82+
self::ENTITY_TYPE . ' = ?', $contentAssetLink->getContentId()->getEntityType()
83+
);
84+
$field = $connection->quoteInto(
85+
self::FIELD . ' = ?', $contentAssetLink->getContentId()->getField()
8486
);
87+
$condition[] = '(' . $assetId . ' AND ' . $entityId . ' AND ' . $entityType . ' AND ' . $field . ')';
8588
}
89+
return implode(' OR ', $condition);
8690
}
8791
}

0 commit comments

Comments
 (0)