|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Spatie\Newsletter\Drivers; |
| 4 | + |
| 5 | +use MailerLite\MailerLite; |
| 6 | +use Spatie\Newsletter\Support\Lists; |
| 7 | + |
| 8 | +class MailerLiteDriver implements Driver |
| 9 | +{ |
| 10 | + protected MailerLite $mailerLite; |
| 11 | + protected Lists $lists; |
| 12 | + |
| 13 | + public static function make(array $arguments, Lists $lists): self |
| 14 | + { |
| 15 | + return new self($arguments, $lists); |
| 16 | + } |
| 17 | + |
| 18 | + public function __construct(array $arguments, Lists $lists) |
| 19 | + { |
| 20 | + $this->mailerLite = new MailerLite([ |
| 21 | + 'api_key' => $arguments['api_key'] ?? '' |
| 22 | + ]); |
| 23 | + |
| 24 | + $this->lists = $lists; |
| 25 | + } |
| 26 | + |
| 27 | + public function getApi(): MailerLite |
| 28 | + { |
| 29 | + return $this->mailerLite; |
| 30 | + } |
| 31 | + |
| 32 | + public function subscribe( |
| 33 | + string $email, |
| 34 | + array $properties = [], |
| 35 | + string $listName = '', |
| 36 | + array $options = [], |
| 37 | + bool $unsubscribe = false |
| 38 | + ): array|false { |
| 39 | + $groupId = $this->lists->findByName($listName)->getId(); |
| 40 | + |
| 41 | + try { |
| 42 | + $response = $this->mailerLite->subscribers->create([ |
| 43 | + 'email' => $email, |
| 44 | + 'groups' => [$groupId], |
| 45 | + 'fields' => $properties, |
| 46 | + 'status' => $unsubscribe ? 'unsubscribed' : null, |
| 47 | + ]); |
| 48 | + |
| 49 | + return $response['body']['data'] ?? false; |
| 50 | + } catch (\Exception $e) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public function subscribeOrUpdate( |
| 56 | + string $email, |
| 57 | + array $properties = [], |
| 58 | + string $listName = '', |
| 59 | + array $options = [] |
| 60 | + ): array|false { |
| 61 | + return $this->subscribe($email, $properties, $listName, $options); |
| 62 | + } |
| 63 | + |
| 64 | + public function unsubscribe(string $email, string $listName = ''): array|false |
| 65 | + { |
| 66 | + if ($listName) { |
| 67 | + $subscriber = $this->getMember($email, $listName); |
| 68 | + |
| 69 | + if ($subscriber) { |
| 70 | + $groupId = $this->lists->findByName($listName)->getId(); |
| 71 | + $this->mailerLite->groups->unAssignSubscriber($groupId, $subscriber['id']); |
| 72 | + |
| 73 | + return $this->getMember($email); |
| 74 | + } |
| 75 | + |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + return $this->subscribe($email, listName: $listName, unsubscribe: true); |
| 80 | + } |
| 81 | + |
| 82 | + public function delete(string $email, string $listName = ''): bool |
| 83 | + { |
| 84 | + $subscriber = $this->getMember($email, $listName); |
| 85 | + |
| 86 | + if ($subscriber) { |
| 87 | + try { |
| 88 | + $this->mailerLite->subscribers->delete($subscriber['id']); |
| 89 | + return true; |
| 90 | + } catch (\Exception $e) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + public function getMember(string $email, string $listName = ''): array|false |
| 99 | + { |
| 100 | + try { |
| 101 | + $response = $this->mailerLite->subscribers->find($email); |
| 102 | + |
| 103 | + $groupId = $this->lists->findByName($listName)->getId(); |
| 104 | + |
| 105 | + return $this->matchGroup($response, $groupId); |
| 106 | + } catch (\Exception $e) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public function hasMember(string $email, string $listName = ''): bool |
| 112 | + { |
| 113 | + return $this->getMember($email, $listName) !== false; |
| 114 | + } |
| 115 | + |
| 116 | + public function isSubscribed(string $email, string $listName = ''): bool |
| 117 | + { |
| 118 | + $subscriber = $this->getMember($email, $listName); |
| 119 | + |
| 120 | + return $subscriber && ($subscriber['status'] ?? null) === 'active'; |
| 121 | + } |
| 122 | + |
| 123 | + protected function matchGroup(array $subscriber, string|int $groupId): array|false |
| 124 | + { |
| 125 | + $groups = $subscriber['body']['data']['groups'] ?? []; |
| 126 | + |
| 127 | + $groupIds = array_column($groups, 'id'); |
| 128 | + |
| 129 | + if (in_array($groupId, $groupIds, true)) { |
| 130 | + return $subscriber['body']['data']; |
| 131 | + } |
| 132 | + |
| 133 | + return false; |
| 134 | + } |
| 135 | +} |
0 commit comments