|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogRule\Test\Fixture; |
| 9 | + |
| 10 | +use Magento\CatalogRule\Test\Fixture\Data\ActionsSerializer; |
| 11 | +use Magento\CatalogRule\Test\Fixture\Data\ConditionsSerializer; |
| 12 | +use Magento\Customer\Model\ResourceModel\Group\CollectionFactory; |
| 13 | +use Magento\Framework\DataObject; |
| 14 | +use Magento\CatalogRule\Model\ResourceModel\Rule as ResourceModel; |
| 15 | +use Magento\CatalogRule\Model\RuleFactory; |
| 16 | +use Magento\Store\Model\StoreManagerInterface; |
| 17 | +use Magento\TestFramework\Fixture\Data\ProcessorInterface; |
| 18 | +use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface; |
| 19 | + |
| 20 | +class Rule implements RevertibleDataFixtureInterface |
| 21 | +{ |
| 22 | + private const DEFAULT_DATA = [ |
| 23 | + 'name' => 'catalogrule%uniqid%', |
| 24 | + 'sort_order' => 0, |
| 25 | + 'is_active' => 1, |
| 26 | + 'description' => null, |
| 27 | + 'website_ids' => [], |
| 28 | + 'customer_group_ids' => [], |
| 29 | + 'stop_rules_processing' => true, |
| 30 | + 'simple_action' => 'by_percent', |
| 31 | + 'discount_amount' => 0, |
| 32 | + 'conditions' => [], |
| 33 | + 'actions' => [], |
| 34 | + ]; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ProcessorInterface |
| 38 | + */ |
| 39 | + private ProcessorInterface $dataProcessor; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ResourceModel |
| 43 | + */ |
| 44 | + private ResourceModel $catalogRuleResourceModel; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var RuleFactory |
| 48 | + */ |
| 49 | + private RuleFactory $catalogRuleFactory; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var CollectionFactory |
| 53 | + */ |
| 54 | + private CollectionFactory $customerGroupCollectionFactory; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var StoreManagerInterface |
| 58 | + */ |
| 59 | + private StoreManagerInterface $storeManager; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var ConditionsSerializer |
| 63 | + */ |
| 64 | + private ConditionsSerializer $conditionsSerializer; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var ActionsSerializer |
| 68 | + */ |
| 69 | + private ActionsSerializer $actionsSerializer; |
| 70 | + |
| 71 | + /** |
| 72 | + * @param ProcessorInterface $dataProcessor |
| 73 | + * @param ResourceModel $catalogRuleResourceModel |
| 74 | + * @param RuleFactory $catalogRuleFactory |
| 75 | + * @param CollectionFactory $customerGroupCollectionFactory |
| 76 | + * @param StoreManagerInterface $storeManager |
| 77 | + * @param ConditionsSerializer $conditionsSerializer |
| 78 | + * @param ActionsSerializer $actionsSerializer |
| 79 | + */ |
| 80 | + public function __construct( |
| 81 | + ProcessorInterface $dataProcessor, |
| 82 | + ResourceModel $catalogRuleResourceModel, |
| 83 | + RuleFactory $catalogRuleFactory, |
| 84 | + CollectionFactory $customerGroupCollectionFactory, |
| 85 | + StoreManagerInterface $storeManager, |
| 86 | + ConditionsSerializer $conditionsSerializer, |
| 87 | + ActionsSerializer $actionsSerializer |
| 88 | + ) { |
| 89 | + $this->dataProcessor = $dataProcessor; |
| 90 | + $this->catalogRuleResourceModel = $catalogRuleResourceModel; |
| 91 | + $this->catalogRuleFactory = $catalogRuleFactory; |
| 92 | + $this->customerGroupCollectionFactory = $customerGroupCollectionFactory; |
| 93 | + $this->storeManager = $storeManager; |
| 94 | + $this->conditionsSerializer = $conditionsSerializer; |
| 95 | + $this->actionsSerializer = $actionsSerializer; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @inheritdoc |
| 100 | + */ |
| 101 | + public function apply(array $data = []): ?DataObject |
| 102 | + { |
| 103 | + /** @var \Magento\CatalogRule\Model\Rule $model */ |
| 104 | + $model = $this->catalogRuleFactory->create(); |
| 105 | + $data = $this->prepareData($data); |
| 106 | + $model->setData($data); |
| 107 | + $this->catalogRuleResourceModel->save($model); |
| 108 | + |
| 109 | + return $model; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * {@inheritdoc} |
| 114 | + * @param array $data Parameters. Same format as Rule::DEFAULT_DATA. |
| 115 | + * - $data['conditions']: can be supplied in following formats: |
| 116 | + * - [['attribute'=>'..','value'=>'..'],['attribute'=>'..','value'=>'..','operator'=>'..'], [..]] |
| 117 | + * - ['aggregator'=>'any', 'conditions' => [[..],[..]]] |
| 118 | + */ |
| 119 | + public function revert(DataObject $data): void |
| 120 | + { |
| 121 | + /** @var \Magento\CatalogRule\Model\Rule $model */ |
| 122 | + $model = $this->catalogRuleFactory->create(); |
| 123 | + $this->catalogRuleResourceModel->load($model, $data->getId()); |
| 124 | + if ($model->getId()) { |
| 125 | + $this->catalogRuleResourceModel->delete($model); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Prepares rule default data |
| 131 | + * |
| 132 | + * @return array |
| 133 | + */ |
| 134 | + private function prepareDefaultData(): array |
| 135 | + { |
| 136 | + $data = self::DEFAULT_DATA; |
| 137 | + $customerGroupCollection = $this->customerGroupCollectionFactory->create(); |
| 138 | + foreach ($customerGroupCollection->getAllIds() as $customerGroupId) { |
| 139 | + $data['customer_group_ids'][] = $customerGroupId; |
| 140 | + } |
| 141 | + |
| 142 | + foreach ($this->storeManager->getWebsites() as $website) { |
| 143 | + $data['website_ids'][] = $website->getId(); |
| 144 | + } |
| 145 | + |
| 146 | + return $data; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Prepare CatalogRule data |
| 151 | + * |
| 152 | + * @param array $data |
| 153 | + * @return array |
| 154 | + */ |
| 155 | + private function prepareData(array $data): array |
| 156 | + { |
| 157 | + $data = array_merge($this->prepareDefaultData(), $data); |
| 158 | + $data['conditions_serialized'] = $this->conditionsSerializer->serialize($data['conditions']); |
| 159 | + $data['actions_serialized'] = $this->actionsSerializer->serialize($data['actions']); |
| 160 | + unset($data['conditions'], $data['actions']); |
| 161 | + |
| 162 | + return $this->dataProcessor->process($this, $data); |
| 163 | + } |
| 164 | +} |
0 commit comments