Completed
Push — symfony3-fqcn ( fc44dc...d46cdc )
by Kamil
34:08 queued 14:53
created

omNameAndCode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
nop 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\Test\Services\PaymentMethodNameToGatewayConverterInterface;
18
use Sylius\Component\Core\Model\ChannelInterface;
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 FactoryInterface
43
     */
44
    private $paymentMethodFactory;
45
46
    /**
47
     * @var FactoryInterface
48
     */
49
    private $paymentMethodTranslationFactory;
50
51
    /**
52
     * @var PaymentMethodNameToGatewayConverterInterface
53
     */
54
    private $paymentMethodNameToGatewayConverter;
55
56
    /**
57
     * @var ObjectManager
58
     */
59
    private $objectManager;
60
61
    /**
62
     * @param SharedStorageInterface $sharedStorage
63
     * @param PaymentMethodRepositoryInterface $paymentMethodRepository
64
     * @param FactoryInterface $paymentMethodFactory
65
     * @param FactoryInterface $paymentMethodTranslationFactory
66
     * @param PaymentMethodNameToGatewayConverterInterface $paymentMethodNameToGatewayConverter
67
     * @param ObjectManager $objectManager
68
     */
69
    public function __construct(
70
        SharedStorageInterface $sharedStorage,
71
        PaymentMethodRepositoryInterface $paymentMethodRepository,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $paymentMethodRepository exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
72
        FactoryInterface $paymentMethodFactory,
73
        FactoryInterface $paymentMethodTranslationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $paymentMethodTranslationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
74
        PaymentMethodNameToGatewayConverterInterface $paymentMethodNameToGatewayConverter,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $paymentMethodNameToGatewayConverter exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
75
        ObjectManager $objectManager
76
    ) {
77
        $this->sharedStorage = $sharedStorage;
78
        $this->paymentMethodRepository = $paymentMethodRepository;
79
        $this->paymentMethodFactory = $paymentMethodFactory;
80
        $this->paymentMethodTranslationFactory = $paymentMethodTranslationFactory;
81
        $this->paymentMethodNameToGatewayConverter = $paymentMethodNameToGatewayConverter;
82
        $this->objectManager = $objectManager;
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, 'Payment method', true, $position);
92
    }
93
94
    /**
95
     * @Given the store has a payment method :paymentMethodName with a code :paymentMethodCode
96
     */
97
    public function theStoreHasAPaymentMethodWithACode($paymentMethodName, $paymentMethodCode)
98
    {
99
        $this->createPaymentMethod($paymentMethodName, $paymentMethodCode);
100
    }
101
102
    /**
103
     * @Given /^(this payment method) is named "([^"]+)" in the "([^"]+)" locale$/
104
     */
105
    public function thisPaymentMethodIsNamedIn(PaymentMethodInterface $paymentMethod, $name, $locale)
106
    {
107
        /** @var PaymentMethodTranslationInterface $translation */
108
        $translation = $this->paymentMethodTranslationFactory->createNew();
109
        $translation->setLocale($locale);
110
        $translation->setName($name);
111
112
        $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...
113
114
        $this->objectManager->flush();
115
    }
116
117
    /**
118
     * @Given the payment method :paymentMethod is disabled
119
     * @Given /^(this payment method) is disabled$/
120
     */
121
    public function theStoreHasAPaymentMethodDisabled(PaymentMethodInterface $paymentMethod)
122
    {
123
        $paymentMethod->disable();
124
125
        $this->objectManager->flush();
126
    }
127
128
    /**
129
     * @Given /^(it) has instructions "([^"]+)"$/
130
     */
131
    public function itHasInstructions(PaymentMethodInterface $paymentMethod, $instructions)
132
    {
133
        $paymentMethod->setInstructions($instructions);
134
135
        $this->objectManager->flush();
136
    }
137
138
    /**
139
     * @Given the store has :paymentMethodName payment method not assigned to any channel
140
     */
141
    public function theStoreHasPaymentMethodNotAssignedToAnyChannel($paymentMethodName)
142
    {
143
        $this->createPaymentMethod($paymentMethodName, 'PM_'.$paymentMethodName, 'Payment method', false);
144
    }
145
146
    /**
147
     * @param string $name
148
     * @param string $code
149
     * @param bool $addForCurrentChannel
150
     * @param string $description
151
     */
152
    private function createPaymentMethod($name, $code, $description = '', $addForCurrentChannel = true, $position = null)
153
    {
154
        /** @var PaymentMethodInterface $paymentMethod */
155
        $paymentMethod = $this->paymentMethodFactory->createNew();
156
        $paymentMethod->setName(ucfirst($name));
157
        $paymentMethod->setCode($code);
158
        $paymentMethod->setPosition($position);
159
        $paymentMethod->setGateway($this->paymentMethodNameToGatewayConverter->convert($name));
160
        $paymentMethod->setDescription($description);
161
162
        if ($addForCurrentChannel && $this->sharedStorage->has('channel')) {
163
            $paymentMethod->addChannel($this->sharedStorage->get('channel'));
164
        }
165
166
        $this->sharedStorage->set('payment_method', $paymentMethod);
167
        $this->paymentMethodRepository->add($paymentMethod);
168
    }
169
}
170