Completed
Push — master ( 749f69...94328c )
by Kamil
27:45
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 Sylius\Component\Addressing\Model\ZoneInterface;
16
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
17
use Sylius\Component\Resource\Factory\FactoryInterface;
18
use Sylius\Component\Resource\Repository\RepositoryInterface;
19
use Sylius\Component\Shipping\Calculator\DefaultCalculators;
20
21
/**
22
 * @author Arkadiusz Krakowiak <[email protected]>
23
 */
24
class ShippingContext implements Context
25
{
26
    /**
27
     * @var RepositoryInterface
28
     */
29
    private $shippingMethodRepository;
30
31
    /**
32
     * @var FactoryInterface
33
     */
34
    private $shippingMethodFactory;
35
36
    /**
37
     * @var SharedStorageInterface
38
     */
39
    private $sharedStorage;
40
41
    /**
42
     * @param RepositoryInterface $shippingMethodRepository
43
     * @param FactoryInterface $shippingMethodFactory
44
     * @param SharedStorageInterface $sharedStorage
45
     */
46
    public function __construct(
47
        RepositoryInterface $shippingMethodRepository,
48
        FactoryInterface $shippingMethodFactory,
49
        SharedStorageInterface $sharedStorage
50
    ) {
51
        $this->shippingMethodRepository = $shippingMethodRepository;
52
        $this->shippingMethodFactory = $shippingMethodFactory;
53
        $this->sharedStorage = $sharedStorage;
54
    }
55
56
    /**
57
     * @Transform :shippingMethodName shipping method
58
     * @Transform shipping method :shippingMethodName
59
     */
60
    public function getShippingMethodByName($shippingMethodName)
61
    {
62
        $shippingMethod = $this->shippingMethodRepository->findOneBy(['name' => $shippingMethodName]);
63
        if (null === $shippingMethod) {
64
            throw new \Exception('Shipping method with name "'.$shippingMethodName.'" does not exist');
65
        }
66
67
        return $shippingMethod;
68
    }
69
70
    /**
71
     * @Given store ships everything for free
72
     */
73
    public function storeShipsEverythingForFree()
74
    {
75
        $this->createShippingMethod('Free');
76
    }
77
78
    /**
79
     * @Given /^store has "([^"]*)" shipping method with "(?:€|£|\$)([^"]*)" fee$/
80
     */
81
    public function storeHasShippingMethodWithFee($shippingMethodName, $fee)
82
    {
83
        $this->createShippingMethod($shippingMethodName, 'en', ['amount' => $this->getFeeFromString($fee)]);
84
    }
85
86
    /**
87
     * @param string $name
88
     * @param string $locale
89
     * @param array $configuration
90
     * @param string $calculator
91
     * @param ZoneInterface|null $zone
92
     */
93
    private function createShippingMethod(
94
        $name,
95
        $locale = 'en',
96
        $configuration = ['amount' => 0],
97
        $calculator = DefaultCalculators::FLAT_RATE,
98
        $zone = null
99
    ) {
100
        if (null === $zone) {
101
            $zone = $this->sharedStorage->getCurrentResource('zone');
102
        }
103
104
        $shippingMethod = $this->shippingMethodFactory->createNew();
105
        $shippingMethod->setCode($this->getCodeFromName($name));
106
        $shippingMethod->setName($name);
107
        $shippingMethod->setCurrentLocale($locale);
108
        $shippingMethod->setConfiguration($configuration);
109
        $shippingMethod->setCalculator($calculator);
110
        $shippingMethod->setZone($zone);
111
112
        $this->shippingMethodRepository->add($shippingMethod);
113
    }
114
115
    /**
116
     * @param string $shippingMethodName
117
     *
118
     * @return string
119
     */
120
    private function getCodeFromName($shippingMethodName)
121
    {
122
        return str_replace(' ', '_', strtolower($shippingMethodName));
123
    }
124
125
    /**
126
     * @param string $shippingMethodFee
127
     *
128
     * @return string
129
     */
130
    private function getFeeFromString($shippingMethodFee)
131
    {
132
        return ((int) $shippingMethodFee) * 100;
133
    }
134
}
135