Completed
Push — master ( 4e9462...2570d1 )
by Kamil
18:48
created

ShippingContext::generateCodeFromNameAndZone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
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\Addressing\Model\ZoneInterface;
17
use Sylius\Component\Addressing\Repository\ZoneRepositoryInterface;
18
use Sylius\Component\Core\Model\ShippingMethodInterface;
19
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
use Sylius\Component\Shipping\Calculator\DefaultCalculators;
23
use Sylius\Component\Shipping\Repository\ShippingMethodRepositoryInterface;
24
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
25
26
/**
27
 * @author Arkadiusz Krakowiak <[email protected]>
28
 */
29
final class ShippingContext implements Context
30
{
31
    /**
32
     * @var SharedStorageInterface
33
     */
34
    private $sharedStorage;
35
36
    /**
37
     * @var ShippingMethodRepositoryInterface
38
     */
39
    private $shippingMethodRepository;
40
41
    /**
42
     * @var ZoneRepositoryInterface
43
     */
44
    private $zoneRepository;
45
46
    /**
47
     * @var FactoryInterface
48
     */
49
    private $shippingMethodFactory;
50
51
    /**
52
     * @var ObjectManager
53
     */
54
    private $shippingMethodManager;
55
56
    /**
57
     * @param SharedStorageInterface $sharedStorage
58
     * @param ShippingMethodRepositoryInterface $shippingMethodRepository
59
     * @param ZoneRepositoryInterface $zoneRepository
60
     * @param FactoryInterface $shippingMethodFactory
61
     * @param ObjectManager $shippingMethodManager
62
     */
63
    public function __construct(
64
        SharedStorageInterface $sharedStorage,
65
        ShippingMethodRepositoryInterface $shippingMethodRepository,
66
        ZoneRepositoryInterface $zoneRepository,
67
        FactoryInterface $shippingMethodFactory,
68
        ObjectManager $shippingMethodManager
69
    ) {
70
        $this->sharedStorage = $sharedStorage;
71
        $this->shippingMethodRepository = $shippingMethodRepository;
72
        $this->zoneRepository = $zoneRepository;
73
        $this->shippingMethodFactory = $shippingMethodFactory;
74
        $this->shippingMethodManager = $shippingMethodManager;
75
    }
76
77
    /**
78
     * @Given the store ships everything for free within :zone zone
79
     * @Given /^the store ships everything for free for (the rest of the world)$/
80
     */
81
    public function storeShipsEverythingForFree(ZoneInterface $zone = null)
82
    {
83
        $this->createShippingMethod('Free', $zone);
84
    }
85
86
    /**
87
     * @Given /^the store ships everywhere for free$/
88
     */
89
    public function theStoreShipsEverywhereForFree()
90
    {
91
        foreach ($this->zoneRepository->findAll() as $zone) {
92
            $this->createShippingMethod('Free', $zone);
93
        }
94
    }
95
96
    /**
97
     * @Given /^the store allows shipping with "([^"]*)"$/
98
     */
99
    public function theStoreAllowsShippingMethod($shippingMethodName)
100
    {
101
        $this->createShippingMethod($shippingMethodName);
102
    }
103
104
    /**
105
     * @Given /^the store has "([^"]*)" shipping method with ("[^"]+") fee$/
106
     * @Given /^the store has "([^"]*)" shipping method with ("[^"]+") fee within ("([^"]*)" zone)$/
107
     * @Given /^the store has "([^"]*)" shipping method with ("[^"]+") fee for (the rest of the world)$/
108
     */
109
    public function storeHasShippingMethodWithFee($shippingMethodName, $fee, ZoneInterface $zone = null)
110
    {
111
        $this->createShippingMethod($shippingMethodName, $zone, 'en', ['amount' => $fee]);
112
    }
113
114
    /**
115
     * @Given /^(shipping method "[^"]+") belongs to ("[^"]+" tax category)$/
116
     */
117
    public function shippingMethodBelongsToTaxCategory(ShippingMethodInterface $shippingMethod, TaxCategoryInterface $taxCategory)
118
    {
119
        $shippingMethod->setTaxCategory($taxCategory);
120
        $this->shippingMethodManager->flush();
121
    }
122
123
    /**
124
     * @param string $name
125
     * @param ZoneInterface|null $zone
126
     * @param string $locale
127
     * @param array $configuration
128
     * @param string $calculator
129
     */
130
    private function createShippingMethod(
131
        $name,
132
        ZoneInterface $zone = null,
133
        $locale = 'en',
134
        $configuration = ['amount' => 0],
135
        $calculator = DefaultCalculators::FLAT_RATE
136
    ) {
137
        if (null === $zone) {
138
            $zone = $this->sharedStorage->get('zone');
139
        }
140
141
        /** @var ShippingMethodInterface $shippingMethod */
142
        $shippingMethod = $this->shippingMethodFactory->createNew();
143
        $shippingMethod->setCode($this->generateCodeFromNameAndZone($name, $zone->getCode()));
144
        $shippingMethod->setName($name);
145
        $shippingMethod->setCurrentLocale($locale);
146
        $shippingMethod->setConfiguration($configuration);
147
        $shippingMethod->setCalculator($calculator);
148
        $shippingMethod->setZone($zone);
149
150
        $this->shippingMethodRepository->add($shippingMethod);
151
    }
152
153
    /**
154
     * @param string $shippingMethodName
155
     * @param string|null $zoneCode
156
     *
157
     * @return string
158
     */
159
    private function generateCodeFromNameAndZone($shippingMethodName, $zoneCode = null)
160
    {
161
        return str_replace([' ', '-'], '_', strtolower($shippingMethodName)).'_'.strtolower($zoneCode);
162
    }
163
}
164