1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mrkj\Laposta; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Mrkj\Laposta\Models\List_; |
7
|
|
|
use GuzzleHttp\Exception\RequestException; |
8
|
|
|
|
9
|
|
|
class Laposta |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $apiKey; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Client |
18
|
|
|
*/ |
19
|
|
|
private $client; |
20
|
|
|
|
21
|
|
|
public function __construct($apiKey = null, $client = null) |
22
|
|
|
{ |
23
|
|
|
if (empty(trim($apiKey))) { |
24
|
|
|
throw new \Exception('No API key provided'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->apiKey = $apiKey; |
28
|
|
|
|
29
|
|
|
if ($client && $client instanceof Client) { |
30
|
|
|
$this->client = $client; |
31
|
|
|
} else { |
32
|
|
|
$this->client = new Client([ |
33
|
|
|
'base_uri' => 'https://api.laposta.nl/v2/', |
34
|
|
|
'auth' => [$this->apiKey, ''], |
35
|
|
|
'http_errors' => true, |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return List_[] |
42
|
|
|
*/ |
43
|
|
|
public function getLists() |
44
|
|
|
{ |
45
|
|
|
$data = $this->get('list'); |
46
|
|
|
|
47
|
|
|
$lists = []; |
48
|
|
|
|
49
|
|
|
foreach ($data['data'] as $listResponse) { |
50
|
|
|
$lists[] = List_::createFromResponse($listResponse['list']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $lists; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $listId |
58
|
|
|
* @return bool|List_ |
59
|
|
|
*/ |
60
|
|
|
public function getList($listId) |
61
|
|
|
{ |
62
|
|
|
$data = $this->get('list/'.$listId); |
63
|
|
|
|
64
|
|
|
$list = List_::createFromResponse($data['list']); |
65
|
|
|
|
66
|
|
|
return $list; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function createList(List_ $list) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$data = $this->post('list', [ |
72
|
|
|
'form_params' => [ |
73
|
|
|
'name' => $list->getName(), |
74
|
|
|
'remarks' => $list->getRemarks(), |
75
|
|
|
'subscribe_notification_email' => $list->getSubscribeNotificationEmail(), |
76
|
|
|
'unsubscribe_notification_email' => $list->getUnsubsribeNotificationEmail(), |
77
|
|
|
], |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
$list->updateFromResponse($data['list']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function updateList(List_ $list) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$this->client->post('list/'.$list->getListId(), [ |
86
|
|
|
'form_params' => [ |
87
|
|
|
'name' => $list->getName(), |
88
|
|
|
'remarks' => $list->getRemarks(), |
89
|
|
|
'subscribe_notification_email' => $list->getSubscribeNotificationEmail(), |
90
|
|
|
'unsubscribe_notification_email' => $list->getUnsubsribeNotificationEmail(), |
91
|
|
|
], |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function get($uri, $options = []) |
96
|
|
|
{ |
97
|
|
|
return $this->request('get', $uri, $options); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function post($uri, $options = []) |
101
|
|
|
{ |
102
|
|
|
return $this->request('post', $uri, $options); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function request($method, $uri, $options = []) |
106
|
|
|
{ |
107
|
|
|
try { |
108
|
|
|
$response = $this->client->request($method, $uri, $options); |
109
|
|
|
|
110
|
|
|
$data = json_decode($response->getBody()->getContents(), true); |
111
|
|
|
|
112
|
|
|
return $data; |
113
|
|
|
} catch (RequestException $e) { |
114
|
|
|
throw new \Exception('Loading Laposta resource failed: '.$e->getMessage()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.