Passed
Push — master ( f5f62a...783e97 )
by Christian
12:34 queued 10s
created

createSetGroupDiscount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 10
dl 0
loc 41
rs 9.6333
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Test\Cart\Promotion\Helpers\Traits;
4
5
use Shopware\Core\Checkout\Promotion\Aggregate\PromotionDiscount\PromotionDiscountEntity;
6
use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
7
use Shopware\Core\Defaults;
8
use Shopware\Core\Framework\Context;
9
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
10
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
11
use Shopware\Core\Framework\Test\TestCaseBase\TaxAddToSalesChannelTestBehaviour;
12
use Shopware\Core\Framework\Uuid\Uuid;
13
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
14
use Shopware\Core\System\SalesChannel\SalesChannelContext;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
trait PromotionTestFixtureBehaviour
18
{
19
    use TaxAddToSalesChannelTestBehaviour;
20
21
    /**
22
     * @param $value
23
     */
24
    public function createSetGroupFixture(string $packagerKey, $value, string $sorterKey, string $promotionId, ContainerInterface $container): string
25
    {
26
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
27
28
        $repository = $container->get('promotion_setgroup.repository');
29
30
        $groupId = Uuid::randomHex();
31
32
        $data = [
33
            'id' => $groupId,
34
            'promotionId' => $promotionId,
35
            'packagerKey' => $packagerKey,
36
            'sorterKey' => $sorterKey,
37
            'value' => $value,
38
        ];
39
40
        $repository->create([$data], $context->getContext());
41
42
        return $groupId;
43
    }
44
45
    /**
46
     * Creates a new product in the database.
47
     */
48
    private function createTestFixtureProduct(string $productId, float $grossPrice, float $taxRate, ContainerInterface $container, SalesChannelContext $context): void
49
    {
50
        /** @var EntityRepositoryInterface $productRepository */
51
        $productRepository = $container->get('product.repository');
52
53
        $tax = ['id' => Uuid::randomHex(), 'taxRate' => $taxRate, 'name' => 'with id'];
54
55
        $productRepository->create(
56
            [
57
                [
58
                    'id' => $productId,
59
                    'productNumber' => $productId,
60
                    'stock' => 1,
61
                    'name' => 'Test',
62
                    'active' => true,
63
                    'price' => [
64
                        [
65
                            'currencyId' => Defaults::CURRENCY,
66
                            'gross' => $grossPrice,
67
                            'net' => 9, 'linked' => false,
68
                        ],
69
                    ],
70
                    'manufacturer' => ['name' => 'test'],
71
                    'tax' => $tax,
72
                    'visibilities' => [
73
                        ['salesChannelId' => $context->getSalesChannel()->getId(), 'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL],
74
                    ],
75
                    'categories' => [
76
                        ['id' => Uuid::randomHex(), 'name' => 'Clothing'],
77
                    ],
78
                ],
79
            ],
80
            $context->getContext()
81
        );
82
83
        $this->addTaxDataToSalesChannel($context, $tax);
84
    }
85
86
    /**
87
     * Creates a new absolute promotion in the database.
88
     *
89
     * @return string
90
     */
91
    private function createTestFixtureAbsolutePromotion(string $promotionId, string $code, float $value, ContainerInterface $container, string $scope = PromotionDiscountEntity::SCOPE_CART)
92
    {
93
        /** @var EntityRepositoryInterface $promotionRepository */
94
        $promotionRepository = $container->get('promotion.repository');
95
96
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
97
98
        $this->createPromotion(
99
            $promotionId,
100
            $code,
101
            $promotionRepository,
102
            $context
103
        );
104
105
        return $this->createTestFixtureDiscount($promotionId, PromotionDiscountEntity::TYPE_ABSOLUTE, $scope, $value, null, $container, $context);
106
    }
107
108
    /**
109
     * Creates a new percentage promotion in the database.
110
     *
111
     * @return string
112
     */
113
    private function createTestFixturePercentagePromotion(string $promotionId, ?string $code, float $percentage, ?float $maxValue, ContainerInterface $container, string $scope = PromotionDiscountEntity::SCOPE_CART)
114
    {
115
        /** @var EntityRepositoryInterface $promotionRepository */
116
        $promotionRepository = $container->get('promotion.repository');
117
118
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
119
120
        $this->createPromotion(
121
            $promotionId,
122
            $code,
123
            $promotionRepository,
124
            $context
125
        );
126
127
        return $this->createTestFixtureDiscount($promotionId, PromotionDiscountEntity::TYPE_PERCENTAGE, $scope, $percentage, $maxValue, $container, $context);
128
    }
129
130
    /**
131
     * Creates a new percentage promotion in the database.
132
     *
133
     * @return string
134
     */
135
    private function createTestFixtureSetGroupPromotion(string $promotionId, ?string $code, ContainerInterface $container)
136
    {
137
        /** @var EntityRepositoryInterface $promotionRepository */
138
        $promotionRepository = $container->get('promotion.repository');
139
140
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
141
142
        $this->createSetGroupPromotion(
143
            $promotionId,
144
            $code,
145
            $promotionRepository,
146
            $context
147
        );
148
    }
149
150
    private function createSetGroupDiscount(
151
        string $promotionId,
152
        int $groupIndex,
153
        ContainerInterface $container,
154
        float $value,
155
        ?float $maxValue,
156
        string $discountType = PromotionDiscountEntity::TYPE_PERCENTAGE,
157
        string $sortKey = 'PRICE_ASC',
158
        string $applierKey = 'ALL',
159
        string $usageKey = 'ALL',
160
        string $pickerKey = 'VERTICAL'
161
    ) {
162
        $scope = PromotionDiscountEntity::SCOPE_SETGROUP . '-' . $groupIndex;
163
164
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
165
166
        /** @var EntityRepositoryInterface $discountRepository */
167
        $discountRepository = $container->get('promotion_discount.repository');
168
169
        $discountId = Uuid::randomHex();
170
171
        $data = [
172
            'id' => $discountId,
173
            'promotionId' => $promotionId,
174
            'scope' => $scope,
175
            'type' => $discountType,
176
            'value' => $value,
177
            'considerAdvancedRules' => true,
178
            'sorterKey' => $sortKey,
179
            'applierKey' => $applierKey,
180
            'usageKey' => $usageKey,
181
            'pickerKey' => $pickerKey,
182
        ];
183
184
        if ($maxValue !== null) {
185
            $data['maxValue'] = $maxValue;
186
        }
187
188
        $discountRepository->create([$data], $context->getContext());
189
190
        return $discountId;
191
    }
192
193
    /**
194
     * Creates a new advanced currency price for the provided discount
195
     */
196
    private function createTestFixtureAdvancedPrice(string $discountId, string $currency, float $price, ContainerInterface $container): void
197
    {
198
        /** @var EntityRepositoryInterface $pricesRepository */
199
        $pricesRepository = $container->get('promotion_discount_prices.repository');
200
201
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
202
203
        $pricesRepository->create(
204
            [
205
                [
206
                    'discountId' => $discountId,
207
                    'currencyId' => $currency,
208
                    'price' => $price,
209
                ],
210
            ],
211
            $context->getContext()
212
        );
213
    }
214
215
    /**
216
     * function creates a discount for a promotion
217
     */
218
    private function createTestFixtureDiscount(
219
        string $promotionId,
220
        string $discountType,
221
        string $scope,
222
        float $value,
223
        ?float $maxValue,
224
        ContainerInterface $container,
225
        SalesChannelContext $context,
226
        bool $considerAdvancedRules = false
227
    ): string {
228
        $discountRepository = $container->get('promotion_discount.repository');
229
230
        $discountId = Uuid::randomHex();
231
232
        $data = [
233
            'id' => $discountId,
234
            'promotionId' => $promotionId,
235
            'scope' => $scope,
236
            'type' => $discountType,
237
            'value' => $value,
238
            'considerAdvancedRules' => $considerAdvancedRules,
239
        ];
240
241
        if ($maxValue !== null) {
242
            $data['maxValue'] = $maxValue;
243
        }
244
245
        $discountRepository->create([$data], $context->getContext());
246
247
        return $discountId;
248
    }
249
250
    /**
251
     * function creates a promotion
252
     */
253
    private function createPromotion(string $promotionId, ?string $code, EntityRepositoryInterface $promotionRepository, SalesChannelContext $context): EntityWrittenContainerEvent
254
    {
255
        $data = [
256
            'id' => $promotionId,
257
        ];
258
259
        if ($code !== null) {
260
            $data['code'] = $code;
261
            $data['useCodes'] = true;
262
        }
263
264
        return $this->createPromotionWithCustomData($data, $promotionRepository, $context);
265
    }
266
267
    private function createPromotionWithCustomData(array $data, EntityRepositoryInterface $promotionRepository, SalesChannelContext $context): EntityWrittenContainerEvent
268
    {
269
        $data = array_merge([
270
            'id' => Uuid::randomHex(),
271
            'name' => 'Black Friday',
272
            'active' => true,
273
            'useCodes' => false,
274
            'useSetGroups' => false,
275
            'salesChannels' => [
276
                ['salesChannelId' => $context->getSalesChannel()->getId(), 'priority' => 1],
277
            ],
278
        ], $data);
279
280
        return $promotionRepository->create([$data], $context->getContext());
281
    }
282
283
    /**
284
     * function creates an individual promotion code
285
     */
286
    private function createIndividualCode(string $promotionId, ?string $code, EntityRepositoryInterface $promotionIndividualRepository, Context $context): EntityWrittenContainerEvent
287
    {
288
        $data = [
289
            'id' => Uuid::randomHex(),
290
            'promotionId' => $promotionId,
291
            'code' => $code,
292
        ];
293
294
        return $promotionIndividualRepository->create([$data], $context);
295
    }
296
297
    private function createSetGroupPromotion(string $promotionId, ?string $code, EntityRepositoryInterface $promotionRepository, SalesChannelContext $context): void
298
    {
299
        $data = [
300
            'id' => $promotionId,
301
            'name' => 'Black Friday',
302
            'active' => true,
303
            'useCodes' => false,
304
            'useSetGroups' => true,
305
            'salesChannels' => [
306
                ['salesChannelId' => $context->getSalesChannel()->getId(), 'priority' => 1],
307
            ],
308
        ];
309
310
        if ($code !== null) {
311
            $data['code'] = $code;
312
            $data['useCodes'] = true;
313
        }
314
315
        $promotionRepository->create([$data], $context->getContext());
316
    }
317
318
    /**
319
     * Creates a new promotion with a discount that has a scope DELIVERY
320
     */
321
    private function createTestFixtureDeliveryPromotion(
322
        string $promotionId,
323
        string $discountType,
324
        float $value,
325
        ContainerInterface $container,
326
        SalesChannelContext $context,
327
        ?string $code
328
    ): string {
329
        /** @var EntityRepositoryInterface $promotionRepository */
330
        $promotionRepository = $container->get('promotion.repository');
331
332
        $this->createPromotion(
333
            $promotionId,
334
            $code,
335
            $promotionRepository,
336
            $context
337
        );
338
339
        $deliveryId = $this->createTestFixtureDiscount(
340
            $promotionId,
341
            $discountType,
342
            PromotionDiscountEntity::SCOPE_DELIVERY,
343
            $value,
344
            null,
345
            $container,
346
            $context
347
        );
348
349
        return $deliveryId;
350
    }
351
352
    /**
353
     * function creates a promotion and a discount for it.
354
     * function returns the id of the new discount
355
     */
356
    private function createTestFixtureFixedUnitDiscountPromotion(
357
        string $promotionId,
358
        float $fixedPrice,
359
        string $scope,
360
        ?string $code,
361
        ContainerInterface $container,
362
        SalesChannelContext $context,
363
        bool $considerAdvancedRules = false
364
    ): string {
365
        /** @var EntityRepositoryInterface $promotionRepository */
366
        $promotionRepository = $container->get('promotion.repository');
367
368
        $this->createPromotion(
369
            $promotionId,
370
            $code,
371
            $promotionRepository,
372
            $context
373
        );
374
375
        $discountId = $this->createTestFixtureDiscount(
376
            $promotionId,
377
            PromotionDiscountEntity::TYPE_FIXED_UNIT,
378
            $scope,
379
            $fixedPrice,
380
            null,
381
            $this->getContainer(),
0 ignored issues
show
Bug introduced by
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

381
            $this->/** @scrutinizer ignore-call */ 
382
                   getContainer(),
Loading history...
382
            $context,
383
            $considerAdvancedRules
384
        );
385
386
        return $discountId;
387
    }
388
389
    /**
390
     * function creates a promotion and a discount for it.
391
     * function returns the id of the new discount
392
     */
393
    private function createTestFixtureFixedDiscountPromotion(string $promotionId, float $fixedPrice, string $scope, ?string $code, ContainerInterface $container, SalesChannelContext $context): string
394
    {
395
        /** @var EntityRepositoryInterface $promotionRepository */
396
        $promotionRepository = $container->get('promotion.repository');
397
398
        $this->createPromotion(
399
            $promotionId,
400
            $code,
401
            $promotionRepository,
402
            $context
403
        );
404
405
        $discountId = $this->createTestFixtureDiscount(
406
            $promotionId,
407
            PromotionDiscountEntity::TYPE_FIXED,
408
            $scope,
409
            $fixedPrice,
410
            null,
411
            $this->getContainer(),
412
            $context,
413
            false
414
        );
415
416
        return $discountId;
417
    }
418
}
419