1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace talismanfr\psbbank; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use talismanfr\psbbank\api\Api; |
9
|
|
|
use talismanfr\psbbank\api\vo\LoginRequest; |
10
|
|
|
use talismanfr\psbbank\api\vo\OrderRequest; |
11
|
|
|
use talismanfr\psbbank\vo\City; |
12
|
|
|
use talismanfr\psbbank\vo\Login; |
13
|
|
|
use talismanfr\psbbank\vo\Order; |
14
|
|
|
|
15
|
|
|
class ApiService |
16
|
|
|
{ |
17
|
|
|
/** @var Api */ |
18
|
|
|
private $api; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ApiService constructor. |
22
|
|
|
* @param Api $api |
23
|
|
|
*/ |
24
|
4 |
|
public function __construct(Api $api) |
25
|
|
|
{ |
26
|
4 |
|
$this->api = $api; |
27
|
4 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Auth method. Getting token. |
31
|
|
|
* @param string $email |
32
|
|
|
* @param $password |
33
|
|
|
* @return Login|null |
34
|
|
|
* @throws Exception |
35
|
|
|
*/ |
36
|
1 |
|
public function login(string $email, $password): ?Login |
37
|
|
|
{ |
38
|
1 |
|
$response = $this->api->login(new LoginRequest($email, $password)); |
39
|
1 |
|
if ($response->getCode() !== 200 || $response->getBody() == null) { |
|
|
|
|
40
|
1 |
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
return Login::fromResponseData($this->deserializationResponse($response->getBody())); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $token |
48
|
|
|
* @return City[]|null |
49
|
|
|
* @throws Exception |
50
|
|
|
*/ |
51
|
1 |
|
public function cities(string $token): ?array |
52
|
|
|
{ |
53
|
1 |
|
$response = $this->api->cities($token); |
54
|
1 |
|
$response = $this->deserializationResponse($response->getBody()); |
|
|
|
|
55
|
1 |
|
if (isset($response['data'])) { |
56
|
1 |
|
$cities = []; |
57
|
1 |
|
foreach ($response['data'] as $datum) { |
58
|
1 |
|
$cities[] = City::fromResponseData($datum); |
59
|
|
|
} |
60
|
1 |
|
return $cities; |
61
|
|
|
} else { |
62
|
1 |
|
return [City::fromResponseData($response)]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $token |
68
|
|
|
* @param OrderRequest $orderRequest |
69
|
|
|
* @return Order|null |
70
|
|
|
* @throws Exception |
71
|
|
|
*/ |
72
|
1 |
|
public function createOrder(string $token, OrderRequest $orderRequest): ?Order |
73
|
|
|
{ |
74
|
1 |
|
$response = $this->api->createOrder($token, $orderRequest); |
75
|
1 |
|
if ($response->getCode() == 503 || $response->getBody() == null) { |
|
|
|
|
76
|
1 |
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return Order::fromResponseData($this->deserializationResponse($response->getBody())); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $token |
84
|
|
|
* @param int $page |
85
|
|
|
* @return Order[]|null |
86
|
|
|
* @throws Exception |
87
|
|
|
*/ |
88
|
1 |
|
public function orders(string $token, int $page = 1): ?array |
89
|
|
|
{ |
90
|
1 |
|
$response = $this->api->orders($token, $page); |
91
|
1 |
|
if ($response->getCode() == 503 || $response->getBody() == null) { |
|
|
|
|
92
|
1 |
|
return null; |
93
|
|
|
} |
94
|
1 |
|
$response = $this->deserializationResponse($response->getBody()); |
95
|
|
|
|
96
|
1 |
|
if (isset($response['data'])) { |
97
|
1 |
|
$cities = []; |
98
|
1 |
|
foreach ($response['data'] as $datum) { |
99
|
1 |
|
$cities[] = Order::fromResponseData($datum); |
100
|
|
|
} |
101
|
1 |
|
return $cities; |
102
|
|
|
} else { |
103
|
1 |
|
return [Order::fromResponseData($response)]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $body |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
4 |
|
private function deserializationResponse(string $body): array |
112
|
|
|
{ |
113
|
4 |
|
return json_decode($body, true); |
114
|
|
|
} |
115
|
|
|
} |