1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sendy\SendyBundle\Service; |
6
|
|
|
|
7
|
|
|
use Sendy\SendyBundle\SendyException; |
8
|
|
|
use SendyPHP\SendyPHP; |
9
|
|
|
|
10
|
|
|
final class SendyManager implements SendyManagerInterface |
11
|
|
|
{ |
12
|
|
|
private SendyPHP $sendy; |
13
|
|
|
|
14
|
|
|
public function __construct(SendyPHP $sendy) |
15
|
|
|
{ |
16
|
|
|
$this->sendy = $sendy; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @throws SendyException |
21
|
|
|
*/ |
22
|
|
|
public function getSubscriberCount(string $list = ''): int |
23
|
|
|
{ |
24
|
|
|
if ('' !== $list) { |
25
|
|
|
$this->setList($list); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
try { |
29
|
|
|
$result = $this->sendy->subcount($list); |
30
|
|
|
|
31
|
|
|
if (false === $result['status']) { // check status and throw exception |
32
|
|
|
throw new SendyException($result['message']); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return (int) $result['message']; |
36
|
|
|
} catch (\Exception $exception) { |
37
|
|
|
throw new SendyException($exception->getMessage()); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @throws SendyException |
43
|
|
|
*/ |
44
|
|
|
public function getSubscriberStatus(string $email, string $list = ''): string |
45
|
|
|
{ |
46
|
|
|
if ('' !== $list) { |
47
|
|
|
$this->setList($list); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$result = $this->sendy->substatus($email); |
52
|
|
|
|
53
|
|
|
if (false === $result['status']) { // check status and throw exception |
54
|
|
|
throw new SendyException($result['message']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $result['message']; |
58
|
|
|
} catch (\Exception $exception) { |
59
|
|
|
throw new SendyException($exception->getMessage()); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @throws SendyException |
65
|
|
|
*/ |
66
|
|
|
public function setList(string $list): void |
67
|
|
|
{ |
68
|
|
|
try { |
69
|
|
|
$this->sendy->setListId($list); |
70
|
|
|
} catch (\Exception $exception) { |
71
|
|
|
throw new SendyException($exception->getMessage()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @throws SendyException |
77
|
|
|
*/ |
78
|
|
|
public function subscribe(string $name, string $email, string $list = ''): void |
79
|
|
|
{ |
80
|
|
|
if ('' !== $list) { |
81
|
|
|
$this->setList($list); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$result = $this->sendy->subscribe([ |
86
|
|
|
'name' => $name, |
87
|
|
|
'email' => $email, |
88
|
|
|
]); |
89
|
|
|
|
90
|
|
|
if (false === $result['status']) { // check status and throw exception |
91
|
|
|
throw new SendyException($result['message']); |
92
|
|
|
} |
93
|
|
|
} catch (\Exception $exception) { |
94
|
|
|
throw new SendyException($exception->getMessage()); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws SendyException |
100
|
|
|
*/ |
101
|
|
|
public function unsubscribe(string $email, string $list = ''): void |
102
|
|
|
{ |
103
|
|
|
if ('' !== $list) { |
104
|
|
|
$this->setList($list); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
try { |
108
|
|
|
$result = $this->sendy->unsubscribe($email); |
109
|
|
|
|
110
|
|
|
if (false === $result['status']) { // check status and throw exception |
111
|
|
|
throw new SendyException($result['message']); |
112
|
|
|
} |
113
|
|
|
} catch (\Exception $exception) { |
114
|
|
|
throw new SendyException($exception->getMessage()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|