Skip to content

Commit

Permalink
Merge pull request #1054 from romainruaud/fix_optimizer-json-backend
Browse files Browse the repository at this point in the history
Store Optimizer config and rule condition as JSON.
  • Loading branch information
romainruaud authored Nov 21, 2018
2 parents 88e2310 + d5b5df0 commit 2ed106b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/module-elasticsuite-catalog-optimizer/Model/Optimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class Optimizer extends \Magento\Framework\Model\AbstractModel implements Optimi
*/
private $dateFilter;

/**
* @var \Magento\Framework\Serialize\SerializerInterface
*/
private $serializer;

/**
* @var string
*/
Expand All @@ -52,6 +57,7 @@ class Optimizer extends \Magento\Framework\Model\AbstractModel implements Optimi
* @param \Magento\Framework\Registry $registry Registry.
* @param \Smile\ElasticsuiteCatalogRule\Model\RuleFactory $ruleFactory Rule factory.
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter Date Filter.
* @param \Magento\Framework\Serialize\SerializerInterface $serializer Serializer.
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource Resource.
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection Resource collection.
* @param array $data Data.
Expand All @@ -61,13 +67,15 @@ public function __construct(
\Magento\Framework\Registry $registry,
\Smile\ElasticsuiteCatalogRule\Model\RuleFactory $ruleFactory,
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
\Magento\Framework\Serialize\SerializerInterface $serializer,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
$this->ruleFactory = $ruleFactory;
$this->dateFilter = $dateFilter;
$this->serializer = $serializer;
}

/**
Expand Down Expand Up @@ -118,7 +126,7 @@ public function getModel()
public function getConfig($key = null)
{
if (is_string($this->getData(self::CONFIG))) {
$this->setData(self::CONFIG, unserialize($this->getData(self::CONFIG)));
$this->setData(self::CONFIG, $this->serializer->unserialize($this->getData(self::CONFIG)));
}

$result = $this->getData(self::CONFIG);
Expand Down Expand Up @@ -173,7 +181,7 @@ public function getRuleCondition()
$rule = $this->ruleFactory->create();

if (is_string($ruleData)) {
$ruleData = unserialize($ruleData);
$ruleData = $this->serializer->unserialize($ruleData);
}

if (is_array($ruleData)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ class Optimizer extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
*
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context Context.
* @param RuleFactory $ruleFactory Rule factory.
* @param \Magento\Framework\Serialize\SerializerInterface $serializer Serializer.
* @param string $connectionName Connection name.
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
RuleFactory $ruleFactory,
\Magento\Framework\Serialize\SerializerInterface $serializer,
$connectionName = null
) {
parent::__construct($context, $connectionName);
$this->ruleFactory = $ruleFactory;
$this->serializer = $serializer;
}

/**
Expand Down Expand Up @@ -117,7 +120,7 @@ protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
{
if (is_array($object->getConfig())) {
$object->setConfig(serialize($object->getConfig()));
$object->setConfig($this->serializer->serialize($object->getConfig()));
}

$rule = $this->ruleFactory->create();
Expand All @@ -128,7 +131,7 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
} elseif (is_array($ruleCondition)) {
$rule->getConditions()->loadArray($ruleCondition);
}
$object->setRuleCondition(serialize($rule->getConditions()->asArray()));
$object->setRuleCondition($this->serializer->serialize($rule->getConditions()->asArray()));

return parent::_beforeSave($object);
}
Expand Down
47 changes: 43 additions & 4 deletions src/module-elasticsuite-catalog-optimizer/Setup/OptimizerSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,23 @@ class OptimizerSetup
*/
private $metadataPool;

/**
* @var \Magento\Framework\DB\FieldDataConverterFactory
*/
private $fieldDataConverterFactory;

/**
* Class Constructor
*
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool Metadata Pool.
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool Metadata Pool.
* @param \Magento\Framework\DB\FieldDataConverterFactory $fieldDataConverterFactory Field Data Converter Factory.
*/
public function __construct(\Magento\Framework\EntityManager\MetadataPool $metadataPool)
{
$this->metadataPool = $metadataPool;
public function __construct(
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
\Magento\Framework\DB\FieldDataConverterFactory $fieldDataConverterFactory
) {
$this->metadataPool = $metadataPool;
$this->fieldDataConverterFactory = $fieldDataConverterFactory;
}

/**
Expand Down Expand Up @@ -269,4 +278,34 @@ public function createOptimizerLimitationTable(SchemaSetupInterface $setup)
$setup->getConnection()->createTable($optimizerCategoryTable);
}
}

/**
* Upgrade legacy serialized data to JSON data.
* Targets :
* - columns "config" and "rule_condition" of the smile_elasticsuite_optimizer table.
*
* @param \Magento\Setup\Module\DataSetup $setup Setup
*
* @return void
*/
public function convertSerializedRulesToJson(\Magento\Setup\Module\DataSetup $setup)
{
$fieldDataConverter = $this->fieldDataConverterFactory->create(
\Magento\Framework\DB\DataConverter\SerializedToJson::class
);

$fieldDataConverter->convert(
$setup->getConnection(),
$setup->getTable(OptimizerInterface::TABLE_NAME),
OptimizerInterface::OPTIMIZER_ID,
OptimizerInterface::CONFIG
);

$fieldDataConverter->convert(
$setup->getConnection(),
$setup->getTable(OptimizerInterface::TABLE_NAME),
OptimizerInterface::OPTIMIZER_ID,
OptimizerInterface::RULE_CONDITION
);
}
}
53 changes: 53 additions & 0 deletions src/module-elasticsuite-catalog-optimizer/Setup/UpgradeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\ElasticsuiteCatalogOptimizer
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2018 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCatalogOptimizer\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
* Upgrade Data for Smile Elasticsuite Optimizer module.
*
* @category Smile
* @package Smile\ElasticsuiteCatalogOptimizer
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
{
/**
* @var OptimizerSetup
*/
private $optimizerSetup;

/**
* InstallSchema constructor.
*
* @param \Smile\ElasticsuiteCatalogOptimizer\Setup\OptimizerSetupFactory $optimizerSetupFactory Setup Factory
*/
public function __construct(OptimizerSetupFactory $optimizerSetupFactory)
{
$this->optimizerSetup = $optimizerSetupFactory->create();
}

/**
* {@inheritdoc}
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if (version_compare($context->getVersion(), "1.2.0") < 0) {
$this->optimizerSetup->convertSerializedRulesToJson($setup);
}
$setup->endSetup();
}
}
2 changes: 1 addition & 1 deletion src/module-elasticsuite-catalog-optimizer/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Smile_ElasticsuiteCatalogOptimizer" setup_version="1.1.0">
<module name="Smile_ElasticsuiteCatalogOptimizer" setup_version="1.2.0">
<sequence>
<module name="Smile_ElasticsuiteCatalogRule"/>
</sequence>
Expand Down

0 comments on commit 2ed106b

Please sign in to comment.