Skip to content

Commit

Permalink
Add unsubscribed since endpoint (#15)
Browse files Browse the repository at this point in the history
* allow query params in adapter get method
* add getUnsubscribedContactsSince endpoint
  • Loading branch information
amneale authored Jun 13, 2018
1 parent fa62644 commit 996db03
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Currently the following endpoints are covered:
- [x] Delete contact from address book
- [x] Get contact by email
- [x] Get contact address books
- [x] Get unsubscribed contacts since date
- [x] Unsubscribe contact
- [x] Resubscribe contact
- [ ] **Contact data fields**
Expand Down
3 changes: 2 additions & 1 deletion src/Adapter/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ interface Adapter
{
/**
* @param string $url
* @param array $params
*
* @return ResponseInterface
*/
public function get(string $url): ResponseInterface;
public function get(string $url, array $params = []): ResponseInterface;

/**
* @param string $url
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/GuzzleAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public static function fromCredentials(
/**
* @inheritdoc
*/
public function get(string $url): ResponseInterface
public function get(string $url, array $params = []): ResponseInterface
{
return $this->client->request('GET', $url);
return $this->client->request('GET', $url, ['query' => $params]);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/Dotmailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,44 @@ public function getContactAddressBooks(Contact $contact): array
return $addressBooks;
}

/**
* @param \DateTimeInterface $dateTime
* @param int|null $select
* @param int|null $skip
*
* @return Contact[]
*/
public function getUnsubscribedContactsSince(
\DateTimeInterface $dateTime,
int $select = null,
int $skip = null
): array {
$this->response = $this->adapter->get(
'/v2/contacts/unsubscribed-since/' . $dateTime->format('Y-m-d'),
array_filter([
'select' => $select,
'skip' => $skip,
])
);

$unsubscriptions = [];

foreach (json_decode($this->response->getBody()->getContents()) as $unsubscription) {
$unsubscriptions[] = [
'suppressedContact' => new Contact(
$unsubscription->suppressedContact->id,
$unsubscription->suppressedContact->email,
$unsubscription->suppressedContact->optInType,
$unsubscription->suppressedContact->emailType
),
'dateRemoved' => new \DateTime($unsubscription->dateRemoved),
'reason' => $unsubscription->reason,
];
}

return $unsubscriptions;
}

/**
* @param Contact $contact
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/Adapter/GuzzleAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public function testGet()
$this->client
->expects($this->once())
->method('request')
->with('GET', self::URL)
->with('GET', self::URL, ['query' => self::CONTENT])
->willReturn($this->response);

$this->assertEquals($this->response, $this->adapter->get(self::URL));
$this->assertEquals($this->response, $this->adapter->get(self::URL, self::CONTENT));
}

public function testPost()
Expand Down
40 changes: 40 additions & 0 deletions tests/DotmailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class DotmailerTest extends TestCase
const LOCALE = 'en-GB';
const WEBSITE = 'http://foo.bar/baz';
const DATA_FIELD = 'DATAFIELD';
const DATE_FROM = '2018-01-01';

/**
* @var Adapter|MockObject
Expand Down Expand Up @@ -194,6 +195,45 @@ public function testGetContactAddressBooks()
$this->assertEquals([$this->getAddressBook()], $this->dotmailer->getContactAddressBooks($this->getContact()));
}

public function testGetUnsubscribedContactsSince()
{
$contact = $this->getContact();
$dateRemoved = new \DateTime('2018-01-10');

$this->adapter
->expects($this->once())
->method('get')
->with(
'/v2/contacts/unsubscribed-since/' . self::DATE_FROM,
[
'select' => 1,
'skip' => 2,
]
)
->willReturn(
$this->getResponse(
[
[
'suppressedContact' => $contact->asArray(),
'dateRemoved' => $dateRemoved->format('Y-m-d'),
'reason' => 'unsubscribed',
]
]
)
);

$this->assertEquals(
[
[
'suppressedContact' => $contact,
'dateRemoved' => $dateRemoved,
'reason' => 'unsubscribed',
]
],
$this->dotmailer->getUnsubscribedContactsSince(new \DateTime(self::DATE_FROM), 1, 2)
);
}

public function testUnsubscribeContact()
{
$response = $this->getResponse();
Expand Down

0 comments on commit 996db03

Please sign in to comment.