|
5 | 5 | use App\Synchronizer\Mapper\Mapper; |
6 | 6 | use App\Http\MailChimpClient; |
7 | 7 | use App\Synchronizer\Filter; |
| 8 | +use App\Exceptions\FakeEmailException; |
| 9 | +use App\Exceptions\InvalidEmailException; |
| 10 | +use App\Exceptions\AlreadyInListException; |
| 11 | +use App\Exceptions\CleanedEmailException; |
| 12 | +use App\Exceptions\UnsubscribedEmailException; |
| 13 | +use App\Exceptions\ArchivedException; |
| 14 | +use App\Exceptions\EmailComplianceException; |
| 15 | +use App\Exceptions\MailchimpTooManySubscriptionsException; |
| 16 | +use Illuminate\Support\Facades\Log; |
8 | 17 |
|
9 | 18 | class WebsiteToMailchimpSynchronizer |
10 | 19 | { |
| 20 | + use NotificationTrait; |
11 | 21 | /** |
12 | 22 | * @var Config |
13 | 23 | */ |
@@ -69,6 +79,7 @@ public function __construct(string $configFileName) |
69 | 79 | * @throws \App\Exceptions\MergeFieldException |
70 | 80 | * @throws \App\Exceptions\MailchimpTooManySubscriptionsException |
71 | 81 | * @throws \App\Exceptions\ArchivedException |
| 82 | + * @throws \App\Exceptions\CleanedEmailException |
72 | 83 | */ |
73 | 84 | public function syncSingle(array $websiteData) |
74 | 85 | { |
@@ -99,7 +110,34 @@ public function syncSingle(array $websiteData) |
99 | 110 | $mailchimpData['status'] = 'subscribed'; |
100 | 111 |
|
101 | 112 | // Add the subscriber to Mailchimp |
102 | | - $response = $this->mailchimpClient->putSubscriber($mailchimpData); |
| 113 | + try { |
| 114 | + $response = $this->mailchimpClient->putSubscriber($mailchimpData); |
| 115 | + } catch (AlreadyInListException $e) { |
| 116 | + Log::debug("Subscriber already in list: {$mailchimpData['email_address']}"); |
| 117 | + return ['status' => 'success']; |
| 118 | + } catch (InvalidEmailException $e) { |
| 119 | + Log::info("Invalid email address: {$mailchimpData['email_address']}"); |
| 120 | + return ['status' => 'success']; |
| 121 | + } catch (FakeEmailException $e) { |
| 122 | + $this->notifyAdminInvalidEmail($mailchimpData); |
| 123 | + Log::info("Fake or invalid email detected: {$mailchimpData['email_address']}"); |
| 124 | + return ['status' => 'success']; |
| 125 | + } catch (CleanedEmailException $e) { |
| 126 | + Log::info("Email marked as cleaned (bounced): {$mailchimpData['email_address']}"); |
| 127 | + return ['status' => 'success']; |
| 128 | + } catch (UnsubscribedEmailException $e) { |
| 129 | + Log::info("Email previously unsubscribed: {$mailchimpData['email_address']}"); |
| 130 | + return ['status' => 'success']; |
| 131 | + } catch (ArchivedException $e) { |
| 132 | + Log::info("Email is archived: {$mailchimpData['email_address']}"); |
| 133 | + return ['status' => 'success']; |
| 134 | + } catch (EmailComplianceException $e) { |
| 135 | + Log::info("Email compliance issue: {$mailchimpData['email_address']}"); |
| 136 | + return ['status' => 'success']; |
| 137 | + } catch (MailchimpTooManySubscriptionsException $e) { |
| 138 | + Log::info("Too many subscriptions from email: {$mailchimpData['email_address']}"); |
| 139 | + return ['status' => 'success']; |
| 140 | + } |
103 | 141 |
|
104 | 142 | // Add tags to new subscriber |
105 | 143 | $tags = [$this->config->getNewTag()]; |
|
0 commit comments