Completed
Pull Request — master (#4)
by
unknown
02:26
created

PaytureInPayTerminal::init()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
ccs 14
cts 14
cp 1
cc 3
nc 4
nop 8
crap 3

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Lamoda\Payture\InPayClient;
4
5
use Lamoda\Payture\InPayClient\Exception\TransportException;
6
7
final class PaytureInPayTerminal implements PaytureInPayTerminalInterface
8
{
9
    private const API_PREFIX = 'apim';
10
11
    /** @var TerminalConfiguration */
12
    private $config;
13
14
    /** @var TransportInterface */
15
    private $transport;
16
17 8
    public function __construct(TerminalConfiguration $config, TransportInterface $transport)
18
    {
19 8
        $this->config = $config;
20 8
        $this->transport = $transport;
21 8
    }
22
23 2
    private static function mapSessionType(SessionType $sessionType): string
24
    {
25 2
        switch ((string) $sessionType) {
26 2
            case (string) SessionType::PAY():
27 1
                return 'Pay';
28 1
            case (string) SessionType::BLOCK():
29 1
                return 'Block';
30
        }
31
32
        // @codeCoverageIgnoreStart
33
        throw new \LogicException('Unknown session type');
34
        // @codeCoverageIgnoreEnd
35
    }
36
37
    /**
38
     * @see https://payture.com/api#inpay_init_
39
     *
40
     * @param SessionType $sessionType
41
     * @param string $orderId Payment ID in Merchant system
42
     * @param string $product
43
     * @param int $amount Payment amount
44
     * @param string $clientIp User IP address
45
     * @param string $url back URL
46
     * @param string $templateTag Used template tag. If empty string - no template tag will be passed
47
     * @param array $extra Payture none requirement extra fields
48
     *
49
     * @return TerminalResponse
50
     *
51
     * @throws TransportException
52
     */
53 2
    public function init(
54
        SessionType $sessionType,
55
        string $orderId,
56
        string $product,
57
        int $amount,
58
        string $clientIp,
59
        string $url,
60
        string $templateTag = '',
61
        array $extra = []
62
    ): TerminalResponse {
63
        $data = [
64 2
            'SessionType' => self::mapSessionType($sessionType),
65 2
            'OrderId' => $orderId,
66 2
            'Amount' => $amount,
67 2
            'IP' => $clientIp,
68 2
            'Product' => $product,
69 2
            'Url' => $url,
70
        ];
71
72 2
        if ($templateTag !== '') {
73 2
            $data['TemplateTag'] = $templateTag;
74
        }
75
76 2
        if (\count($extra)) {
77 2
            $data = array_merge($data, $extra);
78
        }
79
80
        $urlParams = [
81 2
            'Key' => $this->config->getKey(),
82 2
            'Data' => http_build_query($data, '', ';'),
83
        ];
84
85 2
        return $this->sendRequest(PaytureOperation::INIT(), $urlParams);
86
    }
87
88
    /**
89
     * @see https://payture.com/api#inpay_charge_
90
     *
91
     * @param string $orderId Payment ID in Merchant system
92
     * @param int $amount Charging amount in kopecks
93
     *
94
     * @return TerminalResponse
95
     *
96
     * @throws TransportException
97
     */
98 1
    public function charge(string $orderId, int $amount): TerminalResponse
99
    {
100
        $data = [
101 1
            'Key' => $this->config->getKey(),
102 1
            'Password' => $this->config->getPassword(),
103 1
            'OrderId' => $orderId,
104 1
            'Amount' => $amount,
105
        ];
106
107 1
        return $this->sendRequest(PaytureOperation::CHARGE(), $data);
108
    }
109
110
    /**
111
     * Perform partial or full amount unblock for Block session type.
112
     *
113
     * @see https://payture.com/api#inpay_unblock_
114
     *
115
     * @param string $orderId Payment ID in Merchant system
116
     * @param int $amount Amount in kopecks that is to be returned
117
     *
118
     * @return TerminalResponse
119
     *
120
     * @throws TransportException
121
     */
122 1
    public function unblock(string $orderId, int $amount): TerminalResponse
123
    {
124
        $data = [
125 1
            'Key' => $this->config->getKey(),
126 1
            'Password' => $this->config->getPassword(),
127 1
            'OrderId' => $orderId,
128 1
            'Amount' => $amount,
129
        ];
130
131 1
        return $this->sendRequest(PaytureOperation::UNBLOCK(), $data);
132
    }
133
134
    /**
135
     * Create new deal to refund given Amount from the OrderId.
136
     *
137
     * @see https://payture.com/api#inpay_refund_
138
     *
139
     * @param string $orderId Payment ID in Merchant system
140
     * @param int $amount Amount in kopecks that is to be returned
141
     *
142
     * @return TerminalResponse
143
     *
144
     * @throws TransportException
145
     */
146 1
    public function refund(string $orderId, int $amount): TerminalResponse
147
    {
148
        $data = [
149 1
            'Key' => $this->config->getKey(),
150 1
            'Password' => $this->config->getPassword(),
151 1
            'OrderId' => $orderId,
152 1
            'Amount' => $amount,
153
        ];
154
155 1
        return $this->sendRequest(PaytureOperation::REFUND(), $data);
156
    }
157
158
    /**
159
     * @deprecated
160
     * @see PaytureInPayTerminalInterface::getState()
161
     *
162
     * @see https://payture.com/api#inpay_paystatus_
163
     *
164
     * @param string $orderId Payment ID in Merchant system
165
     *
166
     * @return TerminalResponse
167
     *
168
     * @throws TransportException
169
     */
170 1
    public function payStatus(string $orderId): TerminalResponse
171
    {
172
        $data = [
173 1
            'Key' => $this->config->getKey(),
174 1
            'OrderId' => $orderId,
175
        ];
176
177 1
        return $this->sendRequest(PaytureOperation::PAY_STATUS(), $data);
178
    }
179
180
    /**
181
     * Returns actual order state
182
     *
183
     * @see https://payture.com/api/#inpay_getstate_
184
     *
185
     * @param string $orderId Payment ID in Merchant system
186
     *
187
     * @return TerminalResponse
188
     *
189
     * @throws TransportException
190
     */
191 1
    public function getState(string $orderId): TerminalResponse
192
    {
193
        $data = [
194 1
            'Key' => $this->config->getKey(),
195 1
            'OrderId' => $orderId,
196
        ];
197
198 1
        return $this->sendRequest(PaytureOperation::GET_STATE(), $data);
199
    }
200
201 1
    public function createPaymentUrl(string $sessionId): string
202
    {
203
        $data = [
204 1
            'SessionId' => $sessionId,
205
        ];
206
207 1
        return $this->config->buildOperationUrl(PaytureOperation::PAY(), self::API_PREFIX, $data);
208
    }
209
210
    /**
211
     * @param PaytureOperation $operation
212
     * @param array $parameters
213
     *
214
     * @return TerminalResponse
215
     *
216
     * @throws TransportException
217
     */
218 7
    private function sendRequest(PaytureOperation $operation, array $parameters): TerminalResponse
219
    {
220 7
        $transportResponse = $this->transport->request($operation, self::API_PREFIX, $parameters);
221
222 7
        return TerminalResponseBuilder::parseTransportResponse($transportResponse, $operation);
223
    }
224
}
225