Completed
Push — master ( 1671b1...4e9462 )
by Kamil
23:39
created

PromotionContext::thereIsPromotionWithCoupon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Component\Core\Factory\ActionFactoryInterface;
17
use Sylius\Component\Core\Factory\RuleFactoryInterface;
18
use Sylius\Component\Core\Model\PromotionInterface;
19
use Sylius\Component\Core\Model\TaxonInterface;
20
use Sylius\Component\Core\Test\Factory\TestPromotionFactoryInterface;
21
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
22
use Sylius\Component\Promotion\Model\ActionInterface;
23
use Sylius\Component\Promotion\Factory\CouponFactoryInterface;
24
use Sylius\Component\Promotion\Model\CouponInterface;
25
use Sylius\Component\Promotion\Repository\PromotionRepositoryInterface;
26
27
/**
28
 * @author Mateusz Zalewski <[email protected]>
29
 */
30
final class PromotionContext implements Context
31
{
32
    /**
33
     * @var SharedStorageInterface
34
     */
35
    private $sharedStorage;
36
37
    /**
38
     * @var ActionFactoryInterface
39
     */
40
    private $actionFactory;
41
42
    /**
43
     * @var CouponFactoryInterface
44
     */
45
    private $couponFactory;
46
47
    /**
48
     * @var RuleFactoryInterface
49
     */
50
    private $ruleFactory;
51
52
    /**
53
     * @var TestPromotionFactoryInterface
54
     */
55
    private $testPromotionFactory;
56
57
    /**
58
     * @var PromotionRepositoryInterface
59
     */
60
    private $promotionRepository;
61
62
    /**
63
     * @var ObjectManager
64
     */
65
    private $objectManager;
66
67
    /**
68
     * @param SharedStorageInterface $sharedStorage
69
     * @param ActionFactoryInterface $actionFactory
70
     * @param CouponFactoryInterface $couponFactory
71
     * @param RuleFactoryInterface $ruleFactory
72
     * @param TestPromotionFactoryInterface $testPromotionFactory
73
     * @param PromotionRepositoryInterface $promotionRepository
74
     * @param ObjectManager $objectManager
75
     */
76
    public function __construct(
77
        SharedStorageInterface $sharedStorage,
78
        ActionFactoryInterface $actionFactory,
79
        CouponFactoryInterface $couponFactory,
80
        RuleFactoryInterface $ruleFactory,
81
        TestPromotionFactoryInterface $testPromotionFactory,
82
        PromotionRepositoryInterface $promotionRepository,
83
        ObjectManager $objectManager
84
    ) {
85
        $this->sharedStorage = $sharedStorage;
86
        $this->actionFactory = $actionFactory;
87
        $this->couponFactory = $couponFactory;
88
        $this->ruleFactory = $ruleFactory;
89
        $this->testPromotionFactory = $testPromotionFactory;
90
        $this->promotionRepository = $promotionRepository;
91
        $this->objectManager = $objectManager;
92
    }
93
94
    /**
95
     * @Given there is a promotion :promotionName
96
     */
97
    public function thereIsPromotion($promotionName)
98
    {
99
        $promotion = $this->testPromotionFactory
100
            ->createForChannel($promotionName, $this->sharedStorage->get('channel'))
101
        ;
102
103
        $this->promotionRepository->add($promotion);
104
        $this->sharedStorage->set('promotion', $promotion);
105
    }
106
107
    /**
108
     * @Given the store has promotion :promotionName with coupon :couponCode
109
     */
110
    public function thereIsPromotionWithCoupon($promotionName, $couponCode)
111
    {
112
        /** @var CouponInterface $coupon */
113
        $coupon = $this->couponFactory->createNew();
114
        $coupon->setCode($couponCode);
115
116
        $promotion = $this->testPromotionFactory
117
            ->createForChannel($promotionName, $this->sharedStorage->get('channel'))
118
        ;
119
        $promotion->addCoupon($coupon);
120
        $promotion->setCouponBased(true);
121
122
        $this->promotionRepository->add($promotion);
123
        $this->sharedStorage->set('promotion', $promotion);
124
        $this->sharedStorage->set('coupon', $coupon);
125
    }
126
127
    /**
128
     * @Given /^([^"]+) gives ("[^"]+") fixed discount to every order$/
129
     */
130
    public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $amount)
131
    {
132
        $action = $this->actionFactory->createFixedDiscount($amount);
133
        $promotion->addAction($action);
134
135
        $this->objectManager->flush();
136
    }
137
138
    /**
139
     * @Given /^([^"]+) gives ("[^"]+") percentage discount to every order$/
140
     */
141
    public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount)
142
    {
143
        $action = $this->actionFactory->createPercentageDiscount($discount);
144
        $promotion->addAction($action);
145
146
        $this->objectManager->flush();
147
    }
148
149
    /**
150
     * @Given /^([^"]+) gives ("[^"]+") fixed discount to every order with quantity at least ([^"]+)$/
151
     */
152
    public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast(PromotionInterface $promotion, $amount, $quantity)
153
    {
154
        $action = $this->actionFactory->createFixedDiscount($amount);
155
        $promotion->addAction($action);
156
157
        $rule = $this->ruleFactory->createCartQuantity((int) $quantity);
158
        $promotion->addRule($rule);
159
160
        $this->objectManager->flush();
161
    }
162
163
    /**
164
     * @Given /^([^"]+) gives ("[^"]+") fixed discount to every order with items total at least ("[^"]+")$/
165
     */
166
    public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast(
167
        PromotionInterface $promotion,
168
        $amount,
169
        $targetAmount
170
    ) {
171
        $action = $this->actionFactory->createFixedDiscount($amount);
172
        $promotion->addAction($action);
173
174
        $rule = $this->ruleFactory->createItemTotal($targetAmount);
175
        $promotion->addRule($rule);
176
177
        $this->objectManager->flush();
178
    }
179
180
    /**
181
     * @Given /^([^"]+) gives ("[^"]+") percentage discount on shipping to every order$/
182
     */
183
    public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount)
184
    {
185
        $action = $this->actionFactory->createPercentageShippingDiscount($discount);
186
        $promotion->addAction($action);
187
188
        $this->objectManager->flush();
189
    }
190
191
    /**
192
     * @Given /^([^"]+) gives ("[^"]+%") off every product (classified as "[^"]+")$/
193
     */
194
    public function itGivesPercentageOffEveryProductClassifiedAs(
195
        PromotionInterface $promotion,
196
        $discount,
197
        TaxonInterface $taxon
198
    ) {
199
        $action = $this->actionFactory->createItemPercentageDiscount($discount);
200
        $promotion->addAction($this->configureActionTaxonFilter($action, [$taxon->getCode()]));
201
202
        $this->objectManager->flush();
203
    }
204
205
    /**
206
     * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/
207
     */
208
    public function itGivesFixedOffEveryProductClassifiedAs(
209
        PromotionInterface $promotion,
210
        $discount,
211
        TaxonInterface $taxon
212
    ) {
213
        $action = $this->actionFactory->createItemFixedDiscount($discount);
214
        $promotion->addAction($this->configureActionTaxonFilter($action, [$taxon->getCode()]));
215
216
        $this->objectManager->flush();
217
    }
218
219
    /**
220
     * @param ActionInterface $action
221
     * @param array $taxonCodes
222
     *
223
     * @return ActionInterface
224
     */
225
    private function configureActionTaxonFilter(ActionInterface $action, array $taxonCodes)
226
    {
227
        $configuration = array_merge(['filters' => ['taxons' => $taxonCodes]], $action->getConfiguration());
228
        $action->setConfiguration($configuration);
229
230
        return $action;
231
    }
232
}
233