Completed
Push — menu-builders ( aa4c7a...012b35 )
by Kamil
22:12
created

ShippingContext::createShippingMethod()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 6.8493
c 0
b 0
f 0
cc 8
eloc 35
nc 32
nop 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A ShippingContext::theShippingMethodIsDisabled() 0 5 1
A ShippingContext::thisShippingMethodRequiresAtLeastOneUnitMatchToShippingCategory() 0 8 1
A ShippingContext::thisShippingMethodRequiresThatAllUnitsMatchToShippingCategory() 0 8 1
A ShippingContext::thisShippingMethodRequiresThatNoUnitsMatchToShippingCategory() 0 8 1

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
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\Bundle\CoreBundle\Fixture\Factory\ShippingMethodExampleFactory;
18
use Sylius\Component\Addressing\Model\ZoneInterface;
19
use Sylius\Component\Core\Model\ChannelInterface;
20
use Sylius\Component\Core\Model\ShippingMethodInterface;
21
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
22
use Sylius\Component\Resource\Factory\FactoryInterface;
23
use Sylius\Component\Resource\Repository\RepositoryInterface;
24
use Sylius\Component\Shipping\Calculator\CalculatorInterface;
25
use Sylius\Component\Shipping\Calculator\DefaultCalculators;
26
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
27
use Sylius\Component\Shipping\Model\ShippingMethodTranslationInterface;
28
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
29
30
/**
31
 * @author Arkadiusz Krakowiak <[email protected]>
32
 */
33
final class ShippingContext implements Context
34
{
35
    /**
36
     * @var SharedStorageInterface
37
     */
38
    private $sharedStorage;
39
40
    /**
41
     * @var ShippingMethodRepositoryInterface
42
     */
43
    private $shippingMethodRepository;
44
45
    /**
46
     * @var RepositoryInterface
47
     */
48
    private $zoneRepository;
49
50
    /**
51
     * @var ShippingMethodExampleFactory
52
     */
53
    private $shippingMethodExampleFactory;
54
55
    /**
56
     * @var FactoryInterface
57
     */
58
    private $shippingMethodTranslationFactory;
59
60
    /**
61
     * @var ObjectManager
62
     */
63
    private $shippingMethodManager;
64
65
    /**
66
     * @param SharedStorageInterface $sharedStorage
67
     * @param ShippingMethodRepositoryInterface $shippingMethodRepository
68
     * @param RepositoryInterface $zoneRepository
69
     * @param ShippingMethodExampleFactory $shippingMethodExampleFactory
70
     * @param FactoryInterface $shippingMethodTranslationFactory
71
     * @param ObjectManager $shippingMethodManager
72
     */
73
    public function __construct(
74
        SharedStorageInterface $sharedStorage,
75
        ShippingMethodRepositoryInterface $shippingMethodRepository,
76
        RepositoryInterface $zoneRepository,
77
        ShippingMethodExampleFactory $shippingMethodExampleFactory,
78
        FactoryInterface $shippingMethodTranslationFactory,
79
        ObjectManager $shippingMethodManager
80
    ) {
81
        $this->sharedStorage = $sharedStorage;
82
        $this->shippingMethodRepository = $shippingMethodRepository;
83
        $this->zoneRepository = $zoneRepository;
84
        $this->shippingMethodExampleFactory = $shippingMethodExampleFactory;
85
        $this->shippingMethodTranslationFactory = $shippingMethodTranslationFactory;
86
        $this->shippingMethodManager = $shippingMethodManager;
87
    }
88
89
    /**
90
     * @Given the store ships everything for free within the :zone zone
91
     */
92
    public function storeShipsEverythingForFree(ZoneInterface $zone)
93
    {
94
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
95
            'name' => 'Free',
96
            'enabled' => true,
97
            'zone' => $zone,
98
            'calculator' => [
99
                'type' => DefaultCalculators::FLAT_RATE,
100
                'configuration' => $this->getConfigurationByChannels([$this->sharedStorage->get('channel')]),
101
            ],
102
        ]));
103
    }
104
105
    /**
106
     * @Given the store ships everywhere for free
107
     */
108
    public function theStoreShipsEverywhereForFree()
109
    {
110
        /** @var ZoneInterface $zone */
111
        foreach ($this->zoneRepository->findAll() as $zone) {
112
            $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
113
                'name' => 'Free',
114
                'code' => 'FREE-' . $zone->getCode(),
115
                'enabled' => true,
116
                'zone' => $zone,
117
                'calculator' => [
118
                    'type' => DefaultCalculators::FLAT_RATE,
119
                    'configuration' => $this->getConfigurationByChannels([$this->sharedStorage->get('channel')]),
120
                ],
121
            ]));
122
        }
123
    }
124
125
    /**
126
     * @Given /^the store ships everywhere for free for (all channels)$/
127
     */
128
    public function theStoreShipsEverywhereForFreeForAllChannels(array $channels)
129
    {
130
        foreach ($this->zoneRepository->findAll() as $zone) {
131
            $configuration = $this->getConfigurationByChannels($channels);
132
            $shippingMethod = $this->shippingMethodExampleFactory->create([
133
                'name' => 'Free',
134
                'enabled' => true,
135
                'zone' => $zone,
136
                'calculator' => [
137
                    'type' => DefaultCalculators::FLAT_RATE,
138
                    'configuration' => $configuration,
139
                ],
140
                'channels' => $channels,
141
            ]);
142
143
            $this->saveShippingMethod($shippingMethod);
144
        }
145
    }
146
147
    /**
148
     * @Given the store (also )allows shipping with :name
149
     */
150
    public function theStoreAllowsShippingMethodWithName($name)
151
    {
152
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create(['name' => $name, 'enabled' => true]));
153
    }
154
155
    /**
156
     * @Given the store (also )allows shipping with :name identified by :code
157
     */
158
    public function theStoreAllowsShippingMethodWithNameAndCode($name, $code)
159
    {
160
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
161
            'name' => $name,
162
            'enabled' => true,
163
            'code' => $code,
164
        ]));
165
    }
166
167
    /**
168
     * @Given the store (also )allows shipping with :name at position :position
169
     */
170
    public function theStoreAllowsShippingMethodWithNameAndPosition($name, $position)
171
    {
172
        $shippingMethod = $this->shippingMethodExampleFactory->create(['name' => $name, 'enabled' => true]);
173
174
        $shippingMethod->setPosition($position);
175
176
        $this->saveShippingMethod($shippingMethod);
177
    }
178
179
    /**
180
     * @Given /^(this shipping method) is named "([^"]+)" in the "([^"]+)" locale$/
181
     */
182
    public function thisShippingMethodIsNamedInLocale(ShippingMethodInterface $shippingMethod, $name, $locale)
183
    {
184
        /** @var ShippingMethodTranslationInterface $translation */
185
        $translation = $this->shippingMethodTranslationFactory->createNew();
186
        $translation->setLocale($locale);
187
        $translation->setName($name);
188
189
        $shippingMethod->addTranslation($translation);
0 ignored issues
show
Documentation introduced by
$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...
190
191
        $this->shippingMethodManager->flush();
192
    }
193
194
    /**
195
     * @Given the store allows shipping with :firstName and :secondName
196
     */
197
    public function theStoreAllowsShippingWithAnd(...$names)
198
    {
199
        foreach ($names as $name) {
200
            $this->theStoreAllowsShippingMethodWithName($name);
201
        }
202
    }
203
204
    /**
205
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee within the ("[^"]+" zone)$/
206
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee for the (rest of the world)$/
207
     */
208
    public function storeHasShippingMethodWithFeeAndZone($shippingMethodName, $fee, ZoneInterface $zone)
209
    {
210
        $channel = $this->sharedStorage->get('channel');
211
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
212
213
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
214
            'name' => $shippingMethodName,
215
            'enabled' => true,
216
            'zone' => $zone,
217
            'calculator' => [
218
                'type' => DefaultCalculators::FLAT_RATE,
219
                'configuration' => $configuration,
220
            ],
221
            'channels' => [$this->sharedStorage->get('channel')],
222
        ]));
223
    }
224
225
    /**
226
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee$/
227
     */
228
    public function storeHasShippingMethodWithFee($shippingMethodName, $fee)
229
    {
230
        $channel = $this->sharedStorage->get('channel');
231
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
232
233
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
234
            'name' => $shippingMethodName,
235
            'enabled' => true,
236
            'zone' => $this->sharedStorage->get('zone'),
237
            'calculator' => [
238
                'type' => DefaultCalculators::FLAT_RATE,
239
                'configuration' => $configuration,
240
            ],
241
            'channels' => [$this->sharedStorage->get('channel')],
242
        ]));
243
    }
244
245
    /**
246
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per shipment for ("[^"]+" channel) and ("[^"]+") for ("[^"]+" channel)$/
247
     */
248
    public function storeHasShippingMethodWithFeePerShipmentForChannels(
249
        $shippingMethodName,
250
        $firstFee,
251
        ChannelInterface $firstChannel,
252
        $secondFee,
253
        ChannelInterface $secondChannel
254
    ) {
255
        $configuration[$firstChannel->getCode()] = ['amount' => $firstFee];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$configuration was never initialized. Although not strictly required by PHP, it is generally a good practice to add $configuration = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
256
        $configuration[$secondChannel->getCode()] = ['amount' => $secondFee];
257
258
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
259
            'name' => $shippingMethodName,
260
            'enabled' => true,
261
            'calculator' => [
262
                'type' => DefaultCalculators::FLAT_RATE,
263
                'configuration' => $configuration,
264
            ],
265
            'channels' => [$firstChannel, $secondChannel],
266
        ]));
267
    }
268
269
    /**
270
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per unit for ("[^"]+" channel) and ("[^"]+") for ("[^"]+" channel)$/
271
     */
272
    public function storeHasShippingMethodWithFeePerUnitForChannels(
273
        $shippingMethodName,
274
        $firstFee,
275
        ChannelInterface $firstChannel,
276
        $secondFee,
277
        ChannelInterface $secondChannel
278
    ) {
279
        $configuration = [];
280
        $configuration[$firstChannel->getCode()] = ['amount' => $firstFee];
281
        $configuration[$secondChannel->getCode()] = ['amount' => $secondFee];
282
283
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
284
            'name' => $shippingMethodName,
285
            'enabled' => true,
286
            'calculator' => [
287
                'type' => DefaultCalculators::PER_UNIT_RATE,
288
                'configuration' => $configuration,
289
            ],
290
            'channels' => [$firstChannel, $secondChannel],
291
        ]));
292
    }
293
294
    /**
295
     * @Given /^the store has disabled "([^"]+)" shipping method with ("[^"]+") fee$/
296
     */
297
    public function storeHasDisabledShippingMethodWithFee($shippingMethodName, $fee)
298
    {
299
        $channel = $this->sharedStorage->get('channel');
300
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
301
302
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
303
            'name' => $shippingMethodName,
304
            'enabled' => false,
305
            'calculator' => [
306
                'type' => DefaultCalculators::FLAT_RATE,
307
                'configuration' => $configuration,
308
            ],
309
            'channels' => [$this->sharedStorage->get('channel')],
310
        ]));
311
    }
312
313
    /**
314
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per unit$/
315
     */
316
    public function theStoreHasShippingMethodWithFeePerUnit($shippingMethodName, $fee)
317
    {
318
        $channel = $this->sharedStorage->get('channel');
319
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
320
321
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
322
            'name' => $shippingMethodName,
323
            'enabled' => true,
324
            'calculator' => [
325
                'type' => DefaultCalculators::PER_UNIT_RATE,
326
                'configuration' => $configuration,
327
            ],
328
            'channels' => [$this->sharedStorage->get('channel')],
329
        ]));
330
    }
331
332
    /**
333
     * @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee not assigned to any channel$/
334
     */
335
    public function storeHasShippingMethodWithFeeNotAssignedToAnyChannel($shippingMethodName, $fee)
336
    {
337
        $channel = $this->sharedStorage->get('channel');
338
        $configuration = $this->getConfigurationByChannels([$channel], $fee);
339
340
341
        $this->saveShippingMethod($this->shippingMethodExampleFactory->create([
342
            'name' => $shippingMethodName,
343
            'enabled' => true,
344
            'calculator' => [
345
                'type' => DefaultCalculators::FLAT_RATE,
346
                'configuration' => $configuration,
347
            ],
348
            'channels' => [],
349
        ]));
350
    }
351
352
    /**
353
     * @Given /^(shipping method "[^"]+") belongs to ("[^"]+" tax category)$/
354
     */
355
    public function shippingMethodBelongsToTaxCategory(ShippingMethodInterface $shippingMethod, TaxCategoryInterface $taxCategory)
356
    {
357
        $shippingMethod->setTaxCategory($taxCategory);
358
        $this->shippingMethodManager->flush();
359
    }
360
361
    /**
362
     * @Given the shipping method :shippingMethod is enabled
363
     */
364
    public function theShippingMethodIsEnabled(ShippingMethodInterface $shippingMethod)
365
    {
366
        $shippingMethod->enable();
367
        $this->shippingMethodManager->flush();
368
    }
369
370
    /**
371
     * @Given the shipping method :shippingMethod is disabled
372
     */
373
    public function theShippingMethodIsDisabled(ShippingMethodInterface $shippingMethod)
374
    {
375
        $shippingMethod->disable();
376
        $this->shippingMethodManager->flush();
377
    }
378
379
    /**
380
     * @Given /^(this shipping method) requires at least one unit matches to ("([^"]+)" shipping category)$/
381
     */
382
    public function thisShippingMethodRequiresAtLeastOneUnitMatchToShippingCategory(
383
        ShippingMethodInterface $shippingMethod,
384
        ShippingCategoryInterface $shippingCategory
385
    ) {
386
        $shippingMethod->setCategory($shippingCategory);
387
        $shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ANY);
388
        $this->shippingMethodManager->flush();
389
    }
390
391
    /**
392
     * @Given /^(this shipping method) requires that all units match to ("([^"]+)" shipping category)$/
393
     */
394
    public function thisShippingMethodRequiresThatAllUnitsMatchToShippingCategory(
395
        ShippingMethodInterface $shippingMethod,
396
        ShippingCategoryInterface $shippingCategory
397
    ) {
398
        $shippingMethod->setCategory($shippingCategory);
399
        $shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_ALL);
400
        $this->shippingMethodManager->flush();
401
    }
402
403
    /**
404
     * @Given /^(this shipping method) requires that no units match to ("([^"]+)" shipping category)$/
405
     */
406
    public function thisShippingMethodRequiresThatNoUnitsMatchToShippingCategory(
407
        ShippingMethodInterface $shippingMethod,
408
        ShippingCategoryInterface $shippingCategory
409
    ) {
410
        $shippingMethod->setCategory($shippingCategory);
411
        $shippingMethod->setCategoryRequirement(ShippingMethodInterface::CATEGORY_REQUIREMENT_MATCH_NONE);
412
        $this->shippingMethodManager->flush();
413
    }
414
415
    /**
416
     * @param array $channels
417
     * @param int $amount
418
     *
419
     * @return array
420
     */
421
    private function getConfigurationByChannels(array $channels, $amount = 0)
422
    {
423
        $configuration = [];
424
425
        /** @var ChannelInterface $channel */
426
        foreach ($channels as $channel) {
427
            $configuration[$channel->getCode()] = ['amount' => $amount];
428
        }
429
430
        return $configuration;
431
    }
432
433
    /**
434
     * @param ShippingMethodInterface $shippingMethod
435
     */
436
    private function saveShippingMethod(ShippingMethodInterface $shippingMethod)
437
    {
438
        $this->shippingMethodRepository->add($shippingMethod);
439
        $this->sharedStorage->set('shipping_method', $shippingMethod);
440
    }
441
}
442