Completed
Push — master ( 6570ae...141d1f )
by Kamil
11:00
created

src/Sylius/Behat/Context/Setup/ShippingContext.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Setup;
15
16
use Behat\Behat\Context\Context;
17
use Doctrine\Common\Persistence\ObjectManager;
18
use Sylius\Behat\Service\SharedStorageInterface;
19
use Sylius\Bundle\CoreBundle\Fixture\Factory\ShippingMethodExampleFactory;
20
use Sylius\Component\Addressing\Model\Scope;
21
use Sylius\Component\Addressing\Model\ZoneInterface;
22
use Sylius\Component\Core\Model\ChannelInterface;
23
use Sylius\Component\Core\Model\Scope as CoreScope;
24
use Sylius\Component\Core\Model\ShippingMethodInterface;
25
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
26
use Sylius\Component\Resource\Factory\FactoryInterface;
27
use Sylius\Component\Resource\Repository\RepositoryInterface;
28
use Sylius\Component\Shipping\Calculator\DefaultCalculators;
29
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
30
use Sylius\Component\Shipping\Model\ShippingMethodTranslationInterface;
31
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
32
33
/**
34
 * @author Arkadiusz Krakowiak <[email protected]>
35
 */
36
final class ShippingContext implements Context
37
{
38
    /**
39
     * @var SharedStorageInterface
40
     */
41
    private $sharedStorage;
42
43
    /**
44
     * @var ShippingMethodRepositoryInterface
45
     */
46
    private $shippingMethodRepository;
47
48
    /**
49
     * @var RepositoryInterface
50
     */
51
    private $zoneRepository;
52
53
    /**
54
     * @var ShippingMethodExampleFactory
55
     */
56
    private $shippingMethodExampleFactory;
57
58
    /**
59
     * @var FactoryInterface
60
     */
61
    private $shippingMethodTranslationFactory;
62
63
    /**
64
     * @var ObjectManager
65
     */
66
    private $shippingMethodManager;
67
68
    /**
69
     * @param SharedStorageInterface $sharedStorage
70
     * @param ShippingMethodRepositoryInterface $shippingMethodRepository
71
     * @param RepositoryInterface $zoneRepository
72
     * @param ShippingMethodExampleFactory $shippingMethodExampleFactory
73
     * @param FactoryInterface $shippingMethodTranslationFactory
74
     * @param ObjectManager $shippingMethodManager
75
     */
76
    public function __construct(
77
        SharedStorageInterface $sharedStorage,
78
        ShippingMethodRepositoryInterface $shippingMethodRepository,
79
        RepositoryInterface $zoneRepository,
80
        ShippingMethodExampleFactory $shippingMethodExampleFactory,
81
        FactoryInterface $shippingMethodTranslationFactory,
82
        ObjectManager $shippingMethodManager
83
    ) {
84
        $this->sharedStorage = $sharedStorage;
85
        $this->shippingMethodRepository = $shippingMethodRepository;
86
        $this->zoneRepository = $zoneRepository;
87
        $this->shippingMethodExampleFactory = $shippingMethodExampleFactory;
88
        $this->shippingMethodTranslationFactory = $shippingMethodTranslationFactory;
89
        $this->shippingMethodManager = $shippingMethodManager;
90
    }
91
92
    /**
93
     * @Given the store ships everything for free within the :zone zone
94
     */
95
    public function storeShipsEverythingForFree(ZoneInterface $zone)
96
    {
97
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
98
            'name' => 'Free',
99
            'enabled' => true,
100
            'zone' => $zone,
101
            'calculator' => [
102
                'type' => DefaultCalculators::FLAT_RATE,
103
                'configuration' => $this->getConfigurationByChannels([$this->sharedStorage->get('channel')]),
104
            ],
105
        ]));
106
    }
107
108
    /**
109
     * @Given the store ships everywhere for free
110
     */
111
    public function theStoreShipsEverywhereForFree()
112
    {
113
        /** @var ZoneInterface $zone */
114
        foreach ($this->zoneRepository->findBy(['scope' => [CoreScope::SHIPPING, Scope::ALL]]) as $zone) {
115
            $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
116
                'name' => 'Free',
117
                'code' => 'FREE-' . $zone->getCode(),
118
                'enabled' => true,
119
                'zone' => $zone,
120
                'calculator' => [
121
                    'type' => DefaultCalculators::FLAT_RATE,
122
                    'configuration' => $this->getConfigurationByChannels([$this->sharedStorage->get('channel')]),
123
                ],
124
            ]));
125
        }
126
    }
127
128
    /**
129
     * @Given /^the store ships everywhere for free for (all channels)$/
130
     */
131
    public function theStoreShipsEverywhereForFreeForAllChannels(array $channels)
132
    {
133
        foreach ($this->zoneRepository->findBy(['scope' => [CoreScope::SHIPPING, Scope::ALL]]) as $zone) {
134
            $configuration = $this->getConfigurationByChannels($channels);
135
            $shippingMethod = $this->shippingMethodExampleFactory->create([
136
                'name' => 'Free',
137
                'enabled' => true,
138
                'zone' => $zone,
139
                'calculator' => [
140
                    'type' => DefaultCalculators::FLAT_RATE,
141
                    'configuration' => $configuration,
142
                ],
143
                'channels' => $channels,
144
            ]);
145
146
            $this->saveShippingMethod($shippingMethod);
147
        }
148
    }
149
150
    /**
151
     * @Given the store (also )allows shipping with :name
152
     */
153
    public function theStoreAllowsShippingMethodWithName($name)
154
    {
155
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create(['name' => $name, 'enabled' => true]));
156
    }
157
158
    /**
159
     * @Given the store (also )allows shipping with :name identified by :code
160
     */
161
    public function theStoreAllowsShippingMethodWithNameAndCode($name, $code)
162
    {
163
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
164
            'name' => $name,
165
            'zone' => $this->getShippingZone(),
166
            'enabled' => true,
167
            'code' => $code,
168
        ]));
169
    }
170
171
    /**
172
     * @Given the store (also )allows shipping with :name at position :position
173
     */
174
    public function theStoreAllowsShippingMethodWithNameAndPosition($name, $position)
175
    {
176
        $shippingMethod = $this->shippingMethodExampleFactory->create([
177
            'name' => $name,
178
            'enabled' => true,
179
            'zone' => $this->getShippingZone(),
180
        ]);
181
182
        $shippingMethod->setPosition((int) $position);
183
184
        $this->saveShippingMethod($shippingMethod);
185
    }
186
187
    /**
188
     * @Given /^(this shipping method) is named "([^"]+)" in the "([^"]+)" locale$/
189
     */
190
    public function thisShippingMethodIsNamedInLocale(ShippingMethodInterface $shippingMethod, $name, $locale)
191
    {
192
        /** @var ShippingMethodTranslationInterface $translation */
193
        $translation = $this->shippingMethodTranslationFactory->createNew();
194
        $translation->setLocale($locale);
195
        $translation->setName($name);
196
197
        $shippingMethod->addTranslation($translation);
198
199
        $this->shippingMethodManager->flush();
200
    }
201
202
    /**
203
     * @Given the store allows shipping with :firstName and :secondName
204
     */
205
    public function theStoreAllowsShippingWithAnd(...$names)
206
    {
207
        foreach ($names as $name) {
208
            $this->theStoreAllowsShippingMethodWithName($name);
209
        }
210
    }
211
212
    /**
213
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee within the ("[^"]+" zone)$/
214
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee for the (rest of the world)$/
215
     */
216
    public function storeHasShippingMethodWithFeeAndZone($shippingMethodName, $fee, ZoneInterface $zone)
217
    {
218
        $channel = $this->sharedStorage->get('channel');
219
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
220
221
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
222
            'name' => $shippingMethodName,
223
            'enabled' => true,
224
            'zone' => $zone,
225
            'calculator' => [
226
                'type' => DefaultCalculators::FLAT_RATE,
227
                'configuration' => $configuration,
228
            ],
229
            'channels' => [$this->sharedStorage->get('channel')],
230
        ]));
231
    }
232
233
    /**
234
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee$/
235
     */
236
    public function storeHasShippingMethodWithFee($shippingMethodName, $fee)
237
    {
238
        $channel = $this->sharedStorage->get('channel');
239
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
240
241
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
242
            'name' => $shippingMethodName,
243
            'enabled' => true,
244
            'zone' => $this->getShippingZone(),
245
            'calculator' => [
246
                'type' => DefaultCalculators::FLAT_RATE,
247
                'configuration' => $configuration,
248
            ],
249
            'channels' => [$this->sharedStorage->get('channel')],
250
        ]));
251
    }
252
253
    /**
254
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per shipment for ("[^"]+" channel) and ("[^"]+") for ("[^"]+" channel)$/
255
     */
256
    public function storeHasShippingMethodWithFeePerShipmentForChannels(
257
        $shippingMethodName,
258
        $firstFee,
259
        ChannelInterface $firstChannel,
260
        $secondFee,
261
        ChannelInterface $secondChannel
262
    ) {
263
        $configuration[$firstChannel->getCode()] = ['amount' => $firstFee];
264
        $configuration[$secondChannel->getCode()] = ['amount' => $secondFee];
265
266
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
267
            'name' => $shippingMethodName,
268
            'enabled' => true,
269
            'zone' => $this->getShippingZone(),
270
            'calculator' => [
271
                'type' => DefaultCalculators::FLAT_RATE,
272
                'configuration' => $configuration,
273
            ],
274
            'channels' => [$firstChannel, $secondChannel],
275
        ]));
276
    }
277
278
    /**
279
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per unit for ("[^"]+" channel)$/
280
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per unit for ("[^"]+" channel) and ("[^"]+") for ("[^"]+" channel)$/
281
     */
282
    public function storeHasShippingMethodWithFeePerUnitForChannels(
283
        $shippingMethodName,
284
        $firstFee,
285
        ChannelInterface $firstChannel,
286
        $secondFee = null,
287
        ChannelInterface $secondChannel = null
288
    ) {
289
        $configuration = [];
290
        $channels = [];
291
292
        $configuration[$firstChannel->getCode()] = ['amount' => $firstFee];
293
        $channels[] = $firstChannel;
294
295
        if (null !== $secondFee) {
296
            $configuration[$secondChannel->getCode()] = ['amount' => $secondFee];
0 ignored issues
show
It seems like $secondChannel is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
297
            $channels[] = $secondChannel;
298
        }
299
300
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
301
            'name' => $shippingMethodName,
302
            'enabled' => true,
303
            'zone' => $this->getShippingZone(),
304
            'calculator' => [
305
                'type' => DefaultCalculators::PER_UNIT_RATE,
306
                'configuration' => $configuration,
307
            ],
308
            'channels' => $channels,
309
        ]));
310
    }
311
312
    /**
313
     * @Given /^the store has disabled "([^"]+)" shipping method with ("[^"]+") fee$/
314
     */
315
    public function storeHasDisabledShippingMethodWithFee($shippingMethodName, $fee)
316
    {
317
        $channel = $this->sharedStorage->get('channel');
318
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
319
320
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
321
            'name' => $shippingMethodName,
322
            'enabled' => false,
323
            'zone' => $this->getShippingZone(),
324
            'calculator' => [
325
                'type' => DefaultCalculators::FLAT_RATE,
326
                'configuration' => $configuration,
327
            ],
328
            'channels' => [$this->sharedStorage->get('channel')],
329
        ]));
330
    }
331
332
    /**
333
     * @Given /^the store has an archival "([^"]+)" shipping method with ("[^"]+") fee$/
334
     */
335
    public function theStoreHasArchivalShippingMethodWithFee($shippingMethodName, $fee)
336
    {
337
        $channel = $this->sharedStorage->get('channel');
338
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
339
340
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
341
            'name' => $shippingMethodName,
342
            'enabled' => true,
343
            'zone' => $this->getShippingZone(),
344
            'calculator' => [
345
                'type' => DefaultCalculators::FLAT_RATE,
346
                'configuration' => $configuration,
347
            ],
348
            'channels' => [$this->sharedStorage->get('channel')],
349
            'archived_at' => new \DateTime(),
350
        ]));
351
    }
352
353
    /**
354
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per unit$/
355
     */
356
    public function theStoreHasShippingMethodWithFeePerUnit($shippingMethodName, $fee)
357
    {
358
        $channel = $this->sharedStorage->get('channel');
359
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
360
361
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
362
            'name' => $shippingMethodName,
363
            'enabled' => true,
364
            'zone' => $this->getShippingZone(),
365
            'calculator' => [
366
                'type' => DefaultCalculators::PER_UNIT_RATE,
367
                'configuration' => $configuration,
368
            ],
369
            'channels' => [$this->sharedStorage->get('channel')],
370
        ]));
371
    }
372
373
    /**
374
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee not assigned to any channel$/
375
     */
376
    public function storeHasShippingMethodWithFeeNotAssignedToAnyChannel($shippingMethodName, $fee)
377
    {
378
        $channel = $this->sharedStorage->get('channel');
379
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
380
381
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
382
            'name' => $shippingMethodName,
383
            'enabled' => true,
384
            'zone' => $this->getShippingZone(),
385
            'calculator' => [
386
                'type' => DefaultCalculators::FLAT_RATE,
387
                'configuration' => $configuration,
388
            ],
389
            'channels' => [],
390
        ]));
391
    }
392
393
    /**
394
     * @Given /^(shipping method "[^"]+") belongs to ("[^"]+" tax category)$/
395
     */
396
    public function shippingMethodBelongsToTaxCategory(ShippingMethodInterface $shippingMethod, TaxCategoryInterface $taxCategory)
397
    {
398
        $shippingMethod->setTaxCategory($taxCategory);
399
        $this->shippingMethodManager->flush();
400
    }
401
402
    /**
403
     * @Given the shipping method :shippingMethod is enabled
404
     */
405
    public function theShippingMethodIsEnabled(ShippingMethodInterface $shippingMethod)
406
    {
407
        $shippingMethod->enable();
408
        $this->shippingMethodManager->flush();
409
    }
410
411
    /**
412
     * @Given the shipping method :shippingMethod is disabled
413
     */
414
    public function theShippingMethodIsDisabled(ShippingMethodInterface $shippingMethod)
415
    {
416
        $shippingMethod->disable();
417
        $this->shippingMethodManager->flush();
418
    }
419
420
    /**
421
     * @Given /^(this shipping method) requires at least one unit matches to ("([^"]+)" shipping category)$/
422
     */
423
    public function thisShippingMethodRequiresAtLeastOneUnitMatchToShippingCategory(
424
        ShippingMethodInterface $shippingMethod,
425
        ShippingCategoryInterface $shippingCategory
426
    ) {
427
        $shippingMethod->setCategory($shippingCategory);
428
        $shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ANY);
429
        $this->shippingMethodManager->flush();
430
    }
431
432
    /**
433
     * @Given /^(this shipping method) requires that all units match to ("([^"]+)" shipping category)$/
434
     */
435
    public function thisShippingMethodRequiresThatAllUnitsMatchToShippingCategory(
436
        ShippingMethodInterface $shippingMethod,
437
        ShippingCategoryInterface $shippingCategory
438
    ) {
439
        $shippingMethod->setCategory($shippingCategory);
440
        $shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ALL);
441
        $this->shippingMethodManager->flush();
442
    }
443
444
    /**
445
     * @Given /^(this shipping method) requires that no units match to ("([^"]+)" shipping category)$/
446
     */
447
    public function thisShippingMethodRequiresThatNoUnitsMatchToShippingCategory(
448
        ShippingMethodInterface $shippingMethod,
449
        ShippingCategoryInterface $shippingCategory
450
    ) {
451
        $shippingMethod->setCategory($shippingCategory);
452
        $shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_NONE);
453
        $this->shippingMethodManager->flush();
454
    }
455
456
    /**
457
     * @Given /^the (shipping method "[^"]+") is archival$/
458
     */
459
    public function theShippingMethodIsArchival(ShippingMethodInterface $shippingMethod)
460
    {
461
        $shippingMethod->setArchivedAt(new \DateTime());
462
        $this->shippingMethodManager->flush();
463
    }
464
465
    /**
466
     * @Given /^the shipping fee for ("[^"]+" shipping method) has been changed to ("[^"]+")$/
467
     */
468
    public function theShippingFeeForShippingMethodHasBeenChangedTo(ShippingMethodInterface $shippingMethod, $fee)
469
    {
470
        $channel = $this->sharedStorage->get('channel');
471
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
472
473
        $shippingMethod->setConfiguration($configuration);
474
475
        $this->shippingMethodManager->flush();
476
    }
477
478
    /**
479
     * @param array $channels
480
     * @param int $amount
481
     *
482
     * @return array
483
     */
484
    private function getConfigurationByChannels(array $channels, $amount = 0)
485
    {
486
        $configuration = [];
487
488
        /** @var ChannelInterface $channel */
489
        foreach ($channels as $channel) {
490
            $configuration[$channel->getCode()] = ['amount' => $amount];
491
        }
492
493
        return $configuration;
494
    }
495
496
    /**
497
     * @param ShippingMethodInterface $shippingMethod
498
     */
499
    private function saveShippingMethod(ShippingMethodInterface $shippingMethod)
500
    {
501
        $this->shippingMethodRepository->add($shippingMethod);
502
        $this->sharedStorage->set('shipping_method', $shippingMethod);
503
    }
504
505
    /**
506
     * @return ZoneInterface
507
     */
508
    private function getShippingZone()
509
    {
510
        if ($this->sharedStorage->has('shipping_zone')) {
511
            return  $this->sharedStorage->get('shipping_zone');
512
        }
513
514
        return $this->sharedStorage->get('zone');
515
    }
516
}
517