Completed
Push — master ( daa8e9...6018cf )
by Kirill
12:53
created

Client::getPemFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Sender;
12
use Psr\Http\Message\RequestInterface;
13
14
final class Client
15
{
16
    use HasSignature, HasSender;
17
18
    private $baseUri;
19
    private $terminalKey;
20
    private $pemFile;
21
22
    /**
23
     * @var array
24
     */
25
    private static $requestHeaders = [
26
        'Accept' => 'application/json',
27
        'Content-Type' => 'application/x-www-form-urlencoded',
28
    ];
29
30
    /**
31
     * Init a new instance.
32
     *
33
     * @param string $uri
34
     * @param string $terminalKey
35
     * @param string $pemFile
36
     * @param Sender $sender
37
     */
38
    public function __construct($uri, $terminalKey, $pemFile, Sender $sender)
39
    {
40
        $this->baseUri = rtrim($uri, '/');
41
        $this->terminalKey = $terminalKey;
42
        $this->pemFile = realpath($pemFile);
43
        $this->setSender($sender);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getPemFile()
50
    {
51
        return $this->pemFile;
52
    }
53
54
    /**
55
     * Init a new payment session.
56
     *
57
     * @param string $orderId Номер заказа в системе Продавца.
58
     * @param string $cardId  Идентификатор карты пополнения.
59
     * @param int    $amount  Сумма в копейках.
60
     * @param array  $extra   Массив дополнительных полей, которые могут быть переданы в этом запросе.
61
     *
62
     * @return Init
63
     * @throws HttpException
64
     */
65 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...
66
    {
67
        $extra['Amount'] = $amount;
68
        $extra['CardId'] = $cardId;
69
        $extra['OrderId'] = $orderId;
70
71
        return new Init($this->send($this->makeRequest('Init', $extra)));
72
    }
73
74
    /**
75
     * Make payout to card.
76
     *
77
     * @param mixed $paymentId
78
     *
79
     * @return Payment
80
     * @throws HttpException
81
     */
82
    public function payment($paymentId): Payment
83
    {
84
        return new Payment($this->send($this->makeRequest('Payment', ['PaymentId' => $paymentId])));
85
    }
86
87
    /**
88
     * Init a new payment session and make payout to card.
89
     *
90
     * @param string $orderId Номер заказа в системе Продавца.
91
     * @param string $cardId  Идентификатор карты пополнения.
92
     * @param int    $amount  Сумма в копейках.
93
     * @param array  $extra   Массив дополнительных полей, которые могут быть переданы в этом запросе.
94
     *
95
     * @return Payment
96
     * @throws HttpException
97
     * @throws ResponseException
98
     */
99 View Code Duplication
    public function payout(string $orderId, string $cardId, int $amount, array $extra = []): Payment
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...
100
    {
101
        $response = $this->init($orderId, $cardId, $amount, $extra);
102
        if ($response->hasError() || 'CHECKED' !== $response->getStatus()) {
103
            throw new ResponseException($response);
104
        }
105
106
        $response = $this->payment($response->getPaymentId());
107
        if ($response->hasError()) {
108
            throw new ResponseException($response);
109
        }
110
111
        return $response;
112
    }
113
114
    /**
115
     * Make a new http request.
116
     *
117
     * @param string $uri
118
     * @param array  $body
119
     *
120
     * @return RequestInterface
121
     */
122
    private function makeRequest(string $uri, array $body = []): RequestInterface
123
    {
124
        $body['TerminalKey'] = $this->terminalKey;
125
126
        $body['DigestValue'] = base64_encode($this->digest($body));
127
        $body['SignatureValue'] = base64_encode($this->sign($body));
128
        $body['X509SerialNumber'] = $this->getSerialNumber($this->pemFile);
0 ignored issues
show
Unused Code introduced by
The call to Client::getSerialNumber() has too many arguments starting with $this->pemFile.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
129
130
        return new Request('post', "$this->baseUri/$uri", self::$requestHeaders, http_build_query($body, '', '&'));
131
    }
132
}
133