-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring virtual categories rules storage/reading.
- Loading branch information
1 parent
609a57f
commit 2c5a914
Showing
12 changed files
with
377 additions
and
123 deletions.
There are no files selected for viewing
104 changes: 0 additions & 104 deletions
104
src/module-elasticsuite-virtual-category/Model/Category/Attribute/Backend/VirtualRule.php
This file was deleted.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
...module-elasticsuite-virtual-category/Model/Category/Attribute/VirtualRule/ReadHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteVirtualCategory | ||
* @author Romain Ruaud <[email protected]> | ||
* @copyright 2017 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteVirtualCategory\Model\Category\Attribute\VirtualRule; | ||
|
||
use Magento\Catalog\Model\Category; | ||
|
||
/** | ||
* Virtual Category rule read handler. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteVirtualCategory | ||
* @author Romain Ruaud <[email protected]> | ||
*/ | ||
class ReadHandler implements \Magento\Framework\EntityManager\Operation\ExtensionInterface | ||
{ | ||
/** | ||
* The virtual rule attribute code. | ||
*/ | ||
const ATTRIBUTE_CODE = 'virtual_rule'; | ||
|
||
/** | ||
* @var \Smile\ElasticsuiteCatalogRule\Model\RuleFactory $ruleFactory | ||
*/ | ||
private $ruleFactory; | ||
|
||
/** | ||
* @var \Magento\Framework\Serialize\Serializer\Json | ||
*/ | ||
private $jsonSerializer; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Smile\ElasticsuiteCatalogRule\Model\RuleFactory $ruleFactory Search rule factory. | ||
* @param \Magento\Framework\Serialize\Serializer\Json $jsonSerializer JSON Serializer. | ||
*/ | ||
public function __construct( | ||
\Smile\ElasticsuiteCatalogRule\Model\RuleFactory $ruleFactory, | ||
\Magento\Framework\Serialize\Serializer\Json $jsonSerializer | ||
) { | ||
$this->ruleFactory = $ruleFactory; | ||
$this->jsonSerializer = $jsonSerializer; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function execute($entity, $arguments = []) | ||
{ | ||
$attributeData = $entity->getData(self::ATTRIBUTE_CODE); | ||
|
||
if (!is_object($attributeData)) { | ||
$rule = $this->ruleFactory->create(); | ||
$rule->setStoreId($entity->getStoreId()); | ||
|
||
if ($attributeData !== null && is_string($attributeData)) { | ||
$attributeData = $this->jsonSerializer->unserialize($attributeData); | ||
} | ||
|
||
if ($attributeData !== null && is_array($attributeData)) { | ||
$rule->getConditions()->loadArray($attributeData); | ||
} | ||
|
||
$entity->setData(self::ATTRIBUTE_CODE, $rule); | ||
} | ||
|
||
return $entity; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...module-elasticsuite-virtual-category/Model/Category/Attribute/VirtualRule/SaveHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteVirtualCategory | ||
* @author Romain Ruaud <[email protected]> | ||
* @copyright 2017 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteVirtualCategory\Model\Category\Attribute\VirtualRule; | ||
|
||
/** | ||
* Virtual Category rule save handler. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteVirtualCategory | ||
* @author Romain Ruaud <[email protected]> | ||
*/ | ||
class SaveHandler implements \Magento\Framework\EntityManager\Operation\ExtensionInterface | ||
{ | ||
/** | ||
* The virtual rule attribute code. | ||
*/ | ||
const ATTRIBUTE_CODE = 'virtual_rule'; | ||
|
||
/** | ||
* @var \Smile\ElasticsuiteCatalogRule\Model\RuleFactory $ruleFactory | ||
*/ | ||
private $ruleFactory; | ||
|
||
/** | ||
* @var \Magento\Framework\Serialize\Serializer\Json | ||
*/ | ||
private $jsonSerializer; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Smile\ElasticsuiteCatalogRule\Model\RuleFactory $ruleFactory Search rule factory. | ||
* @param \Magento\Framework\Serialize\Serializer\Json $jsonSerializer JSON Serializer. | ||
*/ | ||
public function __construct( | ||
\Smile\ElasticsuiteCatalogRule\Model\RuleFactory $ruleFactory, | ||
\Magento\Framework\Serialize\Serializer\Json $jsonSerializer | ||
) { | ||
$this->ruleFactory = $ruleFactory; | ||
$this->jsonSerializer = $jsonSerializer; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function execute($entity, $arguments = []) | ||
{ | ||
$attributeData = $entity->getData(self::ATTRIBUTE_CODE); | ||
|
||
if ($attributeData !== null) { | ||
$rule = $this->ruleFactory->create(); | ||
|
||
if (is_object($attributeData)) { | ||
$rule = $attributeData; | ||
} elseif (is_array($attributeData)) { | ||
$rule->loadPost($attributeData); | ||
} elseif (is_string($attributeData)) { | ||
$attributeData = $this->jsonSerializer->unserialize($attributeData); | ||
$rule->getConditions()->loadArray($attributeData); | ||
} | ||
|
||
$entity->setData(self::ATTRIBUTE_CODE, $this->jsonSerializer->serialize($rule->getConditions()->asArray())); | ||
} | ||
|
||
return $entity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/module-elasticsuite-virtual-category/Observer/ConvertVirtualRuleOnSave.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteVirtualCategory | ||
* @author Romain Ruaud <[email protected]> | ||
* @copyright 2018 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteVirtualCategory\Observer; | ||
|
||
/** | ||
* Observer that convert 'virtual_rule' attribute to string value to ensure it gets properly processed by the | ||
* UpdateAttributes operation of the Entity Manager. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteVirtualCategory | ||
* @author Romain Ruaud <[email protected]> | ||
*/ | ||
class ConvertVirtualRuleOnSave implements \Magento\Framework\Event\ObserverInterface | ||
{ | ||
/** | ||
* @var \Smile\ElasticsuiteVirtualCategory\Model\Category\Attribute\VirtualRule\SaveHandler | ||
*/ | ||
private $saveHandler; | ||
|
||
/** | ||
* FlatPlugin constructor. | ||
* | ||
* @param \Smile\ElasticsuiteVirtualCategory\Model\Category\Attribute\VirtualRule\SaveHandler $saveHandler Read Handler | ||
*/ | ||
public function __construct( | ||
\Smile\ElasticsuiteVirtualCategory\Model\Category\Attribute\VirtualRule\SaveHandler $saveHandler | ||
) { | ||
$this->saveHandler = $saveHandler; | ||
} | ||
|
||
/** | ||
* Observer listening to magento_catalog_api_data_categoryinterface_save_before | ||
* | ||
* @param \Magento\Framework\Event\Observer $observer The Observer | ||
* | ||
* @return void | ||
*/ | ||
public function execute(\Magento\Framework\Event\Observer $observer) | ||
{ | ||
$entity = $observer->getEntity(); | ||
|
||
if ($entity->getVirtualRule() !== null) { | ||
$this->saveHandler->execute($entity); | ||
} | ||
} | ||
} |
Oops, something went wrong.