|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2023 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * NOTICE: All information contained herein is, and remains |
| 7 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 8 | + * and technical concepts contained herein are proprietary to Adobe |
| 9 | + * and its suppliers and are protected by all applicable intellectual |
| 10 | + * property laws, including trade secret and copyright laws. |
| 11 | + * Dissemination of this information or reproduction of this material |
| 12 | + * is strictly forbidden unless prior written permission is obtained from |
| 13 | + * Adobe. |
| 14 | + */ |
| 15 | +declare(strict_types=1); |
| 16 | + |
| 17 | +namespace Magento\SalesRule\Test\Fixture; |
| 18 | + |
| 19 | +use Magento\Framework\DataObject; |
| 20 | +use Magento\SalesRule\Model\Spi\CouponResourceInterface; |
| 21 | +use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface; |
| 22 | +use Magento\SalesRule\Model\CouponFactory; |
| 23 | +use Magento\SalesRule\Api\Data\CouponInterface; |
| 24 | + |
| 25 | +class RuleCoupon implements RevertibleDataFixtureInterface |
| 26 | +{ |
| 27 | + private const DEFAULT_DATA = [ |
| 28 | + 'rule_id' => null, |
| 29 | + 'code' => null, |
| 30 | + 'usage_limit' => false, |
| 31 | + 'usage_per_customer' => false, |
| 32 | + 'type' => CouponInterface::TYPE_MANUAL |
| 33 | + ]; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var CouponFactory |
| 37 | + */ |
| 38 | + private CouponFactory $couponFactory; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var CouponResourceInterface |
| 42 | + */ |
| 43 | + private CouponResourceInterface $couponRuleResourceModel; |
| 44 | + |
| 45 | + /** |
| 46 | + * @param CouponResourceInterface $couponRuleResourceModel |
| 47 | + * @param CouponFactory $couponFactory |
| 48 | + */ |
| 49 | + public function __construct( |
| 50 | + CouponResourceInterface $couponRuleResourceModel, |
| 51 | + CouponFactory $couponFactory, |
| 52 | + ) { |
| 53 | + $this->couponRuleResourceModel = $couponRuleResourceModel; |
| 54 | + $this->couponFactory = $couponFactory; |
| 55 | + } |
| 56 | + |
| 57 | + public function apply(array $data = []): ?DataObject |
| 58 | + { |
| 59 | + $data = array_merge(self::DEFAULT_DATA, $data); |
| 60 | + $coupon = $this->couponFactory->create(); |
| 61 | + $coupon->setData($data); |
| 62 | + $this->couponRuleResourceModel->save($coupon); |
| 63 | + return $coupon; |
| 64 | + } |
| 65 | + |
| 66 | + public function revert(DataObject $data): void |
| 67 | + { |
| 68 | + $coupon = $this->couponFactory->create(); |
| 69 | + $this->couponRuleResourceModel->load($coupon, $data->getId()); |
| 70 | + if ($coupon->getId()) { |
| 71 | + $this->couponRuleResourceModel->delete($coupon); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments