Completed
Push — master ( bfd93b...a37554 )
by Kamil
15s
created

PaymentContext   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 9
dl 0
loc 189
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A storeAllowsPaying() 0 4 1
A storeAllowsPayingForAllChannels() 0 8 2
A theStoreHasAPaymentMethodWithACode() 0 4 1
A thisPaymentMethodIsNamedIn() 0 11 1
A theStoreHasPaymentMethodWithCodeAndPaypalExpressCheckoutGateway() 0 15 1
A theStoreHasAPaymentMethodDisabled() 0 6 1
A itHasInstructions() 0 6 1
A theStoreHasPaymentMethodNotAssignedToAnyChannel() 0 4 1
B createPaymentMethod() 0 30 4
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\ExampleFactoryInterface;
18
use Sylius\Component\Core\Formatter\StringInflector;
19
use Sylius\Component\Core\Model\PaymentMethodInterface;
20
use Sylius\Component\Payment\Model\PaymentMethodTranslationInterface;
21
use Sylius\Component\Payment\Repository\PaymentMethodRepositoryInterface;
22
use Sylius\Component\Resource\Factory\FactoryInterface;
23
24
/**
25
 * @author Arkadiusz Krakowiak <[email protected]>
26
 * @author Łukasz Chruściel <[email protected]>
27
 * @author Grzegorz Sadowski <[email protected]>
28
 */
29
final class PaymentContext implements Context
30
{
31
    /**
32
     * @var SharedStorageInterface
33
     */
34
    private $sharedStorage;
35
36
    /**
37
     * @var PaymentMethodRepositoryInterface
38
     */
39
    private $paymentMethodRepository;
40
41
    /**
42
     * @var ExampleFactoryInterface
43
     */
44
    private $paymentMethodExampleFactory;
45
46
    /**
47
     * @var FactoryInterface
48
     */
49
    private $paymentMethodTranslationFactory;
50
51
    /**
52
     * @var ObjectManager
53
     */
54
    private $paymentMethodManager;
55
56
    /**
57
     * @var array
58
     */
59
    private $gatewayFactories;
60
61
    /**
62
     * @param SharedStorageInterface $sharedStorage
63
     * @param PaymentMethodRepositoryInterface $paymentMethodRepository
64
     * @param ExampleFactoryInterface $paymentMethodExampleFactory
65
     * @param FactoryInterface $paymentMethodTranslationFactory
66
     * @param ObjectManager $paymentMethodManager
67
     * @param array $gatewayFactories
68
     */
69
    public function __construct(
70
        SharedStorageInterface $sharedStorage,
71
        PaymentMethodRepositoryInterface $paymentMethodRepository,
72
        ExampleFactoryInterface $paymentMethodExampleFactory,
73
        FactoryInterface $paymentMethodTranslationFactory,
74
        ObjectManager $paymentMethodManager,
75
        array $gatewayFactories
76
    ) {
77
        $this->sharedStorage = $sharedStorage;
78
        $this->paymentMethodRepository = $paymentMethodRepository;
79
        $this->paymentMethodExampleFactory = $paymentMethodExampleFactory;
80
        $this->paymentMethodTranslationFactory = $paymentMethodTranslationFactory;
81
        $this->paymentMethodManager = $paymentMethodManager;
82
        $this->gatewayFactories = $gatewayFactories;
83
    }
84
85
    /**
86
     * @Given the store (also )allows paying (with ):paymentMethodName
87
     * @Given the store (also )allows paying with :paymentMethodName at position :position
88
     */
89
    public function storeAllowsPaying($paymentMethodName, $position = null)
90
    {
91
        $this->createPaymentMethod($paymentMethodName, 'PM_'.$paymentMethodName, 'Offline', 'Payment method', true, $position);
92
    }
93
94
    /**
95
     * @Given /^the store allows paying (\w+) for (all channels)$/
96
     */
97
    public function storeAllowsPayingForAllChannels($paymentMethodName, array $channels)
98
    {
99
        $paymentMethod = $this->createPaymentMethod($paymentMethodName, StringInflector::nameToUppercaseCode($paymentMethodName), 'Offline', 'Payment method', false);
100
101
        foreach ($channels as $channel) {
102
            $paymentMethod->addChannel($channel);
103
        }
104
    }
105
106
    /**
107
     * @Given the store has a payment method :paymentMethodName with a code :paymentMethodCode
108
     */
109
    public function theStoreHasAPaymentMethodWithACode($paymentMethodName, $paymentMethodCode)
110
    {
111
        $this->createPaymentMethod($paymentMethodName, $paymentMethodCode, 'Offline');
112
    }
113
114
    /**
115
     * @Given the store has a payment method :paymentMethodName with a code :paymentMethodCode and Paypal Express Checkout gateway
116
     */
117
    public function theStoreHasPaymentMethodWithCodeAndPaypalExpressCheckoutGateway(
118
        $paymentMethodName,
119
        $paymentMethodCode
120
    ) {
121
        $paymentMethod = $this->createPaymentMethod($paymentMethodName, $paymentMethodCode, 'Paypal Express Checkout');
122
        $paymentMethod->getGatewayConfig()->setConfig([
123
            'username' => 'TEST',
124
            'password' => 'TEST',
125
            'signature' => 'TEST',
126
            'payum.http_client' => '@sylius.payum.http_client',
127
            'sandbox' => true,
128
        ]);
129
130
        $this->paymentMethodManager->flush();
131
    }
132
133
    /**
134
     * @Given /^(this payment method) is named "([^"]+)" in the "([^"]+)" locale$/
135
     */
136
    public function thisPaymentMethodIsNamedIn(PaymentMethodInterface $paymentMethod, $name, $locale)
137
    {
138
        /** @var PaymentMethodTranslationInterface $translation */
139
        $translation = $this->paymentMethodTranslationFactory->createNew();
140
        $translation->setLocale($locale);
141
        $translation->setName($name);
142
143
        $paymentMethod->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...
144
145
        $this->paymentMethodManager->flush();
146
    }
147
148
    /**
149
     * @Given the payment method :paymentMethod is disabled
150
     * @Given /^(this payment method) has been disabled$/
151
     */
152
    public function theStoreHasAPaymentMethodDisabled(PaymentMethodInterface $paymentMethod)
153
    {
154
        $paymentMethod->disable();
155
156
        $this->paymentMethodManager->flush();
157
    }
158
159
    /**
160
     * @Given /^(it) has instructions "([^"]+)"$/
161
     */
162
    public function itHasInstructions(PaymentMethodInterface $paymentMethod, $instructions)
163
    {
164
        $paymentMethod->setInstructions($instructions);
165
166
        $this->paymentMethodManager->flush();
167
    }
168
169
    /**
170
     * @Given the store has :paymentMethodName payment method not assigned to any channel
171
     */
172
    public function theStoreHasPaymentMethodNotAssignedToAnyChannel($paymentMethodName)
173
    {
174
        $this->createPaymentMethod($paymentMethodName, 'PM_'.$paymentMethodName, 'Payment method', 'Offline', false);
175
    }
176
177
    /**
178
     * @param string $name
179
     * @param string $code
180
     * @param string $gatewayFactory
181
     * @param string $description
182
     * @param bool $addForCurrentChannel
183
     * @param int|null $position
184
     *
185
     * @return PaymentMethodInterface
186
     */
187
    private function createPaymentMethod(
188
        $name,
189
        $code,
190
        $gatewayFactory = 'Offline',
191
        $description = '',
192
        $addForCurrentChannel = true,
193
        $position = null
194
    ) {
195
        $gatewayFactory = array_search($gatewayFactory, $this->gatewayFactories);
196
197
        /** @var PaymentMethodInterface $paymentMethod */
198
        $paymentMethod = $this->paymentMethodExampleFactory->create([
199
            'name' => ucfirst($name),
200
            'code' => $code,
201
            'description' => $description,
202
            'gatewayName' => $gatewayFactory,
203
            'gatewayFactory' => $gatewayFactory,
204
            'enabled' => true,
205
            'channels' => ($addForCurrentChannel && $this->sharedStorage->has('channel')) ? [$this->sharedStorage->get('channel')] : [],
206
        ]);
207
208
        if (null !== $position) {
209
            $paymentMethod->setPosition($position);
210
        }
211
212
        $this->sharedStorage->set('payment_method', $paymentMethod);
213
        $this->paymentMethodRepository->add($paymentMethod);
214
215
        return $paymentMethod;
216
    }
217
}
218