Skip to content

Commit a7e980c

Browse files
author
Gabriel Galvao da Gama
committed
Refactor and added store view filter
1 parent 1fcb11a commit a7e980c

File tree

18 files changed

+464
-152
lines changed

18 files changed

+464
-152
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContent\Model;
9+
10+
use Magento\MediaContentApi\Model\GetAssetIdByContentFieldInterface;
11+
12+
/**
13+
* Class responsible to return Asset ids by content field
14+
*/
15+
class GetAssetIdByContentFieldComposite implements GetAssetIdByContentFieldInterface
16+
{
17+
/**
18+
* @var GetAssetIdByContentFieldInterface[]
19+
*/
20+
private $getAssetIdByContentFieldArray;
21+
22+
/**
23+
* GetAssetIdByContentStatusComposite constructor.
24+
*
25+
* @param array $getAssetIdByContentFieldArray
26+
*/
27+
public function __construct($getAssetIdByContentFieldArray = [])
28+
{
29+
$this->getAssetIdByContentFieldArray = $getAssetIdByContentFieldArray;
30+
}
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
public function execute(string $value): array
36+
{
37+
$ids = [];
38+
foreach ($this->getAssetIdByContentFieldArray as $getAssetIdByContentField) {
39+
$ids = array_merge($ids, $getAssetIdByContentField->execute($value));
40+
}
41+
return array_unique($ids);
42+
}
43+
}

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

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

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

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@
4343
<argument name="data" xsi:type="object">Magento\MediaContent\Model\Content\Config\Data</argument>
4444
</arguments>
4545
</type>
46+
<virtualType name="Magento\MediaContent\Model\GetAssetIdByContentStatusComposite" type="Magento\MediaContent\Model\GetAssetIdByContentFieldComposite"/>
47+
<virtualType name="Magento\MediaContent\Model\GetAssetIdByContentStoreComposite" type="Magento\MediaContent\Model\GetAssetIdByContentFieldComposite"/>
4648
</config>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentApi\Model;
9+
10+
/**
11+
* Interface used to return Asset id by content field.
12+
*/
13+
interface GetAssetIdByContentFieldInterface
14+
{
15+
/**
16+
* This function returns asset ids by content field
17+
*
18+
* @param string $value
19+
* @return int[]
20+
*/
21+
public function execute(string $value): array;
22+
}

app/code/Magento/MediaContentApi/Model/GetAssetIdByContentStatusInterface.php

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentCatalog\Model;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\MediaContentApi\Model\GetAssetIdByContentFieldInterface;
13+
use Magento\Store\Api\GroupRepositoryInterface;
14+
use Magento\Store\Api\StoreRepositoryInterface;
15+
16+
/**
17+
* Class responsible to return Asset id by content field
18+
*/
19+
class GetAssetIdByCategoryStore implements GetAssetIdByContentFieldInterface
20+
{
21+
private const TABLE_CONTENT_ASSET = 'media_content_asset';
22+
23+
/**
24+
* @var ResourceConnection
25+
*/
26+
private $connection;
27+
28+
/**
29+
* @var StoreRepositoryInterface
30+
*/
31+
private $storeRepository;
32+
33+
/**
34+
* @var GroupRepositoryInterface
35+
*/
36+
private $storeGroupRepository;
37+
38+
/**
39+
* @var string
40+
*/
41+
private $entityType;
42+
43+
/**
44+
* GetAssetIdByProductStore constructor.
45+
*
46+
* @param ResourceConnection $resource
47+
* @param StoreRepositoryInterface $storeRepository
48+
* @param GroupRepositoryInterface $storeGroupRepository
49+
*/
50+
public function __construct(
51+
ResourceConnection $resource,
52+
StoreRepositoryInterface $storeRepository,
53+
GroupRepositoryInterface $storeGroupRepository
54+
) {
55+
$this->connection = $resource;
56+
$this->storeRepository = $storeRepository;
57+
$this->storeGroupRepository = $storeGroupRepository;
58+
$this->entityType = 'catalog_category';
59+
}
60+
61+
/**
62+
* @inheritDoc
63+
* @throws LocalizedException
64+
* @throws \Magento\Framework\Exception\NoSuchEntityException
65+
*/
66+
public function execute(string $value): array
67+
{
68+
$storeView = $this->storeRepository->getById($value);
69+
$storeGroup = $this->storeGroupRepository->get($storeView->getStoreGroupId());
70+
$categoryIds = $this->getCategoryIdsByRootCategory($storeGroup->getRootCategoryId());
71+
$sql = $this->connection->getConnection()->select()->from(
72+
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
73+
['asset_id']
74+
)->where(
75+
'entity_type = ?',
76+
$this->entityType
77+
)->where(
78+
'entity_id IN (?)',
79+
$categoryIds
80+
);
81+
82+
$result = $this->connection->getConnection()->fetchAll($sql);
83+
84+
return array_map(function ($item) {
85+
return $item['asset_id'];
86+
}, $result);
87+
}
88+
89+
private function getCategoryIdsByRootCategory($rootCategoryId)
90+
{
91+
$contentCategoriesSql = $this->connection->getConnection()->select()->from(
92+
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
93+
['entity_id']
94+
)->where(
95+
'entity_type = ?',
96+
$this->entityType
97+
)->joinInner(
98+
['category_table' => $this->connection->getTableName('catalog_category_entity')],
99+
'asset_content_table.entity_id = category_table.entity_id',
100+
['path']
101+
);
102+
103+
$result = $this->connection->getConnection()->fetchAll($contentCategoriesSql);
104+
105+
$result = array_filter($result, function ($item) use ($rootCategoryId) {
106+
$pathArray = explode("/", $item['path']);
107+
$isInPath = false;
108+
foreach ($pathArray as $id) {
109+
if ($id == $rootCategoryId) {
110+
$isInPath = true;
111+
}
112+
}
113+
return $isInPath;
114+
});
115+
116+
return array_map(function ($item) {
117+
return $item['entity_id'];
118+
}, $result);
119+
}
120+
}

app/code/Magento/MediaContent/Model/GetAssetIdByEavContentStatus.php renamed to app/code/Magento/MediaContentCatalog/Model/GetAssetIdByEavContentField.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\MediaContent\Model;
8+
namespace Magento\MediaContentCatalog\Model;
99

1010
use Magento\Eav\Model\Config;
1111
use Magento\Framework\App\ResourceConnection;
12-
use Magento\MediaContentApi\Model\GetAssetIdByContentStatusInterface;
12+
use Magento\MediaContentApi\Model\GetAssetIdByContentFieldInterface;
1313

1414
/**
15-
* Class responsible to return Asset id by eav entity status
15+
* Class responsible to return Asset id by eav content field
1616
*/
17-
class GetAssetIdByEavContentStatus implements GetAssetIdByContentStatusInterface
17+
class GetAssetIdByEavContentField implements GetAssetIdByContentFieldInterface
1818
{
1919
private const TABLE_CONTENT_ASSET = 'media_content_asset';
2020

@@ -70,7 +70,7 @@ public function __construct(
7070
* @inheritDoc
7171
* @throws \Magento\Framework\Exception\LocalizedException
7272
*/
73-
public function execute(string $status): array
73+
public function execute(string $value): array
7474
{
7575
$statusAttribute = $this->config->getAttribute($this->entityType, $this->attributeCode);
7676

@@ -88,7 +88,7 @@ public function execute(string $status): array
8888
[]
8989
)->where(
9090
'entity_eav_type.value = ?',
91-
$this->getValueFromMap($status)
91+
$this->getValueFromMap($value)
9292
);
9393

9494
$result = $this->connection->getConnection()->fetchAll($sql);

0 commit comments

Comments
 (0)