| Current Path : /home/rtorresani/www/vendor/magento/module-catalog-rule/Model/ResourceModel/Rule/ |
| Current File : //home/rtorresani/www/vendor/magento/module-catalog-rule/Model/ResourceModel/Rule/Collection.php |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogRule\Model\ResourceModel\Rule;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;
/**
* @api
*/
class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\AbstractCollection
{
/**
* @var array
*/
protected $_associatedEntitiesMap;
/**
* @var Json
*/
protected $serializer;
/**
* @var string
*/
protected $_eventPrefix = 'catalog_rule_collection';
/**
* @var string
*/
protected $_eventObject = 'catalog_rule';
/**
* Collection constructor.
* @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
* @param Json|null $serializer
*/
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
Json $serializer = null
) {
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
$this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap();
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
}
/**
* Set resource model
*
* @return void
* @codeCoverageIgnore
*/
protected function _construct()
{
$this->_init(\Magento\CatalogRule\Model\Rule::class, \Magento\CatalogRule\Model\ResourceModel\Rule::class);
}
/**
* Find product attribute in conditions or actions
*
* @param string $attributeCode
* @return $this
*/
public function addAttributeInConditionFilter($attributeCode)
{
$match = sprintf('%%%s%%', substr($this->serializer->serialize(['attribute' => $attributeCode]), 1, -1));
$this->addFieldToFilter('conditions_serialized', ['like' => $match]);
return $this;
}
/**
* Map data for associated entities
*
* @param string $entityType
* @param string $objectField
* @throws \Magento\Framework\Exception\LocalizedException
* @return void
*/
protected function mapAssociatedEntities($entityType, $objectField)
{
if (!$this->_items) {
return;
}
$entityInfo = $this->_getAssociatedEntityInfo($entityType);
$ruleIdField = $entityInfo['rule_id_field'];
$entityIds = $this->getColumnValues($ruleIdField);
$select = $this->getConnection()->select()->from(
$this->getTable($entityInfo['associations_table'])
)->where(
$ruleIdField . ' IN (?)',
$entityIds
);
$associatedEntities = $this->getConnection()->fetchAll($select);
array_map(function ($associatedEntity) use ($entityInfo, $ruleIdField, $objectField) {
$item = $this->getItemByColumnValue($ruleIdField, $associatedEntity[$ruleIdField]);
$itemAssociatedValue = $item->getData($objectField) === null ? [] : $item->getData($objectField);
$itemAssociatedValue[] = $associatedEntity[$entityInfo['entity_id_field']];
$item->setData($objectField, $itemAssociatedValue);
}, $associatedEntities);
}
/**
* Perform operations after collection load
*
* @return $this
*/
protected function _afterLoad()
{
$this->mapAssociatedEntities('website', 'website_ids');
$this->mapAssociatedEntities('customer_group', 'customer_group_ids');
$this->setFlag('add_websites_to_result', false);
return parent::_afterLoad();
}
/**
* Limit rules collection by specific customer group
*
* @param int $customerGroupId
* @return $this
*/
public function addCustomerGroupFilter($customerGroupId)
{
$entityInfo = $this->_getAssociatedEntityInfo('customer_group');
if (!$this->getFlag('is_customer_group_joined')) {
$this->setFlag('is_customer_group_joined', true);
$this->getSelect()->join(
['customer_group' => $this->getTable($entityInfo['associations_table'])],
$this->getConnection()
->quoteInto('customer_group.' . $entityInfo['entity_id_field'] . ' = ?', $customerGroupId)
. ' AND main_table.' . $entityInfo['rule_id_field'] . ' = customer_group.'
. $entityInfo['rule_id_field'],
[]
);
}
return $this;
}
/**
* Getter for _associatedEntitiesMap property
*
* @return array
* @deprecated 100.1.0
*/
private function getAssociatedEntitiesMap()
{
if (!$this->_associatedEntitiesMap) {
$this->_associatedEntitiesMap = \Magento\Framework\App\ObjectManager::getInstance()
// phpstan:ignore
->get(\Magento\CatalogRule\Model\ResourceModel\Rule\AssociatedEntityMap::class)
->getData();
}
return $this->_associatedEntitiesMap;
}
}