Skip to content

Commit

Permalink
Merge pull request #1 from t3n/manage-contact-lists
Browse files Browse the repository at this point in the history
Add method to manage contacts for a given contact list
  • Loading branch information
das-nagnag authored Feb 5, 2021
2 parents f57d840 + 1305912 commit dc89bcf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Classes/Exception/InvalidManageListActionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace t3n\MailJetAdapter\Exception;

use Neos\Flow\Exception;

class InvalidManageListActionException extends Exception
{
}
27 changes: 27 additions & 0 deletions Classes/Service/MailJetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Neos\Flow\Property\PropertyMappingConfiguration;
use Neos\Flow\Property\TypeConverter\DateTimeConverter;
use Neos\Flow\Reflection\ReflectionService;
use t3n\MailJetAdapter\Exception\InvalidManageListActionException;
use t3n\MailJetAdapter\Exception\MailJetInvalidContactDataException;
use t3n\MailJetAdapter\Exception\MailJetRequestException;
use t3n\MailJetAdapter\Model\Contact;
Expand Down Expand Up @@ -51,6 +52,8 @@ class MailJetService

protected $allowedContactMetaDataTypes = ['str', 'datetime', 'int', 'float', 'bool'];

protected $allowedManageContactListActions = ['addnoforce', 'addforce', 'remove', 'unsub'];

public function getContactByEmail(MailJetContactEmail $email, bool $includeAttributes): ?Contact
{
return $this->fetchContact((string) $email, $includeAttributes);
Expand Down Expand Up @@ -124,6 +127,30 @@ public function getContactMetaData(): array
return $this->runtimeCache['contactData'];
}

public function manageContactListForContact(Contact $contact, int $listId, string $action = 'addnoforce'): bool
{
if (!in_array($action, $this->allowedManageContactListActions)) {
throw new InvalidManageListActionException(
sprintf(
'Action %s is not a valid action to manage contact lists!
Allowed actions are: %s',
$action,
implode(', ', $this->allowedManageContactListActions)
)
);
}

$response = $this->client->post(
Resources::$ContactManagecontactslists,
[
'id' => $contact->getIdentifier(),
'body' => ['ContactsLists' => [['Action' => $action, 'ListId' => $listId]]],
]
);

return $this->parseResult($response)['count'] === 1;
}

protected function fetchContact(string $identifier, bool $includeAttributes): ?Contact
{
$response = $this->client->get(Resources::$Contact, ['id' => $identifier]);
Expand Down

0 comments on commit dc89bcf

Please sign in to comment.