Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#397 : "More from this model" image preview section #408

Merged
merged 11 commits into from
Sep 23, 2019
Merged
2 changes: 1 addition & 1 deletion AdobeStockAdminUi/view/adminhtml/web/js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ define([], function () {
downloadPreviewUrl: 'adobe_stock/preview/download',
licenseAndDownloadUrl: 'adobe_stock/license/license',
quotaUrl: 'adobe_stock/license/getquota',
seriesUrl: 'adobe_stock/preview/series'
relatedImagesUrl: 'adobe_stock/preview/relatedimages'
};
});
3 changes: 2 additions & 1 deletion AdobeStockClient/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
<argument name="filters" xsi:type="array">
<item name="colors_filter" xsi:type="string">setFilterColors</item>
<item name="serie_id" xsi:type="string">setSerieId</item>
<item name="model_id" xsi:type="string">setModelId</item>
</argument>
</arguments>
</type>
</config>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use Psr\Log\LoggerInterface;

/**
* Class GetImageSeries
* Class GetRelatedImages
*/
class GetImageSeries
class GetRelatedImages
{
/**
* @var GetImageListInterface
Expand All @@ -42,77 +42,88 @@ class GetImageSeries
private $logger;

/**
* GetImageSeries constructor.
* @var string[]
*/
private $fields;

/**
* GetRelatedImages constructor.
* @param GetImageListInterface $getImageList
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param FilterBuilder $filterBuilder
* @param LoggerInterface $logger
* @param array $fields
*/
public function __construct(
GetImageListInterface $getImageList,
SearchCriteriaBuilder $searchCriteriaBuilder,
FilterBuilder $filterBuilder,
LoggerInterface $logger
LoggerInterface $logger,
array $fields = []
) {
$this->getImageList = $getImageList;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->filterBuilder = $filterBuilder;
$this->logger = $logger;
$this->fields = $fields;
}

/**
* Get image related image series.
* Get related images
*
* @param int $serieId
* @param int $imageId
* @param int $limit
*
* @return array
* @throws IntegrationException
*/
public function execute(int $serieId, int $limit): array
public function execute(int $imageId, int $limit): array
{
$relatedImageGroups = [];
try {
$filter = $this->filterBuilder->setField('serie_id')->setValue($serieId)->create();
$searchCriteria = $this->searchCriteriaBuilder->addFilter($filter)->setPageSize($limit)->create();

return $this->serializeImageSeries(
$this->getImageList->execute($searchCriteria)->getItems()
);
foreach ($this->fields as $key => $field) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can still refactor the code using the filter group for better searching. Please see devdocs for more information in order to do that we need to change SearchCriteriaBuilder interface which I mention in the previous comment. Correct me if I'm wrong.
Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! Do you mean we might have been able to add both filters to a single filter group and perform a single request? I don't think that will be possible considering that it's required to keep the related image groups separate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think you are right. I tried to implement and failed to achieve the expected result

$filter = $this->filterBuilder->setField($field)->setValue($imageId)->create();
$searchCriteria = $this->searchCriteriaBuilder->addFilter($filter)
->setPageSize($limit)
->create();
$relatedImageGroups[$key] = $this->serializeRelatedImages(
$this->getImageList->execute($searchCriteria)->getItems()
);
}
return $relatedImageGroups;
} catch (\Exception $exception) {
$message = __('Get image series list failed: %s', $exception->getMessage());
$message = __('Get related images list failed: %s', $exception->getMessage());
throw new IntegrationException($message, $exception);
}
}

/**
* Serialize image series data.
*
* @param Document[] $series
* Serialize related image data.
*
* @param Document[] $images
* @return array
* @throws SerializationException
*/
private function serializeImageSeries(array $series): array
private function serializeRelatedImages(array $images): array
{
$data = [];
try {
/** @var Document $seriesItem */
foreach ($series as $seriesItem) {
$item['id'] = $seriesItem->getId();
$item['title'] = $seriesItem->getCustomAttribute('title')->getValue();
$item['thumbnail_url'] = $seriesItem->getCustomAttribute('thumbnail_240_url')->getValue();
$data[] = $item;
/** @var Document $image */
foreach ($images as $image) {
$data[] = [
'id' => $image->getId(),
'title' => $image->getCustomAttribute('title')->getValue(),
'thumbnail_url' => $image->getCustomAttribute('thumbnail_240_url')->getValue()
];
}

$result = [
'type' => 'series',
'series' => $data,
];

return $result;
return $data;
} catch (\Exception $exception) {
$message = __('An error occurred during image series serialization: %s', $exception->getMessage());
throw new SerializationException($message);
throw new SerializationException(
__(
'An error occurred during related images serialization: %s',
$exception->getMessage()
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@

namespace Magento\AdobeStockImage\Test\Unit\Model;

use Magento\Framework\Exception\IntegrationException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\AdobeStockImageApi\Api\GetImageListInterface;
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
use Magento\Framework\Api\FilterBuilder;
use Psr\Log\LoggerInterface;
use Magento\AdobeStockImage\Model\GetImageSeries;
use Magento\AdobeStockImage\Model\GetRelatedImages;

/**
* Test for GetImageSeries Model
* Test for GetRelatedSeries Model
*/
class GetImageSeriesTest extends TestCase
class GetRelatedSeriesTest extends TestCase
{

/**
Expand All @@ -41,9 +42,14 @@ class GetImageSeriesTest extends TestCase
private $logger;

/**
* @var GetImageSeries $getImageSeries
* @var GetRelatedImages $getRelatedSeries
*/
private $getImageSeries;
private $getRelatedSeries;

/**
* @var string[]
*/
private $fields;

/**
* @inheritDoc
Expand All @@ -54,68 +60,69 @@ public function setUp()
$this->logger = $this->createMock(LoggerInterface::class);
$this->searchCriteriaBuilder = $this->createMock(SearchCriteriaBuilder::class);
$this->getImageListInterface = $this->createMock(GetImageListInterface::class);

$this->getImageSeries = new GetImageSeries(
$this->fields = ['same_series' => 'serie_id', 'same_model' => 'model_id'];
$this->getRelatedSeries = new GetRelatedImages(
$this->getImageListInterface,
$this->searchCriteriaBuilder,
$this->filterBuilder,
$this->logger
$this->logger,
$this->fields
);
}

/**
* Check if image series can be executed.
* Check if related images can be executed.
*
* @param $series
* @param $relatedImagesProvider
* @param $expectedResult
* @dataProvider seriesDataProvider
* @throws \Magento\Framework\Exception\IntegrationException
* @throws IntegrationException
* @dataProvider relatedImagesDataProvider
*/
public function testExecute($seriesProvider, $expectedResult)
public function testExecute($relatedImagesProvider, $expectedResult)
{
$this->filterBuilder->expects($this->once())
$this->filterBuilder->expects($this->any())
->method('setField')
->willReturnSelf();
$this->filterBuilder->expects($this->once())
$this->filterBuilder->expects($this->any())
->method('setValue')
->willReturnSelf();
$this->filterBuilder->expects($this->once())
$this->filterBuilder->expects($this->any())
->method('create')
->willReturn(
$this->createMock(\Magento\Framework\Api\Filter::class)
);
$this->searchCriteriaBuilder->expects($this->once())
$this->searchCriteriaBuilder->expects($this->any())
->method('addFilter')
->willReturnSelf();
$this->searchCriteriaBuilder->expects($this->once())
$this->searchCriteriaBuilder->expects($this->any())
->method('setPageSize')
->willReturnSelf();
$this->searchCriteriaBuilder->expects($this->once())
$this->searchCriteriaBuilder->expects($this->any())
->method('create')
->willReturn(
$this->createMock(\Magento\Framework\Api\Search\SearchCriteria::class)
);
$searchCriteriaMock = $this->createMock(\Magento\Framework\Api\Search\SearchResultInterface::class);
$this->getImageListInterface->expects($this->once())
$this->getImageListInterface->expects($this->any())
->method('execute')
->willReturn($searchCriteriaMock);
$searchCriteriaMock->expects($this->once())
$searchCriteriaMock->expects($this->any())
->method('getItems')
->willReturn($seriesProvider);
->willReturn($relatedImagesProvider);

$this->assertEquals($expectedResult, $this->getImageSeries->execute(12345678, 30));
$this->assertEquals($expectedResult, $this->getRelatedSeries->execute(12345678, 30));
}

/**
* Series Data provider.
*
* @return array
*/
public function seriesDataProvider(): array
public function relatedImagesDataProvider(): array
{
return [
[
'seriesProvider' => [
'relatedImagesProvider' => [
new \Magento\Framework\Api\Search\Document(
[
'id' => 1234556789,
Expand Down Expand Up @@ -143,8 +150,14 @@ public function seriesDataProvider(): array
)
],
'expectedResult' => [
'type' => 'series',
'series' => [
'same_model' => [
[
'id' => 1234556789,
'title' => 'Some Title',
'thumbnail_url' => 'https://t4.ftcdn.net/z6rPCvS5umPhRUNPa62iA2YYVG49yo2n.jpg'
]
],
'same_series' => [
[
'id' => 1234556789,
'title' => 'Some Title',
Expand Down
8 changes: 8 additions & 0 deletions AdobeStockImage/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@
<argument name="driver" xsi:type="object">Magento\Framework\Filesystem\Driver\Https</argument>
</arguments>
</type>
<type name="Magento\AdobeStockImage\Model\GetRelatedImages">
<arguments>
<argument name="fields" xsi:type="array">
<item name="same_series" xsi:type="string">serie_id</item>
<item name="same_model" xsi:type="string">model_id</item>
</argument>
</arguments>
</type>
</config>
Loading