Passed
Push — master ( 3e5289...db9510 )
by Pavel
02:34
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 string $orderId Payment ID in Merchant system
41
     * @param int $amount Payment amount
42
     * @param string $clientIp User IP address
43
     * @param string $url back URL
44
     * @param string $templateTag Used template tag. If empty string - no template tag will be passed
45
     * @param array $extra Payture none requirement extra fields
46
     *
47
     * @throws TransportException
48
     */
49 2
    public function init(
50
        SessionType $sessionType,
51
        string $orderId,
52
        string $product,
53
        int $amount,
54
        string $clientIp,
55
        string $url,
56
        string $templateTag = '',
57
        array $extra = []
58
    ): TerminalResponse {
59
        $data = [
60 2
            'SessionType' => self::mapSessionType($sessionType),
61 2
            'OrderId' => $orderId,
62 2
            'Amount' => $amount,
63 2
            'IP' => $clientIp,
64 2
            'Product' => $product,
65 2
            'Url' => $url,
66
        ];
67
68 2
        if ($templateTag !== '') {
69 2
            $data['TemplateTag'] = $templateTag;
70
        }
71
72 2
        if (\count($extra)) {
73 2
            $data = array_merge($data, $extra);
74
        }
75
76
        $urlParams = [
77 2
            'Key' => $this->config->getKey(),
78 2
            'Data' => http_build_query($data, '', ';'),
79
        ];
80
81 2
        return $this->sendRequest(PaytureOperation::INIT(), $urlParams);
82
    }
83
84
    /**
85
     * @see https://payture.com/api#inpay_charge_
86
     *
87
     * @param string $orderId Payment ID in Merchant system
88
     * @param int $amount Charging amount in kopecks
89
     *
90
     * @throws TransportException
91
     */
92 1
    public function charge(string $orderId, int $amount): TerminalResponse
93
    {
94
        $data = [
95 1
            'Key' => $this->config->getKey(),
96 1
            'Password' => $this->config->getPassword(),
97 1
            'OrderId' => $orderId,
98 1
            'Amount' => $amount,
99
        ];
100
101 1
        return $this->sendRequest(PaytureOperation::CHARGE(), $data);
102
    }
103
104
    /**
105
     * Perform partial or full amount unblock for Block session type.
106
     *
107
     * @see https://payture.com/api#inpay_unblock_
108
     *
109
     * @param string $orderId Payment ID in Merchant system
110
     * @param int $amount Amount in kopecks that is to be returned
111
     *
112
     * @throws TransportException
113
     */
114 1
    public function unblock(string $orderId, int $amount): TerminalResponse
115
    {
116
        $data = [
117 1
            'Key' => $this->config->getKey(),
118 1
            'Password' => $this->config->getPassword(),
119 1
            'OrderId' => $orderId,
120 1
            'Amount' => $amount,
121
        ];
122
123 1
        return $this->sendRequest(PaytureOperation::UNBLOCK(), $data);
124
    }
125
126
    /**
127
     * Create new deal to refund given Amount from the OrderId.
128
     *
129
     * @see https://payture.com/api#inpay_refund_
130
     *
131
     * @param string $orderId Payment ID in Merchant system
132
     * @param int $amount Amount in kopecks that is to be returned
133
     *
134
     * @throws TransportException
135
     */
136 1
    public function refund(string $orderId, int $amount): TerminalResponse
137
    {
138
        $data = [
139 1
            'Key' => $this->config->getKey(),
140 1
            'Password' => $this->config->getPassword(),
141 1
            'OrderId' => $orderId,
142 1
            'Amount' => $amount,
143
        ];
144
145 1
        return $this->sendRequest(PaytureOperation::REFUND(), $data);
146
    }
147
148
    /**
149
     * @deprecated
150
     * @see PaytureInPayTerminalInterface::getState()
151
     * @see https://payture.com/api#inpay_paystatus_
152
     *
153
     * @param string $orderId Payment ID in Merchant system
154
     *
155
     * @throws TransportException
156
     */
157 1
    public function payStatus(string $orderId): TerminalResponse
158
    {
159
        $data = [
160 1
            'Key' => $this->config->getKey(),
161 1
            'OrderId' => $orderId,
162
        ];
163
164 1
        return $this->sendRequest(PaytureOperation::PAY_STATUS(), $data);
165
    }
166
167
    /**
168
     * Returns actual order state.
169
     *
170
     * @see https://payture.com/api/#inpay_getstate_
171
     *
172
     * @param string $orderId Payment ID in Merchant system
173
     *
174
     * @throws TransportException
175
     */
176 1
    public function getState(string $orderId): TerminalResponse
177
    {
178
        $data = [
179 1
            'Key' => $this->config->getKey(),
180 1
            'OrderId' => $orderId,
181
        ];
182
183 1
        return $this->sendRequest(PaytureOperation::GET_STATE(), $data);
184
    }
185
186 1
    public function createPaymentUrl(string $sessionId): string
187
    {
188
        $data = [
189 1
            'SessionId' => $sessionId,
190
        ];
191
192 1
        return $this->config->buildOperationUrl(PaytureOperation::PAY(), self::API_PREFIX, $data);
193
    }
194
195
    /**
196
     * @throws TransportException
197
     */
198 7
    private function sendRequest(PaytureOperation $operation, array $parameters): TerminalResponse
199
    {
200 7
        $transportResponse = $this->transport->request($operation, self::API_PREFIX, $parameters);
201
202 7
        return TerminalResponseBuilder::parseTransportResponse($transportResponse, $operation);
203
    }
204
}
205