1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mrkj\Laposta; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Mrkj\Laposta\Models\List_; |
7
|
|
|
use Mrkj\Laposta\Models\Member; |
8
|
|
|
use Mrkj\Laposta\Transformers\ListTransformer; |
9
|
|
|
|
10
|
|
|
class Laposta |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $apiKey; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Client |
19
|
|
|
*/ |
20
|
|
|
private $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Laposta constructor. |
24
|
|
|
* @param null|string $apiKey |
25
|
|
|
* @param null|Client $client |
26
|
|
|
* @throws \Exception |
27
|
|
|
*/ |
28
|
|
|
public function __construct($apiKey = null, $client = null) |
29
|
|
|
{ |
30
|
|
|
if (empty(trim($apiKey))) { |
31
|
|
|
throw new \Exception('No API key provided'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->apiKey = $apiKey; |
35
|
|
|
|
36
|
|
|
if ($client && $client instanceof Client) { |
37
|
|
|
$this->client = $client; |
38
|
|
|
} else { |
39
|
|
|
$this->client = new Client([ |
40
|
|
|
'base_uri' => 'https://api.laposta.nl/v2/', |
41
|
|
|
'auth' => [$this->apiKey, ''], |
42
|
|
|
'http_errors' => true, |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return List_[] |
49
|
|
|
*/ |
50
|
|
|
public function getLists() : array |
51
|
|
|
{ |
52
|
|
|
$data = $this->get('list'); |
53
|
|
|
|
54
|
|
|
$lists = []; |
55
|
|
|
|
56
|
|
|
foreach ($data['data'] as $listResponse) { |
57
|
|
|
$lists[] = List_::createFromResponse($listResponse['list']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $lists; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $listId |
65
|
|
|
* @return List_ |
66
|
|
|
*/ |
67
|
|
|
public function getList($listId) : List_ |
68
|
|
|
{ |
69
|
|
|
$data = $this->get('list/'.$listId); |
70
|
|
|
|
71
|
|
|
$list = List_::createFromResponse($data['list']); |
72
|
|
|
|
73
|
|
|
return $list; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param List_ $list |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function createList(List_ $list) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$data = $this->post('list', [ |
82
|
|
|
'form_params' => ListTransformer::toFormParams($list), |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$list->updateFromResponse($data['list']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param List_ $list |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function updateList(List_ $list) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$data = $this->post('list/'.$list->getListId(), [ |
94
|
|
|
'form_params' => ListTransformer::toFormParams($list), |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
$list->updateFromResponse($data['list']); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param List_ $list |
102
|
|
|
*/ |
103
|
|
|
public function deleteList(List_ $list) |
104
|
|
|
{ |
105
|
|
|
$data = $this->delete('list/'.$list->getListId()); |
106
|
|
|
|
107
|
|
|
$list->updateFromResponse($data['list']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param List_|string $list |
112
|
|
|
* @param string $state |
113
|
|
|
* @return Member[] |
114
|
|
|
*/ |
115
|
|
|
public function getMembers($list, $state = Member::STATE_ACTIVE) |
116
|
|
|
{ |
117
|
|
|
$listId = $list instanceof List_ ? $list->getListId() : $list; |
118
|
|
|
|
119
|
|
|
$params = ['list_id' => $listId]; |
120
|
|
|
|
121
|
|
|
if(!is_null($state) && in_array($state, Member::STATES)) |
122
|
|
|
{ |
123
|
|
|
$params['state'] = $state; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$data = $this->get('member?'.http_build_query($params)); |
127
|
|
|
|
128
|
|
|
$members = []; |
129
|
|
|
|
130
|
|
|
foreach ($data['data'] as $listResponse) { |
131
|
|
|
$members[] = Member::createFromResponse($listResponse['member']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $members; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $uri |
139
|
|
|
* @param array $options |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
|
|
private function get($uri, $options = []) : array |
143
|
|
|
{ |
144
|
|
|
return $this->request('get', $uri, $options); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $uri |
149
|
|
|
* @param array $options |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
private function post($uri, $options = []) : array |
153
|
|
|
{ |
154
|
|
|
return $this->request('post', $uri, $options); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $uri |
159
|
|
|
* @param array $options |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
private function delete($uri, $options = []) : array |
163
|
|
|
{ |
164
|
|
|
return $this->request('delete', $uri, $options); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $method |
169
|
|
|
* @param string $uri |
170
|
|
|
* @param array $options |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
private function request($method, $uri, $options = []) : array |
174
|
|
|
{ |
175
|
|
|
$response = $this->client->request($method, $uri, $options); |
176
|
|
|
|
177
|
|
|
$data = json_decode($response->getBody()->getContents(), true); |
178
|
|
|
|
179
|
|
|
return $data; |
180
|
|
|
} |
181
|
|
|
} |
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.