You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
973 B

7 years ago
<?php
namespace core\services\newsletter;
3 years ago
use RuntimeException;
7 years ago
class MailChimp implements Newsletter
{
3 years ago
private \DrewM\MailChimp\MailChimp $client;
private string $listId;
7 years ago
public function __construct(\DrewM\MailChimp\MailChimp $client, $listId)
{
$this->client = $client;
$this->listId = $listId;
}
public function subscribe($email): void
{
$this->client->post('lists/' . $this->listId . '/members', [
'email_address' => $email,
'status' => 'subscribed',
]);
if ($error = $this->client->getLastError()) {
3 years ago
throw new RuntimeException($error);
7 years ago
}
}
public function unsubscribe($email): void
{
$hash = $this->client->subscriberHash($email);
$this->client->delete('lists/' . $this->listId . '/members/' . $hash);
if ($error = $this->client->getLastError()) {
3 years ago
throw new RuntimeException($error);
7 years ago
}
}
3 years ago
}