1 | <?php |
||
2 | /** |
||
3 | * @project Promopult Integra client library |
||
4 | */ |
||
5 | |||
6 | namespace Promopult\Integra; |
||
7 | |||
8 | /** |
||
9 | * Class Client |
||
10 | * |
||
11 | * @method \Promopult\Integra\Response hello(array $data) |
||
12 | * @method \Promopult\Integra\Response createUser(array $data) |
||
13 | * @method \Promopult\Integra\Response cryptLogin(array $data) |
||
14 | * @method \Promopult\Integra\Response archiveUser(array $data) |
||
15 | * @method \Promopult\Integra\Response unarchiveUser(array $data) |
||
16 | * @method \Promopult\Integra\Response doPayment(array $data) |
||
17 | * @method \Promopult\Integra\Response confirmPayment(array $data) |
||
18 | * @method \Promopult\Integra\Response declinePayment(array $data) |
||
19 | * @method \Promopult\Integra\Response getUserData(array $data) |
||
20 | * @method \Promopult\Integra\Response getUsersData(array $data) |
||
21 | * @method \Promopult\Integra\Response getUserMessages(array $data) |
||
22 | * @method \Promopult\Integra\Response getMessages(array $data) |
||
23 | * @method \Promopult\Integra\Response getMessageTemplates(array $data) |
||
24 | * @method \Promopult\Integra\Response readMessages(array $data) |
||
25 | * @method \Promopult\Integra\Response changeUrl(array $data) |
||
26 | * @method \Promopult\Integra\Response getFinSummaryByDate(array $data) |
||
27 | * @method \Promopult\Integra\Response attachYandexMetrikaCounter(array $data) |
||
28 | * |
||
29 | * @author Dmitry Gladyshev <[email protected]> |
||
30 | * @since 1.0 |
||
31 | */ |
||
32 | class Client implements \Promopult\Integra\TransportInterface |
||
33 | { |
||
34 | /** |
||
35 | * @var \Promopult\Integra\CredentialsInterface |
||
36 | */ |
||
37 | protected $identity; |
||
38 | |||
39 | /** |
||
40 | * @var \Promopult\Integra\CryptInterface |
||
41 | */ |
||
42 | protected $crypt; |
||
43 | |||
44 | /** |
||
45 | * @var \Psr\Http\Client\ClientInterface |
||
46 | */ |
||
47 | protected $httpClient; |
||
48 | |||
49 | /** |
||
50 | * @var \Psr\Http\Message\RequestInterface |
||
51 | */ |
||
52 | protected $lastHttpRequest; |
||
53 | |||
54 | /** |
||
55 | * @var \Psr\Http\Message\ResponseInterface |
||
56 | */ |
||
57 | protected $lastHttpResponse; |
||
58 | |||
59 | /** |
||
60 | * Client constructor. |
||
61 | * |
||
62 | * @param \Promopult\Integra\CredentialsInterface $identity |
||
63 | * @param \Promopult\Integra\CryptInterface $crypt |
||
64 | * @param \Psr\Http\Client\ClientInterface $httpClient |
||
65 | */ |
||
66 | public function __construct( |
||
67 | \Promopult\Integra\CredentialsInterface $identity, |
||
68 | \Promopult\Integra\CryptInterface $crypt, |
||
69 | \Psr\Http\Client\ClientInterface $httpClient = null |
||
70 | ) { |
||
71 | $this->identity = $identity; |
||
72 | $this->crypt = $crypt; |
||
73 | $this->httpClient = $httpClient; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param string $methodName |
||
78 | * @param array $ars |
||
79 | * @return ResponseInterface |
||
80 | * @throws \Psr\Http\Client\ClientExceptionInterface |
||
81 | */ |
||
82 | public function __call(string $methodName, array $ars = []): \Promopult\Integra\ResponseInterface |
||
83 | { |
||
84 | $request = new \Promopult\Integra\Request( |
||
85 | $methodName, |
||
86 | $ars[0] ?? [], |
||
87 | $this->identity, |
||
88 | $this->crypt |
||
89 | ); |
||
90 | |||
91 | return $this->send($request); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@inheritDoc} |
||
96 | */ |
||
97 | public function send(\Promopult\Integra\RequestInterface $request): \Promopult\Integra\ResponseInterface |
||
98 | { |
||
99 | $httpRequest = new \GuzzleHttp\Psr7\Request('POST', $request->getCryptUrl(), [ |
||
100 | 'Content-Type' => 'application/json' |
||
101 | ]); |
||
102 | |||
103 | $this->lastHttpRequest = $httpRequest; |
||
104 | |||
105 | $httpResponse = $this->getHttpClient()->sendRequest($httpRequest); |
||
106 | |||
107 | $this->lastHttpResponse = $httpResponse; |
||
108 | |||
109 | return \Promopult\Integra\Response::fromHttpResponse($httpResponse); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return \Psr\Http\Client\ClientInterface |
||
114 | */ |
||
115 | protected function getHttpClient(): \Psr\Http\Client\ClientInterface |
||
116 | { |
||
117 | if (empty($this->httpClient)) { |
||
118 | $this->httpClient = \Http\Adapter\Guzzle6\Client::createWithConfig(['verify' => false]); |
||
119 | } |
||
120 | |||
121 | return $this->httpClient; |
||
122 | } |
||
123 | |||
124 | /***************/ |
||
125 | /* Debug stuff */ |
||
126 | /***************/ |
||
127 | |||
128 | /** |
||
129 | * @return \Psr\Http\Message\ResponseInterface|null |
||
130 | */ |
||
131 | public function getLastHttpResponse(): ?\Psr\Http\Message\ResponseInterface |
||
132 | { |
||
133 | return $this->lastHttpResponse; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @return \Psr\Http\Message\RequestInterface|null |
||
138 | */ |
||
139 | public function getLastHttpRequest(): ?\Psr\Http\Message\RequestInterface |
||
140 | { |
||
141 | return $this->lastHttpRequest; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return string |
||
146 | */ |
||
147 | public function getLastHttpResponseAsString(): string |
||
148 | { |
||
149 | if ($this->lastHttpResponse instanceof \Psr\Http\Message\ResponseInterface) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
150 | return \GuzzleHttp\Psr7\str($this->getLastHttpResponse()); |
||
151 | } |
||
152 | |||
153 | return ''; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | public function getLastHttpRequestAsString(): string |
||
160 | { |
||
161 | if ($this->lastHttpRequest instanceof \Psr\Http\Message\RequestInterface) { |
||
0 ignored issues
–
show
|
|||
162 | return \GuzzleHttp\Psr7\str($this->getLastHttpRequest()); |
||
163 | } |
||
164 | |||
165 | return ''; |
||
166 | } |
||
167 | } |
||
168 |