Skip to content

Mc public 17 2 #92

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

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b2cd2af
ECP-515: Storefront GET API
kokoc May 11, 2020
5567091
ECP-531: Fix current data sync bugs
kokoc May 11, 2020
f2f7355
ECP-598: Prototype for SF Import APIs
paliarush May 11, 2020
40e5676
ECP-598: Prototype for SF Import APIs
paliarush May 12, 2020
2fbd081
ECP-515: Catalog Storefront Get API
kokoc May 12, 2020
d7ca61a
ECP-515: get storefront api
kokoc May 12, 2020
51009b5
ECP-598: Prototype for SF Import APIs
paliarush May 13, 2020
7e94bea
ECP-531: Fix current data sync bugs
kokoc May 13, 2020
d45fe2d
ECP-598: Prototype for SF Import APIs
paliarush May 14, 2020
62d8897
ECP-531: Fix current data sync bugs
kokoc May 14, 2020
ca509aa
ECP-515: storefront get api for catalog
kokoc May 18, 2020
13c5b96
ECP-515: storefront get api for catalog
kokoc May 18, 2020
4185cf8
ECP-598: Prototype for SF Import APIs
paliarush May 20, 2020
5a50c2a
ECP-598: Prototype for SF Import APIs
paliarush May 21, 2020
80c5d43
ECP-515: storefront get api for catalog
kokoc May 21, 2020
bef9afa
Merge branch 'api-proto' of github.com:magento/catalog-storefront-ce …
kokoc May 21, 2020
381e617
ECP-515: storefront get api
kokoc May 27, 2020
d3780ac
ECP-515: Storefront Get API
kokoc Jun 3, 2020
ea9e952
ECP-515: Storefront Get API
kokoc Jun 4, 2020
fd7bfdd
ECP-531: Fix current data sync bugs
kokoc Jun 4, 2020
b99b23b
ECP-515: Storefront Get API
kokoc Jun 5, 2020
aaa0f21
ECP-531: Fix current data sync bugs
kokoc Jun 5, 2020
ee2be74
ECP-515: Storefront Get API
mslabko Jun 5, 2020
925f063
ECP-515: Storefront Get API
mslabko Jun 5, 2020
acd091c
ECP-515: Storefront Get API
kokoc Jun 5, 2020
e132f30
ECP-515: Storefront Get API
mslabko Jun 8, 2020
16049ad
ECP-515: Storefront Get API
mslabko Jun 8, 2020
0d96cf9
ECP-515: Storefront Get API
mslabko Jun 8, 2020
e6937d3
ECP-515: Storefront Get API
kokoc Jun 8, 2020
5289509
Merge branch 'api-proto' of github.com:magento/catalog-storefront-ce …
kokoc Jun 8, 2020
1011cde
ECP-515: Storefront Get API
mslabko Jun 10, 2020
11a159a
[17] Read API :: Integration tests :: Customer* #55
skovalenk Jun 17, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public function transform(array $productItems, array $attributes): array
continue;
}
foreach ($attributes as $attributeName => $outputAttribute) {

$rawValue = $item[$attributeName] ?? '';
$item[$outputAttribute] = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private function getAttributesMapping(): array
return $this->imageUrlResolver->resolve($item['file'] ?? '', $attributeName);
};
$attributesMapping[$attributeName]['label'] = 'label';
$attributesMapping[$attributeName]['position'] = 'position';
$attributesMapping[$attributeName]['video_content'] = function ($item) {
return $this->getVideoContent($item);
};
Expand Down Expand Up @@ -134,7 +135,6 @@ private function convertAttributesToOutputFormat(&$output, $productId, array $it
}
}
$output[$productId][$outputAttribute][] = $data;

}
return $output;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DataProviderTest extends \PHPUnit\Framework\TestCase
/**
* @return void
*/
protected function setUp()
protected function setUp(): void
{
$this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
->getMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public function fetch(array $productIds, array $attributes, array $scopes): arra
(int)$data['stock_status'] === self::STATUS_IN_STOCK
? self::VALUE_IN_STOCK
: self::VALUE_OUT_OF_STOCK;

}
return $output;
}
Expand Down
317 changes: 317 additions & 0 deletions app/code/Magento/CatalogStorefront/Model/CatalogService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogStorefront\Model;

use Magento\CatalogStorefrontApi\Api\CatalogServerInterface;
use Magento\CatalogStorefrontApi\Api\Data\Breadcrumb;
use Magento\CatalogStorefrontApi\Api\Data\CategoriesGetResponseInterface;
use Magento\CatalogStorefrontApi\Api\Data\Category;
use Magento\CatalogStorefrontApi\Api\Data\CategoryInterface;
use Magento\CatalogStorefrontApi\Api\Data\Image;
use Magento\CatalogStorefrontApi\Api\Data\MediaGalleryItem;
use Magento\CatalogStorefrontApi\Api\Data\MediaGalleryItemInterface;
use Magento\CatalogStorefrontApi\Api\Data\ProductInterface;
use Magento\CatalogStorefrontApi\Api\Data\ProductsGetRequestInterface;
use Magento\CatalogStorefrontApi\Api\Data\ImportProductsRequestInterface;
use Magento\CatalogStorefrontApi\Api\Data\ProductsGetResult;
use Magento\CatalogStorefrontApi\Api\Data\ProductsGetResultInterface;
use Magento\CatalogStorefrontApi\Api\Data\ImportProductsResponseInterface;
use Magento\CatalogStorefront\DataProvider\ProductDataProvider;
use Magento\CatalogStorefrontApi\Api\Data\CategoriesGetResponse;
use Magento\CatalogStorefrontApi\Api\Data\Video;
use Magento\Framework\Api\DataObjectHelper;
use Magento\CatalogStorefrontApi\Api\Data\CategoriesGetRequestInterface;
use Magento\CatalogStorefront\DataProvider\CategoryDataProvider;
use Magento\CatalogStorefrontApi\Api\Data\ImportCategoriesRequestInterface;
use Magento\CatalogStorefrontApi\Api\Data\ImportCategoriesResponseInterface;
use Psr\Log\LoggerInterface;

/**
* @inheritdoc
*/
class CatalogService implements CatalogServerInterface
{
/**
* @var ProductDataProvider
*/
private $dataProvider;

/**
* @var DataObjectHelper
*/
private $dataObjectHelper;

/**
* @var CategoryDataProvider
*/
private $categoryDataProvider;

/**
* @var LoggerInterface
*/
private $logger;

/**
* CatalogService constructor.
* @param ProductDataProvider $dataProvider
* @param DataObjectHelper $dataObjectHelper
* @param CategoryDataProvider $categoryDataProvider
*/
public function __construct(
ProductDataProvider $dataProvider,
DataObjectHelper $dataObjectHelper,
CategoryDataProvider $categoryDataProvider,
LoggerInterface $logger
) {
$this->dataProvider = $dataProvider;
$this->dataObjectHelper = $dataObjectHelper;
$this->categoryDataProvider = $categoryDataProvider;
$this->logger = $logger;
}

public function GetProducts(
ProductsGetRequestInterface $request
): ProductsGetResultInterface {
$result = new ProductsGetResult();

if (empty($request->getStore()) || $request->getStore() === null) {
$this->logger->error(
__('Store id is not present in Search Criteria. Please add missing info.')
);
return $result;
}
$products = [];
if (empty($request->getIds())) {
return $result;
}

$rawItems = $this->dataProvider->fetch(
$request->getIds(),
$request->getAttributeCodes(),
['store' => $request->getStore()]
);

if (count($rawItems) !== count($request->getIds())) {
$this->logger->error(
__(
'Products with the following ids are not found in catalog: %1',
implode(', ', array_diff($request->getIds(), array_keys($rawItems)))
)
);
return $result;
}

foreach ($rawItems as $item) {
$item = $this->cleanUpNullValues($item);
$variants = [];
foreach ($item['variants'] ?? [] as $productId => $variantData) {
$variant = [
'product' => $productId,
'attributes' => $variantData['attributes']
];
$variants[] = $variant;
}
$item['variants'] = $variants;

$item['description'] = $item['description']['html'] ?? '';
$item['short_description'] = $item['short_description']['html'] ?? '';
//Convert option values to unified array format
if (!empty($item['options'])) {
foreach ($item['options'] as &$option) {
$firstValue = reset($option['value']);
if (!is_array($firstValue)) {
$option['value'] = [0 => $option['value']];
continue;
}
}
}

$product = new \Magento\CatalogStorefrontApi\Api\Data\Product();
$this->dataObjectHelper->populateWithArray($product, $item, ProductInterface::class);
$product = $this->setImage('image', $item, $product);
$product = $this->setImage('small_image', $item, $product);
$product = $this->setImage('thumbnail', $item, $product);

//PopulateWithArray doesn't work with non-array sub-objects which don't set properties using constructor
$mediaGalleryData = $item['media_gallery'] ?? [];
$mediaGallery = [];
foreach ($mediaGalleryData as $mediaGalleryDataItem) {
$mediaGalleryItem = new MediaGalleryItem;
$this->dataObjectHelper->populateWithArray(
$mediaGalleryItem,
$mediaGalleryDataItem,
MediaGalleryItemInterface::class
);
if (!empty($mediaGalleryDataItem['video_content'])) {
$videoContent = new Video;
$videoContent->setMediaType($mediaGalleryDataItem['video_content']['media_type'] ?? '');
$videoContent->setVideoDescription(
$mediaGalleryDataItem['video_content']['video_description'] ?? ''
);
$videoContent->setVideoMetadata(
$mediaGalleryDataItem['video_content']['video_metadata'] ?? ''
);
$videoContent->setVideoProvider(
$mediaGalleryDataItem['video_content']['video_provider'] ?? ''
);
$videoContent->setVideoTitle(
$mediaGalleryDataItem['video_content']['video_title'] ?? ''
);
$videoContent->setVideoUrl(
$mediaGalleryDataItem['video_content']['video_url'] ?? ''
);
$mediaGalleryItem->setVideoContent($videoContent);
}

$mediaGallery[] = $mediaGalleryItem;
}
$product->setMediaGallery($mediaGallery);

$urlRewritesData = $item['url_rewrites'] ?? [];
$urlRewrites = [];
foreach ($urlRewritesData as $urlRewriteData) {
$rewrite = new \Magento\CatalogStorefrontApi\Api\Data\UrlRewrite;
$rewrite->setUrl($urlRewriteData['url'] ?? '');
$parameters = [];
foreach ($urlRewriteData['parameters'] ?? [] as $parameterData) {
$parameter = new \Magento\CatalogStorefrontApi\Api\Data\UrlRewriteParameter;
$parameter->setName($parameterData['name'] ?? '');
$parameter->setValue($parameterData['value'] ?? '');
$parameters[] = $parameter;
}
$rewrite->setParameters($parameters);
$urlRewrites[] = $rewrite;
}
$product->setUrlRewrites($urlRewrites);
$products[] = $product;
}

$result->setItems($products);

return $result;
}

/**
* Unset null values in provided array recursively
*
* @param array $array
* @return array
*/
private function cleanUpNullValues(array $array): array
{
$result = [];
foreach ($array as $key => $value) {
if ($value === null || $value === '') {
continue;
}

$result[$key] = is_array($value) ? $this->cleanUpNullValues($value) : $value;
}
return $result;
}

private function setImage(string $key, array $rawData, ProductInterface $product): ProductInterface
{
if (empty($rawData[$key])) {
return $product;
}

$image = new Image();
$image->setUrl($rawData[$key]['url'] ?? '');
$image->setLabel($rawData[$key]['label'] ?? '');
$parts = explode('_', $key);
$parts = array_map("ucfirst", $parts);
$methodName = 'set' . implode('', $parts);

$product->$methodName($image);
return $product;
}

/**
* @param ImportProductsRequestInterface $request
* @return ImportProductsResponseInterface
*/
public function ImportProducts(
ImportProductsRequestInterface $request
): ImportProductsResponseInterface {
// TODO: Implement ImportProducts() method.
}

/**
* @param ImportCategoriesRequestInterface $request
* @return ImportCategoriesResponseInterface
*/
public function ImportCategories(ImportCategoriesRequestInterface $request): ImportCategoriesResponseInterface
{
// TODO: Implement ImportCategories() method.
}

public function GetCategories(
CategoriesGetRequestInterface $request
): CategoriesGetResponseInterface {
$result = new CategoriesGetResponse();

$categories = $this->categoryDataProvider->fetch(
$request->getIds(),
\array_merge($request->getAttributeCodes(), ['is_active']),
['store' => $request->getStore()]
);

$items = [];
foreach ($categories as $category) {
$item = new Category();
$category = $this->cleanUpNullValues($category);
$category = $this->convertKeyToString('image', $category);
$category = $this->convertKeyToString('canonical_url', $category);
$category = $this->convertKeyToString('description', $category);

$this->dataObjectHelper->populateWithArray($item, $category, CategoryInterface::class);

$breadcrumbsData = $category['breadcrumbs'] ?? [];
if ($breadcrumbsData) {
$breadcrumbs = [];
foreach ($breadcrumbsData as $breadcrumbData) {
$breadcrumb = new Breadcrumb();
$breadcrumb->setCategoryId($breadcrumbData['category_id']);
$breadcrumb->setCategoryLevel((int)$breadcrumbData['category_level']);
$breadcrumb->setCategoryName($breadcrumbData['category_name']);
$breadcrumb->setCategoryUrlKey($breadcrumbData['category_url_key']);
$breadcrumb->setCategoryUrlPath($breadcrumbData['category_url_path']);
$breadcrumbs[] = $breadcrumb;
}
}
$categories = [];
foreach ($category['children'] ?? [] as $categoryId) {
$categories[$categoryId] = $categoryId;
}
$item->setChildren($categories);
$items[] = $item;
}
$result->setItems($items);
return $result;
}

/**
* Converts value of array to string type for provided key
*
* @param string $key
* @param array $data
* @return array
*/
private function convertKeyToString(string $key, array $data): array
{
if (!array_key_exists($key, $data)) {
return $data;
}

if (!is_string($data[$key])) {
$data[$key] = '';
}
return $data;
}
}
Loading