|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace MailMarketing\Entities; |
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use MailMarketing\MailMarketingException; |
|
8
|
|
|
|
|
9
|
|
|
class MailchimpResponse implements ResponseInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected array $response = []; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @throws MailMarketingException |
|
15
|
|
|
*/ |
|
16
|
10 |
|
public function __construct($response = []) |
|
17
|
|
|
{ |
|
18
|
10 |
|
if (is_bool($response)) { |
|
19
|
8 |
|
$response = [ |
|
20
|
8 |
|
'status' => $response, |
|
21
|
8 |
|
]; |
|
22
|
|
|
} |
|
23
|
10 |
|
if (!is_array($response)) { |
|
24
|
1 |
|
throw new MailMarketingException('Mailchimp response not array: ' . print_r($response, true)); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
9 |
|
$this->response = $response; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function toArray(): array |
|
34
|
|
|
{ |
|
35
|
2 |
|
return $this->getResponse(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
public function __toString(): string |
|
39
|
|
|
{ |
|
40
|
2 |
|
return (string)print_r($this->toArray(), true); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritDoc |
|
45
|
|
|
*/ |
|
46
|
10 |
|
public static function init($data): static |
|
47
|
|
|
{ |
|
48
|
10 |
|
return new static($data); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritDoc |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function isSuccess(): bool |
|
55
|
|
|
{ |
|
56
|
2 |
|
if (Arr::has($this->response, 'status') && is_bool(Arr::get($this->response, 'status'))) { |
|
57
|
2 |
|
return Arr::get($this->response, 'status'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
return !(Arr::has($this->response, 'status') && Arr::has($this->response, 'instance')); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritDoc |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function getResponse(): array |
|
67
|
|
|
{ |
|
68
|
2 |
|
return $this->response; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritDoc |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function getErrorMessage(): string |
|
75
|
|
|
{ |
|
76
|
2 |
|
if ($this->isSuccess()) { |
|
77
|
1 |
|
return ''; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
if (Arr::has($this->response, 'errors.0')) { |
|
81
|
2 |
|
return Arr::get($this->response, 'title', 'Error') . ': ' . Arr::get($this->response, 'errors.0.message'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
return Arr::get($this->response, 'title', 'Error') . ': ' . Arr::get($this->response, 'detail', 'Request error'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritDoc |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function get(?string $key = null, mixed $default = null): mixed |
|
91
|
|
|
{ |
|
92
|
2 |
|
if (!is_null($key)) { |
|
93
|
2 |
|
return Arr::get($this->getResponse(), $key, $default); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
return $this->getResponse(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|