Test Failed
Push — main ( 1525b1...f2efdb )
by Evgenii
02:07
created

MindBoxClient::sendData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0052

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 21
ccs 11
cts 12
cp 0.9167
rs 9.8666
cc 3
nc 4
nop 1
crap 3.0052
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 = 15;
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
37
    /**
38
     * @param string $secretKey
39
     * @param string $endpointId
40
     * @param ClientInterface|null $client
41
     * @throws EmptyApiEndPointException
42
     * @throws EmptyApiKeyException
43
     */
44 3
    public function __construct(
45
        string $secretKey,
46
        string $endpointId,
47
        ClientInterface $client = null)
48
    {
49 3
        $this->client = $client ?? new Client();
50
51 3
        $this->secretKey = $secretKey;
52 3
        $this->endpointId = $endpointId;
53
54 3
        if (empty($this->secretKey))
55 1
            throw new EmptyApiKeyException();
56
57 2
        if (empty($this->endpointId))
58 1
            throw new EmptyApiEndPointException();
59
60 1
        $this->headers = [
61 1
            'Content-Type' => 'application/json; charset=utf-8',
62 1
            'Accept' => 'application/json',
63 1
            'Authorization' => "Mindbox secretKey=\"{$this->secretKey}\""
64
        ];
65 1
    }
66
67
    /**
68
     * @param ClientInterface $client
69
     */
70
    public function setClient(ClientInterface $client): void
71
    {
72
        $this->client = $client;
73
    }
74
75
    /**
76
     * @param MindBoxRequest $mindBoxRequest
77
     * @throws GuzzleException
78
     */
79 1
    public function sendData(MindBoxRequest $mindBoxRequest): void
80
    {
81
        $httpRequestParams = [
82 1
            'endpointId' => $this->endpointId,
83 1
            'operation' => $mindBoxRequest->getOperationName()
84
        ];
85
86
87 1
        if ($mindBoxRequest->getDeviceUUID()) {
88
            $httpRequestParams['deviceUUID'] = $mindBoxRequest->getDeviceUUID();
89
        }
90
91 1
        $baseUrl = $mindBoxRequest->isAsync() ? self::ASYNC_MINDBOX_API_URL : self::SYNC_MINDBOX_API_URL;
92
93 1
        $this->httpRequest = new Request(
94 1
            'POST',
95 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

95
            $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...
96 1
            $this->headers,
97 1
            $mindBoxRequest->getBodyAsJson()
98
        );
99 1
        $this->response = $this->client->send($this->httpRequest, ['timeout' => self::DEFAULT_HTTP_TIMEOUT]);
100 1
    }
101
102
    /**
103
     * @return ResponseInterface
104
     */
105
    public function getResponse(): ResponseInterface
106
    {
107
        return $this->response;
108
    }
109
110
    /**
111
     * @return Request
112
     */
113
    public function getHttpRequest(): Request
114
    {
115
        return $this->httpRequest;
116
    }
117
118
}
119