Completed
Push — master ( 91799e...396800 )
by Kirill
09:48
created

Client   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 132
Duplicated Lines 6.06 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 8
loc 132
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getPemFile() 0 4 1
A init() 8 8 1
A payment() 0 4 1
A getState() 0 4 1
B payout() 0 14 5
A makeRequest() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
namespace Personnage\Tinkoff\SDK\E2Card;
4
5
use GuzzleHttp\Psr7\Request;
6
use Personnage\Tinkoff\SDK\Exception\HttpException;
7
use Personnage\Tinkoff\SDK\Exception\ResponseException;
8
use Personnage\Tinkoff\SDK\HasSender;
9
use Personnage\Tinkoff\SDK\Response\Init;
10
use Personnage\Tinkoff\SDK\Response\Payment;
11
use Personnage\Tinkoff\SDK\Response\State;
12
use Personnage\Tinkoff\SDK\Sender;
13
use Psr\Http\Message\RequestInterface;
14
15
final class Client
16
{
17
    use HasSignature, HasSender;
18
19
    private $baseUri;
20
    private $terminalKey;
21
    private $pemFile;
22
23
    /**
24
     * @var array
25
     */
26
    private static $requestHeaders = [
27
        'Accept' => 'application/json',
28
        'Content-Type' => 'application/x-www-form-urlencoded',
29
    ];
30
31
    /**
32
     * Init a new instance.
33
     *
34
     * @param string $uri
35
     * @param string $terminalKey
36
     * @param string $pemFile
37
     * @param Sender $sender
38
     */
39
    public function __construct(string $uri, string $terminalKey, string $pemFile, Sender $sender)
40
    {
41
        $this->baseUri = rtrim($uri, '/');
42
        $this->terminalKey = $terminalKey;
43
        $this->pemFile = realpath($pemFile);
44
        $this->setSender($sender);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getPemFile(): string
51
    {
52
        return $this->pemFile;
53
    }
54
55
    /**
56
     * Init a new payment session.
57
     *
58
     * @param string $orderId Номер заказа в системе Продавца.
59
     * @param string $cardId  Идентификатор карты пополнения.
60
     * @param int    $amount  Сумма в копейках.
61
     * @param array  $extra   Массив дополнительных полей, которые могут быть переданы в этом запросе.
62
     *
63
     * @return Init
64
     * @throws HttpException
65
     */
66 View Code Duplication
    public function init(string $orderId, string $cardId, int $amount, array $extra = []): Init
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $extra['Amount'] = $amount;
69
        $extra['CardId'] = $cardId;
70
        $extra['OrderId'] = $orderId;
71
72
        return new Init($this->send($this->makeRequest('Init', $extra)));
73
    }
74
75
    /**
76
     * Make payout to card.
77
     *
78
     * @param mixed $paymentId
79
     *
80
     * @return Payment
81
     * @throws HttpException
82
     */
83
    public function payment($paymentId): Payment
84
    {
85
        return new Payment($this->send($this->makeRequest('Payment', ['PaymentId' => $paymentId])));
86
    }
87
88
    /**
89
     * Get op state.
90
     *
91
     * @param mixed $paymentId
92
     *
93
     * @return State
94
     * @throws HttpException
95
     */
96
    public function getState($paymentId): State
97
    {
98
        return new State($this->send($this->makeRequest('GetState', ['PaymentId' => $paymentId])));
99
    }
100
101
    /**
102
     * Init a new payment session and make payout to card.
103
     *
104
     * @param string $orderId Номер заказа в системе Продавца.
105
     * @param string $cardId  Идентификатор карты пополнения.
106
     * @param int    $amount  Сумма в копейках.
107
     * @param array  $extra   Массив дополнительных полей, которые могут быть переданы в этом запросе.
108
     *
109
     * @return Payment
110
     * @throws HttpException
111
     * @throws ResponseException
112
     */
113
    public function payout(string $orderId, string $cardId, int $amount, array $extra = []): Payment
114
    {
115
        $response = $this->init($orderId, $cardId, $amount, $extra);
116
        if ($response->hasError() || 'CHECKED' !== $response->getStatus()) {
117
            throw new ResponseException($response);
118
        }
119
120
        $response = $this->payment($response->getPaymentId());
121
        if ($response->hasError() || 'COMPLETED' !== $response->getStatus()) {
122
            throw new ResponseException($response);
123
        }
124
125
        return $response;
126
    }
127
128
    /**
129
     * Make a new http request.
130
     *
131
     * @param string $uri
132
     * @param array  $body
133
     *
134
     * @return RequestInterface
135
     */
136
    private function makeRequest(string $uri, array $body = []): RequestInterface
137
    {
138
        $body['TerminalKey'] = $this->terminalKey;
139
140
        $body['DigestValue'] = base64_encode($this->digest($body));
141
        $body['SignatureValue'] = base64_encode($this->sign($body));
142
        $body['X509SerialNumber'] = $this->getSerialNumber();
143
144
        return new Request('post', "$this->baseUri/$uri", self::$requestHeaders, http_build_query($body, '', '&'));
145
    }
146
}
147