Passed
Push — main ( 109aa6...e5ced1 )
by Evgenii
01:32
created

MindBoxClient::setHttpTimeOut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
4
namespace floor12\MindBox;
5
6
7
use floor12\MindBox\Exceptions\EmptyApiEndPointException;
8
use floor12\MindBox\Exceptions\EmptyApiKeyException;
9
use GuzzleHttp\Client;
10
use GuzzleHttp\ClientInterface;
11
use GuzzleHttp\Exception\GuzzleException;
12
use GuzzleHttp\Psr7\Request;
13
use Psr\Http\Message\ResponseInterface;
14
use function GuzzleHttp\Psr7\build_query;
15
16
class MindBoxClient
17
{
18
    const MODE_ASYNCHRONOUS = 0;
19
    const MODE_SYNCHRONOUS = 1;
20
    const ASYNC_MINDBOX_API_URL = 'https://api.mindbox.ru/v3/operations/async';
21
    const SYNC_MINDBOX_API_URL = 'https://api.mindbox.ru/v3/operations/sync';
22
    const DEFAULT_HTTP_TIMEOUT = 2;
23
24
    /** @var string */
25
    private $endpointId;
26
    /** @var ClientInterface */
27
    private $client;
28
    /** @var string[] */
29
    private $headers;
30
    /** @var string */
31
    private $secretKey;
32
    /** @var ResponseInterface */
33
    private $response;
34
    /** @var Request */
35
    private $httpRequest;
36
    /** @var int */
37
    private $httpTimeOut = self::DEFAULT_HTTP_TIMEOUT;
38
39
    /**
40
     * @param string $secretKey
41
     * @param string $endpointId
42
     * @param ClientInterface|null $client
43
     * @throws EmptyApiEndPointException
44
     * @throws EmptyApiKeyException
45
     */
46 3
    public function __construct(
47
        string $secretKey,
48
        string $endpointId,
49
        ClientInterface $client = null)
50
    {
51 3
        $this->client = $client ?? new Client();
52
53 3
        $this->secretKey = $secretKey;
54 3
        $this->endpointId = $endpointId;
55
56 3
        if (empty($this->secretKey))
57 1
            throw new EmptyApiKeyException();
58
59 2
        if (empty($this->endpointId))
60 1
            throw new EmptyApiEndPointException();
61
62 1
        $this->headers = [
63 1
            'Content-Type' => 'application/json; charset=utf-8',
64 1
            'Accept' => 'application/json',
65 1
            'Authorization' => "Mindbox secretKey=\"{$this->secretKey}\""
66
        ];
67 1
    }
68
69
    /**
70
     * @param ClientInterface $client
71
     */
72
    public function setClient(ClientInterface $client): void
73
    {
74
        $this->client = $client;
75
    }
76
77
    /**
78
     * @param MindBoxRequest $mindBoxRequest
79
     * @throws GuzzleException
80
     */
81 1
    public function sendData(MindBoxRequest $mindBoxRequest): void
82
    {
83
        $httpRequestParams = [
84 1
            'endpointId' => $this->endpointId,
85 1
            'operation' => $mindBoxRequest->getOperationName()
86
        ];
87
88
89 1
        if ($mindBoxRequest->getDeviceUUID()) {
90 1
            $httpRequestParams['deviceUUID'] = $mindBoxRequest->getDeviceUUID();
91
        }
92
93 1
        $baseUrl = $mindBoxRequest->isAsync() ? self::ASYNC_MINDBOX_API_URL : self::SYNC_MINDBOX_API_URL;
94
95 1
        $this->httpRequest = new Request(
96 1
            'POST',
97 1
            $baseUrl . '?' . build_query($httpRequestParams),
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Psr7\build_query() has been deprecated: build_query will be removed in guzzlehttp/psr7:2.0. Use Query::build instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

97
            $baseUrl . '?' . /** @scrutinizer ignore-deprecated */ build_query($httpRequestParams),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
98 1
            $this->headers,
99 1
            $mindBoxRequest->getBodyAsJson()
100
        );
101 1
        $this->response = $this->client->send($this->httpRequest, ['timeout' => $this->httpTimeOut]);
102 1
    }
103
104
    /**
105
     * @return ResponseInterface
106
     */
107
    public function getResponse(): ResponseInterface
108
    {
109
        return $this->response;
110
    }
111
112
    /**
113
     * @return Request
114
     */
115
    public function getHttpRequest(): Request
116
    {
117
        return $this->httpRequest;
118
    }
119
120
    /**
121
     * @param int $httpTimeOut
122
     */
123
    public function setHttpTimeOut(int $httpTimeOut): void
124
    {
125
        $this->httpTimeOut = $httpTimeOut;
126
    }
127
128
}
129