Completed
Push — master ( 37aba0...533498 )
by Paweł
09:21
created

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

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
namespace Sylius\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Behat\Service\SharedStorageInterface;
17
use Sylius\Component\Addressing\Model\ZoneInterface;
18
use Sylius\Component\Core\Formatter\StringInflector;
19
use Sylius\Component\Core\Model\ChannelInterface;
20
use Sylius\Component\Core\Model\ShippingMethodInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Sylius\Component\Resource\Repository\RepositoryInterface;
23
use Sylius\Component\Shipping\Calculator\DefaultCalculators;
24
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
25
use Sylius\Component\Shipping\Model\ShippingMethodTranslationInterface;
26
use Sylius\Component\Shipping\Repository\ShippingMethodRepositoryInterface;
27
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
28
29
/**
30
 * @author Arkadiusz Krakowiak <[email protected]>
31
 */
32
final class ShippingContext implements Context
33
{
34
    /**
35
     * @var SharedStorageInterface
36
     */
37
    private $sharedStorage;
38
39
    /**
40
     * @var ShippingMethodRepositoryInterface
41
     */
42
    private $shippingMethodRepository;
43
44
    /**
45
     * @var RepositoryInterface
46
     */
47
    private $zoneRepository;
48
49
    /**
50
     * @var FactoryInterface
51
     */
52
    private $shippingMethodFactory;
53
54
    /**
55
     * @var FactoryInterface
56
     */
57
    private $shippingMethodTranslationFactory;
58
59
    /**
60
     * @var ObjectManager
61
     */
62
    private $shippingMethodManager;
63
64
    /**
65
     * @param SharedStorageInterface $sharedStorage
66
     * @param ShippingMethodRepositoryInterface $shippingMethodRepository
67
     * @param RepositoryInterface $zoneRepository
68
     * @param FactoryInterface $shippingMethodFactory
69
     * @param FactoryInterface $shippingMethodTranslationFactory
70
     * @param ObjectManager $shippingMethodManager
71
     */
72
    public function __construct(
73
        SharedStorageInterface $sharedStorage,
74
        ShippingMethodRepositoryInterface $shippingMethodRepository,
75
        RepositoryInterface $zoneRepository,
76
        FactoryInterface $shippingMethodFactory,
77
        FactoryInterface $shippingMethodTranslationFactory,
78
        ObjectManager $shippingMethodManager
79
    ) {
80
        $this->sharedStorage = $sharedStorage;
81
        $this->shippingMethodRepository = $shippingMethodRepository;
82
        $this->zoneRepository = $zoneRepository;
83
        $this->shippingMethodFactory = $shippingMethodFactory;
84
        $this->shippingMethodTranslationFactory = $shippingMethodTranslationFactory;
85
        $this->shippingMethodManager = $shippingMethodManager;
86
    }
87
88
    /**
89
     * @Given the store ships everything for free within the :zone zone
90
     * @Given /^the store ships everything for free for the (rest of the world)$/
91
     */
92
    public function storeShipsEverythingForFree(ZoneInterface $zone = null)
93
    {
94
        $this->createShippingMethod('Free', null, null, $zone);
95
    }
96
97
    /**
98
     * @Given the store ships everywhere for free
99
     */
100
    public function theStoreShipsEverywhereForFree()
101
    {
102
        foreach ($this->zoneRepository->findAll() as $zone) {
103
            $this->createShippingMethod('Free', null, null, $zone);
104
        }
105
    }
106
107
    /**
108
     * @Given /^the store ships everywhere for free for (all channels)$/
109
     */
110
    public function theStoreShipsEverywhereForFreeForAllChannels(array $channels)
111
    {
112
        foreach ($this->zoneRepository->findAll() as $zone) {
113
            $configuration = $this->getConfigurationByChannels($channels);
114
            $this->createShippingMethod(
115
                'Free',
116
                null,
117
                null,
118
                $zone,
119
                'en',
120
                $configuration,
121
                DefaultCalculators::FLAT_RATE,
122
                true,
123
                false,
124
                $channels
125
            );
126
        }
127
    }
128
129
    /**
130
     * @Given the store (also) allows shipping with :name
131
     * @Given the store (also) allows shipping with :name identified by :code
132
     * @Given the store (also) allows shipping with :name at position :position
133
     */
134
    public function theStoreAllowsShippingMethod($name, $code = null, $position = null)
135
    {
136
        $this->createShippingMethod($name, $code, $position);
137
    }
138
139
    /**
140
     * @Given /^(this shipping method) is named "([^"]+)" in the "([^"]+)" locale$/
141
     */
142
    public function thisShippingMethodIsNamedInLocale(ShippingMethodInterface $shippingMethod, $name, $locale)
143
    {
144
        /** @var ShippingMethodTranslationInterface $translation */
145
        $translation = $this->shippingMethodTranslationFactory->createNew();
146
        $translation->setLocale($locale);
147
        $translation->setName($name);
148
149
        $shippingMethod->addTranslation($translation);
0 ignored issues
show
$translation is of type object<Sylius\Component\...odTranslationInterface>, but the function expects a object<Sylius\Component\...l\TranslationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150
151
        $this->shippingMethodManager->flush();
152
    }
153
154
    /**
155
     * @Given the store allows shipping with :firstName and :secondName
156
     */
157
    public function theStoreAllowsShippingWithAnd($firstName, $secondName)
158
    {
159
        $this->createShippingMethod($firstName);
160
        $this->createShippingMethod($secondName);
161
    }
162
163
    /**
164
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee$/
165
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee within the ("[^"]+" zone)$/
166
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee for the (rest of the world)$/
167
     */
168
    public function storeHasShippingMethodWithFee($shippingMethodName, $fee, ZoneInterface $zone = null)
169
    {
170
        $channel = $this->sharedStorage->get('channel');
171
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
172
173
        $this->createShippingMethod($shippingMethodName, null, null, $zone, 'en', $configuration);
174
    }
175
176
    /**
177
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per shipment for ("[^"]+" channel) and ("[^"]+") for ("[^"]+" channel)$/
178
     */
179
    public function storeHasShippingMethodWithFeePerShipmentForChannels(
180
        $shippingMethodName,
181
        $firstFee,
182
        ChannelInterface $firstChannel,
183
        $secondFee,
184
        ChannelInterface $secondChannel
185
    ) {
186
        $configuration = [];
187
        $configuration[$firstChannel->getCode()] = ['amount' => $firstFee];
188
        $configuration[$secondChannel->getCode()] = ['amount' => $secondFee];
189
190
        $this->createShippingMethod(
191
            $shippingMethodName,
192
            null,
193
            null,
194
            null,
195
            'en',
196
            $configuration,
197
            DefaultCalculators::FLAT_RATE,
198
            true,
199
            false,
200
            [$firstChannel, $secondChannel]
201
        );
202
    }
203
204
    /**
205
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per unit for ("[^"]+" channel) and ("[^"]+") for ("[^"]+" channel)$/
206
     */
207
    public function storeHasShippingMethodWithFeePerUnitForChannels(
208
        $shippingMethodName,
209
        $firstFee,
210
        ChannelInterface $firstChannel,
211
        $secondFee,
212
        ChannelInterface $secondChannel
213
    ) {
214
        $configuration = [];
215
        $configuration[$firstChannel->getCode()] = ['amount' => $firstFee];
216
        $configuration[$secondChannel->getCode()] = ['amount' => $secondFee];
217
218
        $this->createShippingMethod(
219
            $shippingMethodName,
220
            null,
221
            null,
222
            null,
223
            'en',
224
            $configuration,
225
            DefaultCalculators::PER_UNIT_RATE,
226
            true,
227
            false,
228
            [$firstChannel, $secondChannel]
229
        );
230
    }
231
232
    /**
233
     * @Given /^the store has disabled "([^"]+)" shipping method with ("[^"]+") fee$/
234
     */
235
    public function storeHasDisabledShippingMethodWithFee($shippingMethodName, $fee)
236
    {
237
        $channel = $this->sharedStorage->get('channel');
238
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
239
240
        $this->createShippingMethod(
241
            $shippingMethodName,
242
            null,
243
            null,
244
            null,
245
            'en',
246
            $configuration,
247
            DefaultCalculators::FLAT_RATE,
248
            false
249
        );
250
    }
251
252
    /**
253
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per unit$/
254
     */
255
    public function theStoreHasShippingMethodWithFeePerUnit($shippingMethodName, $fee)
256
    {
257
        $channel = $this->sharedStorage->get('channel');
258
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
259
260
        $this->createShippingMethod(
261
            $shippingMethodName,
262
            null,
263
            null,
264
            null,
265
            'en',
266
            $configuration,
267
            DefaultCalculators::PER_UNIT_RATE
268
        );
269
    }
270
271
    /**
272
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee not assigned to any channel$/
273
     */
274
    public function storeHasShippingMethodWithFeeNotAssignedToAnyChannel($shippingMethodName, $fee)
275
    {
276
        $channel = $this->sharedStorage->get('channel');
277
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
278
279
        $this->createShippingMethod(
280
            $shippingMethodName,
281
            null,
282
            null,
283
            null,
284
            'en',
285
            $configuration,
286
            DefaultCalculators::FLAT_RATE,
287
            false,
288
            false
289
        );
290
    }
291
292
    /**
293
     * @Given /^(shipping method "[^"]+") belongs to ("[^"]+" tax category)$/
294
     */
295
    public function shippingMethodBelongsToTaxCategory(ShippingMethodInterface $shippingMethod, TaxCategoryInterface $taxCategory)
296
    {
297
        $shippingMethod->setTaxCategory($taxCategory);
298
        $this->shippingMethodManager->flush();
299
    }
300
301
    /**
302
     * @Given the shipping method :shippingMethod is enabled
303
     */
304
    public function theShippingMethodIsEnabled(ShippingMethodInterface $shippingMethod)
305
    {
306
        $shippingMethod->enable();
307
        $this->shippingMethodManager->flush();
308
    }
309
310
    /**
311
     * @Given the shipping method :shippingMethod is disabled
312
     */
313
    public function theShippingMethodIsDisabled(ShippingMethodInterface $shippingMethod)
314
    {
315
        $shippingMethod->disable();
316
        $this->shippingMethodManager->flush();
317
    }
318
319
    /**
320
     * @Given /^(this shipping method) requires at least one unit matches to ("([^"]+)" shipping category)$/
321
     */
322
    public function thisShippingMethodRequiresAtLeastOneUnitMatchToShippingCategory(
323
        ShippingMethodInterface $shippingMethod, 
324
        ShippingCategoryInterface $shippingCategory
325
    ) {
326
        $shippingMethod->setCategory($shippingCategory);
327
        $shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ANY);
328
        $this->shippingMethodManager->flush();
329
    }
330
331
    /**
332
     * @Given /^(this shipping method) requires that all units match to ("([^"]+)" shipping category)$/
333
     */
334
    public function thisShippingMethodRequiresThatAllUnitsMatchToShippingCategory(
335
        ShippingMethodInterface $shippingMethod,
336
        ShippingCategoryInterface $shippingCategory
337
    ) {
338
        $shippingMethod->setCategory($shippingCategory);
339
        $shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ALL);
340
        $this->shippingMethodManager->flush();
341
    }
342
343
    /**
344
     * @Given /^(this shipping method) requires that no units match to ("([^"]+)" shipping category)$/
345
     */
346
    public function thisShippingMethodRequiresThatNoUnitsMatchToShippingCategory(
347
        ShippingMethodInterface $shippingMethod,
348
        ShippingCategoryInterface $shippingCategory
349
    ) {
350
        $shippingMethod->setCategory($shippingCategory);
351
        $shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_NONE);
352
        $this->shippingMethodManager->flush();
353
    }
354
355
    /**
356
     * @param string $name
357
     * @param string|null $code
358
     * @param int|null $position
359
     * @param ZoneInterface|null $zone
360
     * @param string $locale
361
     * @param array $configuration
362
     * @param string $calculator
363
     * @param bool $enabled
364
     * @param bool $addForCurrentChannel
365
     * @param array $channels
366
     *
367
     * @return ShippingMethodInterface
368
     */
369
    private function createShippingMethod(
370
        $name,
371
        $code = null,
372
        $position = null,
373
        ZoneInterface $zone = null,
374
        $locale = 'en',
375
        array $configuration = [],
376
        $calculator = DefaultCalculators::FLAT_RATE,
377
        $enabled = true,
378
        $addForCurrentChannel = true,
379
        array $channels = []
380
    ) {
381
        $channel = $this->sharedStorage->get('channel');
382
383
        if (null === $zone) {
384
            $zone = $this->sharedStorage->get('zone');
385
        }
386
387
        if (null === $code) {
388
            $code = $this->generateCodeFromNameAndZone($name, $zone->getCode());
389
        }
390
391
        if (empty($configuration)) {
392
            $configuration = $this->getConfigurationByChannels([$channel]);
393
        }
394
395
        /** @var ShippingMethodInterface $shippingMethod */
396
        $shippingMethod = $this->shippingMethodFactory->createNew();
397
        $shippingMethod->setCode($code);
398
        $shippingMethod->setName($name);
399
        $shippingMethod->setPosition($position);
400
        $shippingMethod->setCurrentLocale($locale);
401
        $shippingMethod->setConfiguration($configuration);
402
        $shippingMethod->setCalculator($calculator);
403
        $shippingMethod->setZone($zone);
404
        $shippingMethod->setEnabled($enabled);
405
406
        if ($addForCurrentChannel && $channel) {
407
            $shippingMethod->addChannel($channel);
408
        }
409
410
        if (!$addForCurrentChannel) {
411
            foreach ($channels as $channel) {
412
                $shippingMethod->addChannel($channel);
413
            }
414
        }
415
416
        $this->shippingMethodRepository->add($shippingMethod);
417
        $this->sharedStorage->set('shipping_method', $shippingMethod);
418
419
        return $shippingMethod;
420
    }
421
422
    /**
423
     * @param string $shippingMethodName
424
     * @param string|null $zoneCode
425
     *
426
     * @return string
427
     */
428
    private function generateCodeFromNameAndZone($shippingMethodName, $zoneCode = null)
429
    {
430
        return StringInflector::nameToLowercaseCode($shippingMethodName).'_'.StringInflector::nameToLowercaseCode($zoneCode);
431
    }
432
433
    /**
434
     * @param array $channels
435
     * @param int $amount
436
     *
437
     * @return array
438
     */
439
    private function getConfigurationByChannels(array $channels, $amount = 0)
440
    {
441
        $configuration = [];
442
443
        /** @var ChannelInterface $channel */
444
        foreach ($channels as $channel) {
445
            $configuration[$channel->getCode()] = ['amount' => $amount];
446
        }
447
448
        return $configuration;
449
    }
450
}
451