Completed
Push — master ( 094334...0b6e60 )
by Kamil
17:47
created

ShippingContext::getShippingMethodByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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