Skip to content

Commit

Permalink
Implemented suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Galvao da Gama committed Jul 10, 2020
1 parent e0a777b commit 8955147
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

namespace Magento\MediaContent\Model;

use Magento\Framework\Exception\InvalidArgumentException;
use Magento\Framework\Exception\LocalizedException;
use Magento\MediaContentApi\Api\GetAssetIdByContentFieldInterface as GetAssetIdByContentFieldApiInterface;
use Magento\MediaContentApi\Model\GetAssetIdByContentFieldInterface;

/**
* Class responsible to return Asset ids by content field
*/
class GetAssetIdByContentFieldComposite implements GetAssetIdByContentFieldInterface
class GetAssetIdByContentFieldComposite implements GetAssetIdByContentFieldApiInterface
{
/**
* @var GetAssetIdByContentFieldInterface[]
Expand All @@ -32,10 +35,13 @@ public function __construct($getAssetIdByContentFieldArray = [])
/**
* @inheritDoc
*/
public function execute(string $value): array
public function execute(string $field, string $value): array
{
if (!array_key_exists($field, $this->getAssetIdByContentFieldArray)) {
throw new InvalidArgumentException(__('The field argument is invalid.'));
}
$ids = [];
foreach ($this->getAssetIdByContentFieldArray as $getAssetIdByContentField) {
foreach ($this->getAssetIdByContentFieldArray[$field] as $getAssetIdByContentField) {
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$ids = array_merge($ids, $getAssetIdByContentField->execute($value));
}
Expand Down
3 changes: 1 addition & 2 deletions app/code/Magento/MediaContent/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<preference for="Magento\MediaContentApi\Api\Data\ContentIdentityInterface" type="Magento\MediaContent\Model\ContentIdentity"/>
<preference for="Magento\MediaContentApi\Api\Data\ContentAssetLinkInterface" type="Magento\MediaContent\Model\ContentAssetLink"/>
<preference for="Magento\MediaContentApi\Model\SearchPatternConfigInterface" type="Magento\MediaContent\Model\Content\SearchPatternConfig"/>
<preference for="Magento\MediaContentApi\Api\GetAssetIdByContentFieldInterface" type="Magento\MediaContent\Model\GetAssetIdByContentFieldComposite"/>
<type name="Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface">
<plugin name="remove_media_content_after_asset_is_removed_by_path" type="Magento\MediaContent\Plugin\MediaGalleryAssetDeleteByPath" />
</type>
Expand Down Expand Up @@ -43,6 +44,4 @@
<argument name="data" xsi:type="object">Magento\MediaContent\Model\Content\Config\Data</argument>
</arguments>
</type>
<virtualType name="Magento\MediaContent\Model\GetAssetIdByContentStatusComposite" type="Magento\MediaContent\Model\GetAssetIdByContentFieldComposite"/>
<virtualType name="Magento\MediaContent\Model\GetAssetIdByContentStoreComposite" type="Magento\MediaContent\Model\GetAssetIdByContentFieldComposite"/>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentApi\Api;

use Magento\Framework\Exception\InvalidArgumentException;

/**
* @api
* Interface used to return Asset id by content field.
*/
interface GetAssetIdByContentFieldInterface
{
/**
* This function returns asset ids by content field
*
* @param string $field
* @param string $value
* @throws InvalidArgumentException
* @return int[]
*/
public function execute(string $field, string $value): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace Magento\MediaContentApi\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Interface used to return Asset id by content field.
*/
Expand All @@ -17,6 +20,8 @@ interface GetAssetIdByContentFieldInterface
*
* @param string $value
* @return int[]
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function execute(string $value): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public function __construct(

/**
* @inheritDoc
* @throws LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function execute(string $value): array
{
Expand All @@ -76,11 +74,7 @@ public function execute(string $value): array
$categoryIds
);

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

return array_map(function ($item) {
return $item['asset_id'];
}, $result);
return $this->connection->getConnection()->fetchCol($sql);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public function __construct(

/**
* @inheritDoc
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(string $value): array
{
Expand All @@ -91,11 +90,7 @@ public function execute(string $value): array
$this->getValueFromMap($value)
);

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

return array_map(function ($item) {
return $item['asset_id'];
}, $result);
return $this->connection->getConnection()->fetchCol($sql);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public function __construct(

/**
* @inheritDoc
* @throws LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function execute(string $value): array
{
Expand All @@ -70,10 +68,6 @@ public function execute(string $value): array
$store->getWebsiteId()
);

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

return array_map(function ($item) {
return $item['asset_id'];
}, $result);
return $this->connection->getConnection()->fetchCol($sql);
}
}
1 change: 0 additions & 1 deletion app/code/Magento/MediaContentCatalog/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"require": {
"php": "~7.3.0||~7.4.0",
"magento/module-media-content-api": "*",
"magento/module-media-content": "*",
"magento/module-catalog": "*",
"magento/module-eav": "*",
"magento/module-store": "*",
Expand Down
20 changes: 12 additions & 8 deletions app/code/Magento/MediaContentCatalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,24 @@
<argument name="entityType" xsi:type="string">catalog_category</argument>
</arguments>
</virtualType>
<virtualType name="Magento\MediaContent\Model\GetAssetIdByContentStatusComposite">
<type name="Magento\MediaContentApi\Api\GetAssetIdByContentFieldInterface">
<arguments>
<argument name="getAssetIdByContentFieldArray" xsi:type="array">
<item name="getAssetIdByProductStatus" xsi:type="object">Magento\MediaContentCatalog\Model\GetAssetIdByProductStatus</item>
<item name="getAssetIdByCategoryStatus" xsi:type="object">Magento\MediaContentCatalog\Model\GetAssetIdByCategoryStatus</item>
<item name="content_status" xsi:type="array">
<item name="getAssetIdByProductStatus" xsi:type="object">Magento\MediaContentCatalog\Model\GetAssetIdByProductStatus</item>
<item name="getAssetIdByCategoryStatus" xsi:type="object">Magento\MediaContentCatalog\Model\GetAssetIdByCategoryStatus</item>
</item>
</argument>
</arguments>
</virtualType>
<virtualType name="Magento\MediaContent\Model\GetAssetIdByContentStoreComposite">
</type>
<type name="Magento\MediaContentApi\Api\GetAssetIdByContentFieldInterface">
<arguments>
<argument name="getAssetIdByContentFieldArray" xsi:type="array">
<item name="getAssetIdByProductStore" xsi:type="object">Magento\MediaContentCatalog\Model\GetAssetIdByProductStore</item>
<item name="getAssetIdByCategoryStore" xsi:type="object">Magento\MediaContentCatalog\Model\GetAssetIdByCategoryStore</item>
<item name="store_id" xsi:type="array">
<item name="getAssetIdByProductStore" xsi:type="object">Magento\MediaContentCatalog\Model\GetAssetIdByProductStore</item>
<item name="getAssetIdByCategoryStore" xsi:type="object">Magento\MediaContentCatalog\Model\GetAssetIdByCategoryStore</item>
</item>
</argument>
</arguments>
</virtualType>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
namespace Magento\MediaContentCms\Model;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\MediaContentApi\Model\GetAssetIdByContentFieldInterface;
use Magento\Store\Api\StoreRepositoryInterface;

/**
* Class responsible to return Asset id by content field
Expand Down Expand Up @@ -44,7 +42,6 @@ class GetAssetIdByContentField implements GetAssetIdByContentFieldInterface
*/
private $idColumn;


/**
* GetAssetIdByContentField constructor.
*
Expand Down Expand Up @@ -88,10 +85,6 @@ public function execute(string $value): array
$value
);

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

return array_map(function ($item) {
return $item['asset_id'];
}, $result);
return $this->connection->getConnection()->fetchCol($sql);
}
}
1 change: 0 additions & 1 deletion app/code/Magento/MediaContentCms/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"require": {
"php": "~7.3.0||~7.4.0",
"magento/module-media-content-api": "*",
"magento/module-media-content": "*",
"magento/module-cms": "*",
"magento/framework": "*"
},
Expand Down
20 changes: 12 additions & 8 deletions app/code/Magento/MediaContentCms/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,24 @@
<argument name="fieldColumn" xsi:type="string">is_active</argument>
</arguments>
</virtualType>
<virtualType name="Magento\MediaContent\Model\GetAssetIdByContentStatusComposite">
<type name="Magento\MediaContentApi\Api\GetAssetIdByContentFieldInterface">
<arguments>
<argument name="getAssetIdByContentFieldArray" xsi:type="array">
<item name="getAssetIdByPageStatus" xsi:type="object">Magento\MediaContentCms\Model\GetAssetIdByPageStatus</item>
<item name="getAssetIdByBlockStatus" xsi:type="object">Magento\MediaContentCms\Model\GetAssetIdByBlockStatus</item>
<item name="content_status" xsi:type="array">
<item name="getAssetIdByPageStatus" xsi:type="object">Magento\MediaContentCms\Model\GetAssetIdByPageStatus</item>
<item name="getAssetIdByBlockStatus" xsi:type="object">Magento\MediaContentCms\Model\GetAssetIdByBlockStatus</item>
</item>
</argument>
</arguments>
</virtualType>
<virtualType name="Magento\MediaContent\Model\GetAssetIdByContentStoreComposite">
</type>
<type name="Magento\MediaContentApi\Api\GetAssetIdByContentFieldInterface">
<arguments>
<argument name="getAssetIdByContentFieldArray" xsi:type="array">
<item name="getAssetIdByPageStore" xsi:type="object">Magento\MediaContentCms\Model\GetAssetIdByPageStore</item>
<item name="getAssetIdByBlockStore" xsi:type="object">Magento\MediaContentCms\Model\GetAssetIdByBlockStore</item>
<item name="store_id" xsi:type="array">
<item name="getAssetIdByPageStore" xsi:type="object">Magento\MediaContentCms\Model\GetAssetIdByPageStore</item>
<item name="getAssetIdByBlockStore" xsi:type="object">Magento\MediaContentCms\Model\GetAssetIdByBlockStore</item>
</item>
</argument>
</arguments>
</virtualType>
</type>
</config>

0 comments on commit 8955147

Please sign in to comment.