Skip to content

Commit

Permalink
Merge pull request #192 from magento-south/MAGETWO-53349
Browse files Browse the repository at this point in the history
[South] Sprint s54
  • Loading branch information
slavvka authored Jul 28, 2016
2 parents 4269541 + 792ca67 commit c800ca3
Show file tree
Hide file tree
Showing 24 changed files with 2,065 additions and 200 deletions.
58 changes: 58 additions & 0 deletions app/code/Magento/Catalog/Api/Data/CategoryLinkInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Api\Data;

use Magento\Framework\Api\ExtensibleDataInterface;

/**
* @api
*/
interface CategoryLinkInterface extends ExtensibleDataInterface
{
/**
* @return int|null
*/
public function getPosition();

/**
* @param int $position
* @return $this
*/
public function setPosition($position);

/**
* Get category id
*
* @return string
*/
public function getCategoryId();

/**
* Set category id
*
* @param string $categoryId
* @return $this
*/
public function setCategoryId($categoryId);

/**
* Retrieve existing extension attributes object.
*
* @return \Magento\Catalog\Api\Data\CategoryLinkExtensionInterface|null
*/
public function getExtensionAttributes();

/**
* Set an extension attributes object.
*
* @param \Magento\Catalog\Api\Data\CategoryLinkExtensionInterface $extensionAttributes
* @return $this
*/
public function setExtensionAttributes(
\Magento\Catalog\Api\Data\CategoryLinkExtensionInterface $extensionAttributes
);
}
73 changes: 73 additions & 0 deletions app/code/Magento/Catalog/Model/Category/Link/ReadHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\Category\Link;

use Magento\Framework\EntityManager\Operation\ExtensionInterface;

/**
* Read handler for catalog product link.
*/
class ReadHandler implements ExtensionInterface
{
/**
* @var \Magento\Catalog\Api\Data\CategoryLinkInterfaceFactory
*/
private $categoryLinkFactory;

/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CategoryLink
*/
private $productCategoryLink;

/**
* @var \Magento\Framework\Api\DataObjectHelper
*/
private $dataObjectHelper;

/**
* ReadHandler constructor.
*
* @param \Magento\Catalog\Api\Data\CategoryLinkInterfaceFactory $categoryLinkFactory
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
* @param \Magento\Catalog\Model\ResourceModel\Product\CategoryLink $productCategoryLink
*/
public function __construct(
\Magento\Catalog\Api\Data\CategoryLinkInterfaceFactory $categoryLinkFactory,
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
\Magento\Catalog\Model\ResourceModel\Product\CategoryLink $productCategoryLink
) {
$this->categoryLinkFactory = $categoryLinkFactory;
$this->dataObjectHelper = $dataObjectHelper;
$this->productCategoryLink = $productCategoryLink;
}

/**
* @param object $entity
* @param array $arguments
* @return object
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute($entity, $arguments = [])
{
$categoryLinks = [];
foreach ($this->productCategoryLink->getCategoryLinks($entity) as $categoryData) {
/** @var \Magento\Catalog\Api\Data\CategoryLinkInterface $categoryLink */
$categoryLink = $this->categoryLinkFactory->create();
$this->dataObjectHelper->populateWithArray(
$categoryLink,
$categoryData,
\Magento\Catalog\Api\Data\CategoryLinkInterface::class
);
$categoryLinks[] = $categoryLink;
}

$extensionAttributes = $entity->getExtensionAttributes();
$extensionAttributes->setCategoryLinks(!empty($categoryLinks) ? $categoryLinks : null);
$entity->setExtensionAttributes($extensionAttributes);

return $entity;
}
}
131 changes: 131 additions & 0 deletions app/code/Magento/Catalog/Model/Category/Link/SaveHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\Category\Link;

use Magento\Catalog\Api\Data\CategoryLinkInterface;
use Magento\Catalog\Model\Indexer\Product\Category;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;

/**
* Save handler for catalog product link.
*/
class SaveHandler implements ExtensionInterface
{

/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CategoryLink
*/
private $productCategoryLink;

/**
* @var \Magento\Framework\EntityManager\HydratorPool
*/
private $hydratorPool;

/**
* SaveHandler constructor.
*
* @param \Magento\Catalog\Model\ResourceModel\Product\CategoryLink $productCategoryLink
* @param \Magento\Framework\EntityManager\HydratorPool $hydratorPool
*/
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CategoryLink $productCategoryLink,
\Magento\Framework\EntityManager\HydratorPool $hydratorPool
) {
$this->productCategoryLink = $productCategoryLink;
$this->hydratorPool = $hydratorPool;
}

/**
* @param object $entity
* @param array $arguments
* @return object
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute($entity, $arguments = [])
{
$entity->setIsChangedCategories(false);

$extensionAttributes = $entity->getExtensionAttributes();
if ($extensionAttributes === null && !$entity->hasCategoryIds()) {
return $entity;
}

$modelCategoryLinks = $this->getCategoryLinksPositions($entity);

$dtoCategoryLinks = $extensionAttributes->getCategoryLinks();
if ($dtoCategoryLinks !== null) {
$hydrator = $this->hydratorPool->getHydrator(CategoryLinkInterface::class);
$dtoCategoryLinks = array_map(function ($categoryLink) use ($hydrator) {
return $hydrator->extract($categoryLink) ;
}, $dtoCategoryLinks);
$processLinks = $this->mergeCategoryLinks($dtoCategoryLinks, $modelCategoryLinks);
} else {
$processLinks = $modelCategoryLinks;
}

$affectedCategoryIds = $this->productCategoryLink->saveCategoryLinks($entity, $processLinks);

if (!empty($affectedCategoryIds)) {
$entity->setAffectedCategoryIds($affectedCategoryIds);
$entity->setIsChangedCategories(true);
}

return $entity;
}

/**
* @param object $entity
* @return array
*/
private function getCategoryLinksPositions($entity)
{
$result = [];
$currentCategoryLinks = $this->productCategoryLink->getCategoryLinks($entity, $entity->getCategoryIds());
foreach ($entity->getCategoryIds() as $categoryId) {
$key = array_search($categoryId, array_column($currentCategoryLinks, 'category_id'));
if ($key === false) {
$result[] = ['category_id' => (int)$categoryId, 'position' => 0];
} else {
$result[] = $currentCategoryLinks[$key];
}
}

return $result;
}

/**
* Merge category links
*
* @param array $newCategoryPositions
* @param array $oldCategoryPositions
* @return array
*/
private function mergeCategoryLinks($newCategoryPositions, $oldCategoryPositions)
{
$result = [];
if (empty($newCategoryPositions)) {
return $result;
}

foreach ($newCategoryPositions as $newCategoryPosition) {
$key = array_search(
$newCategoryPosition['category_id'],
array_column($oldCategoryPositions, 'category_id')
);

if ($key === false) {
$result[] = $newCategoryPosition;
} elseif ($oldCategoryPositions[$key]['position'] != $newCategoryPosition['position']) {
$result[] = $newCategoryPositions[$key];
unset($oldCategoryPositions[$key]);
}
}
$result = array_merge($result, $oldCategoryPositions);

return $result;
}
}
79 changes: 79 additions & 0 deletions app/code/Magento/Catalog/Model/CategoryLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Model;

/**
* @codeCoverageIgnore
*/
class CategoryLink extends \Magento\Framework\Api\AbstractExtensibleObject implements
\Magento\Catalog\Api\Data\CategoryLinkInterface
{
/**#@+
* Constants
*/
const KEY_POSITION = 'position';
const KEY_CATEGORY_ID = 'category_id';
/**#@-*/

/**
* {@inheritdoc}
*/
public function getPosition()
{
return $this->_get(self::KEY_POSITION);
}

/**
* {@inheritdoc}
*/
public function getCategoryId()
{
return $this->_get(self::KEY_CATEGORY_ID);
}

/**
* @param int $position
* @return $this
*/
public function setPosition($position)
{
return $this->setData(self::KEY_POSITION, $position);
}

/**
* Set category id
*
* @param string $categoryId
* @return $this
*/
public function setCategoryId($categoryId)
{
return $this->setData(self::KEY_CATEGORY_ID, $categoryId);
}

/**
* {@inheritdoc}
*
* @return \Magento\Catalog\Api\Data\CategoryLinkExtensionInterface|null
*/
public function getExtensionAttributes()
{
return $this->_getExtensionAttributes();
}

/**
* {@inheritdoc}
*
* @param \Magento\Catalog\Api\Data\CategoryLinkExtensionInterface $extensionAttributes
* @return $this
*/
public function setExtensionAttributes(
\Magento\Catalog\Api\Data\CategoryLinkExtensionInterface $extensionAttributes
) {
return $this->_setExtensionAttributes($extensionAttributes);
}
}
46 changes: 46 additions & 0 deletions app/code/Magento/Catalog/Model/Product/Website/ReadHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\Product\Website;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;

class ReadHandler implements ExtensionInterface
{
/** @var ProductWebsiteLink */
private $productWebsiteLink;

/**
* ReadHandler constructor.
* @param ProductWebsiteLink $resourceModel
*/
public function __construct(
ProductWebsiteLink $productWebsiteLink
) {
$this->productWebsiteLink = $productWebsiteLink;
}

/**
* @param ProductInterface $product
* @param array $arguments
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @return ProductInterface
*/
public function execute($product, $arguments = [])
{
if ($product->getExtensionAttributes()->getWebsiteIds() !== null) {
return $product;
}
$websiteIds = $this->productWebsiteLink->getWebsiteIdsByProductId($product->getId());

$extensionAttributes = $product->getExtensionAttributes();
$extensionAttributes->setWebsiteIds($websiteIds);
$product->setExtensionAttributes($extensionAttributes);

return $product;
}
}
Loading

0 comments on commit c800ca3

Please sign in to comment.