1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the PayBreak/basket package. |
4
|
|
|
* |
5
|
|
|
* (c) PayBreak <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PayBreak\Sdk\ApiClient; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use WNowicki\Generic\ApiClient\ErrorResponseException; |
15
|
|
|
use WNowicki\Generic\ApiClient\WrongResponseException; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ProviderCsvApiClient |
20
|
|
|
* |
21
|
|
|
* @author EA |
22
|
|
|
* @package PayBreak\Sdk\ApiClient |
23
|
|
|
*/ |
24
|
|
|
class ProviderCsvApiClient extends ProviderApiClient |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @author WN |
28
|
|
|
* @param string $baseUrl |
29
|
|
|
* @param string $token |
30
|
|
|
* @param LoggerInterface $logger |
31
|
|
|
* @return ProviderApiClient |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public static function make($baseUrl, $token = '', LoggerInterface $logger = null) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$ar = []; |
36
|
|
|
$ar['base_uri'] = $baseUrl; |
37
|
|
|
|
38
|
|
|
if ($token != '') { |
39
|
|
|
|
40
|
|
|
$ar['headers'] = ['Authorization' => 'ApiToken token="' . $token . '"']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return new self($ar, $logger); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @author EA |
48
|
|
|
* @param ResponseInterface $response |
49
|
|
|
* @return array |
50
|
|
|
* @throws WrongResponseException |
51
|
|
|
*/ |
52
|
|
|
protected function processResponse(ResponseInterface $response) |
53
|
|
|
{ |
54
|
|
|
if ($response->getStatusCode() == 204) { |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (strpos($response->getHeaderLine('Content-Type'), 'text/csv') !== false) { |
59
|
|
|
return ['csv' => $response->getBody()->getContents()] ; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
throw new WrongResponseException('Response body was malformed csv', $response->getStatusCode()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @author EA |
67
|
|
|
* @param string $uri |
68
|
|
|
* @param array $body |
69
|
|
|
* @param array $query |
70
|
|
|
* @return array |
71
|
|
|
* @throws ErrorResponseException |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
|
|
public function get($uri, array $body = [], array $query = []) |
75
|
|
|
{ |
76
|
|
|
return parent::get($uri . '.csv', $body, $query); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.