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

src/Sylius/Behat/Context/Setup/PaymentContext.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\Bundle\CoreBundle\Test\Services\PaymentMethodNameToGatewayConverterInterface;
18
use Sylius\Component\Core\Formatter\StringInflector;
19
use Sylius\Component\Core\Model\ChannelInterface;
20
use Sylius\Component\Core\Model\PaymentMethodInterface;
21
use Sylius\Component\Payment\Model\PaymentMethodTranslationInterface;
22
use Sylius\Component\Payment\Repository\PaymentMethodRepositoryInterface;
23
use Sylius\Component\Resource\Factory\FactoryInterface;
24
25
/**
26
 * @author Arkadiusz Krakowiak <[email protected]>
27
 * @author Łukasz Chruściel <[email protected]>
28
 * @author Grzegorz Sadowski <[email protected]>
29
 */
30
final class PaymentContext implements Context
31
{
32
    /**
33
     * @var SharedStorageInterface
34
     */
35
    private $sharedStorage;
36
37
    /**
38
     * @var PaymentMethodRepositoryInterface
39
     */
40
    private $paymentMethodRepository;
41
42
    /**
43
     * @var FactoryInterface
44
     */
45
    private $paymentMethodFactory;
46
47
    /**
48
     * @var FactoryInterface
49
     */
50
    private $paymentMethodTranslationFactory;
51
52
    /**
53
     * @var PaymentMethodNameToGatewayConverterInterface
54
     */
55
    private $paymentMethodNameToGatewayConverter;
56
57
    /**
58
     * @var ObjectManager
59
     */
60
    private $objectManager;
61
62
    /**
63
     * @param SharedStorageInterface $sharedStorage
64
     * @param PaymentMethodRepositoryInterface $paymentMethodRepository
65
     * @param FactoryInterface $paymentMethodFactory
66
     * @param FactoryInterface $paymentMethodTranslationFactory
67
     * @param PaymentMethodNameToGatewayConverterInterface $paymentMethodNameToGatewayConverter
68
     * @param ObjectManager $objectManager
69
     */
70
    public function __construct(
71
        SharedStorageInterface $sharedStorage,
72
        PaymentMethodRepositoryInterface $paymentMethodRepository,
73
        FactoryInterface $paymentMethodFactory,
74
        FactoryInterface $paymentMethodTranslationFactory,
75
        PaymentMethodNameToGatewayConverterInterface $paymentMethodNameToGatewayConverter,
76
        ObjectManager $objectManager
77
    ) {
78
        $this->sharedStorage = $sharedStorage;
79
        $this->paymentMethodRepository = $paymentMethodRepository;
80
        $this->paymentMethodFactory = $paymentMethodFactory;
81
        $this->paymentMethodTranslationFactory = $paymentMethodTranslationFactory;
82
        $this->paymentMethodNameToGatewayConverter = $paymentMethodNameToGatewayConverter;
83
        $this->objectManager = $objectManager;
84
    }
85
86
    /**
87
     * @Given the store (also )allows paying (with ):paymentMethodName
88
     * @Given the store (also )allows paying with :paymentMethodName at position :position
89
     */
90
    public function storeAllowsPaying($paymentMethodName, $position = null)
91
    {
92
        $this->createPaymentMethod($paymentMethodName, 'PM_'.$paymentMethodName, 'Payment method', true, $position);
93
    }
94
95
    /**
96
     * @Given /^the store allows paying (\w+) for (all channels)$/
97
     */
98
    public function storeAllowsPayingForAllChannels($paymentMethodName, array $channels)
99
    {
100
        $paymentMethod = $this->createPaymentMethod($paymentMethodName, StringInflector::nameToUppercaseCode($paymentMethodName), 'Payment method', false);
101
102
        foreach ($channels as $channel) {
103
            $paymentMethod->addChannel($channel);
104
        }
105
    }
106
107
    /**
108
     * @Given the store has a payment method :paymentMethodName with a code :paymentMethodCode
109
     */
110
    public function theStoreHasAPaymentMethodWithACode($paymentMethodName, $paymentMethodCode)
111
    {
112
        $this->createPaymentMethod($paymentMethodName, $paymentMethodCode);
113
    }
114
115
    /**
116
     * @Given /^(this payment method) is named "([^"]+)" in the "([^"]+)" locale$/
117
     */
118
    public function thisPaymentMethodIsNamedIn(PaymentMethodInterface $paymentMethod, $name, $locale)
119
    {
120
        /** @var PaymentMethodTranslationInterface $translation */
121
        $translation = $this->paymentMethodTranslationFactory->createNew();
122
        $translation->setLocale($locale);
123
        $translation->setName($name);
124
125
        $paymentMethod->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...
126
127
        $this->objectManager->flush();
128
    }
129
130
    /**
131
     * @Given the payment method :paymentMethod is disabled
132
     * @Given /^(this payment method) is disabled$/
133
     */
134
    public function theStoreHasAPaymentMethodDisabled(PaymentMethodInterface $paymentMethod)
135
    {
136
        $paymentMethod->disable();
137
138
        $this->objectManager->flush();
139
    }
140
141
    /**
142
     * @Given /^(it) has instructions "([^"]+)"$/
143
     */
144
    public function itHasInstructions(PaymentMethodInterface $paymentMethod, $instructions)
145
    {
146
        $paymentMethod->setInstructions($instructions);
147
148
        $this->objectManager->flush();
149
    }
150
151
    /**
152
     * @Given the store has :paymentMethodName payment method not assigned to any channel
153
     */
154
    public function theStoreHasPaymentMethodNotAssignedToAnyChannel($paymentMethodName)
155
    {
156
        $this->createPaymentMethod($paymentMethodName, 'PM_'.$paymentMethodName, 'Payment method', false);
157
    }
158
159
    /**
160
     * @param string $name
161
     * @param string $code
162
     * @param bool $addForCurrentChannel
163
     * @param string $description
164
     *
165
     * @return PaymentMethodInterface
166
     */
167
    private function createPaymentMethod($name, $code, $description = '', $addForCurrentChannel = true, $position = null)
168
    {
169
        /** @var PaymentMethodInterface $paymentMethod */
170
        $paymentMethod = $this->paymentMethodFactory->createNew();
171
        $paymentMethod->setName(ucfirst($name));
172
        $paymentMethod->setCode($code);
173
        $paymentMethod->setPosition($position);
174
        $paymentMethod->setGateway($this->paymentMethodNameToGatewayConverter->convert($name));
175
        $paymentMethod->setDescription($description);
176
177
        if ($addForCurrentChannel && $this->sharedStorage->has('channel')) {
178
            $paymentMethod->addChannel($this->sharedStorage->get('channel'));
179
        }
180
181
        $this->sharedStorage->set('payment_method', $paymentMethod);
182
        $this->paymentMethodRepository->add($paymentMethod);
183
184
        return $paymentMethod;
185
    }
186
}
187