Completed
Push — master ( da2f6e...d3b8cb )
by Kamil
20:06
created

itGivesFixedDiscountToEveryOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
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\Repository\PromotionRepositoryInterface;
24
25
/**
26
 * @author Mateusz Zalewski <[email protected]>
27
 */
28
final class PromotionContext implements Context
29
{
30
    /**
31
     * @var SharedStorageInterface
32
     */
33
    private $sharedStorage;
34
35
    /**
36
     * @var ActionFactoryInterface
37
     */
38
    private $actionFactory;
39
40
    /**
41
     * @var RuleFactoryInterface
42
     */
43
    private $ruleFactory;
44
45
    /**
46
     * @var TestPromotionFactoryInterface
47
     */
48
    private $testPromotionFactory;
49
50
    /**
51
     * @var PromotionRepositoryInterface
52
     */
53
    private $promotionRepository;
54
55
    /**
56
     * @var ObjectManager
57
     */
58
    private $objectManager;
59
60
    /**
61
     * @param SharedStorageInterface $sharedStorage
62
     * @param ActionFactoryInterface $actionFactory
63
     * @param RuleFactoryInterface $ruleFactory
64
     * @param TestPromotionFactoryInterface $testPromotionFactory
65
     * @param PromotionRepositoryInterface $promotionRepository
66
     * @param ObjectManager $objectManager
67
     */
68
    public function __construct(
69
        SharedStorageInterface $sharedStorage,
70
        ActionFactoryInterface $actionFactory,
71
        RuleFactoryInterface $ruleFactory,
72
        TestPromotionFactoryInterface $testPromotionFactory,
73
        PromotionRepositoryInterface $promotionRepository,
74
        ObjectManager $objectManager
75
    ) {
76
        $this->sharedStorage = $sharedStorage;
77
        $this->actionFactory = $actionFactory;
78
        $this->ruleFactory = $ruleFactory;
79
        $this->testPromotionFactory = $testPromotionFactory;
80
        $this->promotionRepository = $promotionRepository;
81
        $this->objectManager = $objectManager;
82
    }
83
84
    /**
85
     * @Given there is a promotion :promotionName
86
     */
87
    public function thereIsPromotion($promotionName)
88
    {
89
        $promotion = $this->testPromotionFactory
90
            ->createForChannel($promotionName, $this->sharedStorage->get('channel'))
91
        ;
92
93
        $this->promotionRepository->add($promotion);
94
        $this->sharedStorage->set('promotion', $promotion);
95
    }
96
97
    /**
98
     * @Given /^([^"]+) gives ("[^"]+") fixed discount to every order$/
99
     */
100
    public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $amount)
101
    {
102
        $action = $this->actionFactory->createFixedDiscount($amount);
103
        $promotion->addAction($action);
104
105
        $this->objectManager->flush();
106
    }
107
108
    /**
109
     * @Given /^([^"]+) gives ("[^"]+") percentage discount to every order$/
110
     */
111
    public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount)
112
    {
113
        $action = $this->actionFactory->createPercentageDiscount($discount);
114
        $promotion->addAction($action);
115
116
        $this->objectManager->flush();
117
    }
118
119
    /**
120
     * @Given /^([^"]+) gives ("[^"]+") fixed discount to every order with quantity at least ([^"]+)$/
121
     */
122
    public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast(PromotionInterface $promotion, $amount, $quantity)
123
    {
124
        $action = $this->actionFactory->createFixedDiscount($amount);
125
        $promotion->addAction($action);
126
127
        $rule = $this->ruleFactory->createCartQuantity((int) $quantity);
128
        $promotion->addRule($rule);
129
130
        $this->objectManager->flush();
131
    }
132
133
    /**
134
     * @Given /^([^"]+) gives ("[^"]+") fixed discount to every order with items total at least ("[^"]+")$/
135
     */
136
    public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast(
137
        PromotionInterface $promotion,
138
        $amount,
139
        $targetAmount
140
    ) {
141
        $action = $this->actionFactory->createFixedDiscount($amount);
142
        $promotion->addAction($action);
143
144
        $rule = $this->ruleFactory->createItemTotal($targetAmount);
145
        $promotion->addRule($rule);
146
147
        $this->objectManager->flush();
148
    }
149
150
    /**
151
     * @Given /^([^"]+) gives ("[^"]+") percentage discount on shipping to every order$/
152
     */
153
    public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount)
154
    {
155
        $action = $this->actionFactory->createPercentageShippingDiscount($discount);
156
        $promotion->addAction($action);
157
158
        $this->objectManager->flush();
159
    }
160
161
    /**
162
     * @Given /^([^"]+) gives ("[^"]+%") off every product (classified as "[^"]+")$/
163
     */
164
    public function itGivesPercentageOffEveryProductClassifiedAs(
165
        PromotionInterface $promotion,
166
        $discount,
167
        TaxonInterface $taxon
168
    ) {
169
        $action = $this->actionFactory->createItemPercentageDiscount($discount);
170
        $promotion->addAction($this->configureActionTaxonFilter($action, [$taxon->getCode()]));
171
172
        $this->objectManager->flush();
173
    }
174
175
    /**
176
     * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/
177
     */
178
    public function itGivesFixedOffEveryProductClassifiedAs(
179
        PromotionInterface $promotion,
180
        $discount,
181
        TaxonInterface $taxon
182
    ) {
183
        $action = $this->actionFactory->createItemFixedDiscount($discount);
184
        $promotion->addAction($this->configureActionTaxonFilter($action, [$taxon->getCode()]));
185
186
        $this->objectManager->flush();
187
    }
188
189
    /**
190
     * @param ActionInterface $action
191
     * @param array $taxonCodes
192
     *
193
     * @return ActionInterface
194
     */
195
    private function configureActionTaxonFilter(ActionInterface $action, array $taxonCodes)
196
    {
197
        $configuration = array_merge(['filters' => ['taxons' => $taxonCodes]], $action->getConfiguration());
198
        $action->setConfiguration($configuration);
199
200
        return $action;
201
    }
202
}
203