Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 74 additions & 9 deletions src/ApiManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class ApiManager implements ApiManagerInterface
*/
protected $adapter;

/**
* @var array
*/
protected $subscriptions = [];

/**
* ApiManager constructor.
*
Expand All @@ -31,21 +36,49 @@ public function createSubscriber(
array $attributes = [],
array $globalAttributes = []
) {
$now = time();
$subscription = $this->buildSubscribtion($email, $groupId, $active, $attributes, $globalAttributes);

return $this->adapter->action(
'post',
"/v3/groups.json/{$groupId}/receivers",
[
'email' => $email,
'registered' => $now,
'activated' => $active ? $now : 0,
'attributes' => $attributes,
'global_attributes' => $globalAttributes,
]
"/v3/groups.json/{$subscription['group']}/receivers",
$subscription['user']
);
}

/**
* @inheritDoc
*/
public function addCreateSubscriber(
string $email,
int $groupId,
bool $active = false,
array $attributes = [],
array $globalAttributes = []
) {
$subscription = $this->buildSubscribtion($email, $groupId, $active, $attributes, $globalAttributes);
if (!isset($this->subscriptions[$subscription['group']])) {
$this->subscriptions[$subscription['group']] = [];
}
$this->subscriptions[$subscription['group']][] = $subscription['user'];
}

/**
* @inheritDoc
*/
public function flush()
{
$response = [];
foreach ($this->subscriptions as $group => $subscriptions) {
$response[$group] = $this->adapter->action(
'post',
"/v3/groups.json/{$group}/receivers/upsert",
$subscriptions
);
}
$this->subscriptions = [];
return $response;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -131,4 +164,36 @@ public function getAdapter()
{
return $this->adapter;
}

/**
* Builds subscription information.
*
* @param string $email
* @param int $groupId
* @param bool $active
* @param array $attributes
* @param array $globalAttributes
*
* @return array
*/
protected function buildSubscribtion(
string $email,
int $groupId,
bool $active = false,
array $attributes = [],
array $globalAttributes = []
) {
$now = time();

return [
'group' => $groupId,
'user' => [
'email' => $email,
'registered' => $now,
'activated' => $active ? $now : 0,
'attributes' => $attributes,
'global_attributes' => $globalAttributes,
]
];
}
}
24 changes: 24 additions & 0 deletions src/ApiManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ public function createSubscriber(
array $globalAttributes = []
);

/**
* Creates a subscriber for batch updating with flush().
*
* @param string $email
* @param int $groupId
* @param bool $active
* @param array $attributes
* @param array $globalAttributes
*/
public function addCreateSubscriber(
string $email,
int $groupId,
bool $active = false,
array $attributes = [],
array $globalAttributes = []
);

/**
* Flushes all batched subscription requests.
*
* @return array
*/
public function flush();

/**
* Returns a subscriber.
*
Expand Down
41 changes: 41 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,37 @@ public function testCreateSubscriber()
$this->assertEquals('john.doe@example.org', $response['email']);
}

public function testAddCreateSubscriber()
{
$groupId = getenv('GROUP_ID');
self::$apiManager->addCreateSubscriber(
'john.doe.2@example.org',
$groupId,
false,
[
'salutation' => 'Mr.',
'firstname' => 'John',
'lastname' => 'Doe',
]
);
self::$apiManager->addCreateSubscriber(
'jane.doe.2@example.org',
$groupId,
false,
[
'salutation' => 'Mr.',
'firstname' => 'John',
'lastname' => 'Doe',
]
);
$response = self::$apiManager->flush();

$this->assertCount(1, $response);
$this->assertArrayHasKey($groupId, $response);
$this->assertCount(2, $response[$groupId]);
$this->assertEquals('insert success', $response[$groupId][0]['status']);
}

public function testGetSubscriber()
{
$response = self::$apiManager->getSubscriber(
Expand Down Expand Up @@ -138,6 +169,7 @@ public function testSetSubscriberStatus()

public function testDeleteSubscriber()
{
$groupId = getenv('GROUP_ID');
$response = self::$apiManager->deleteSubscriber(
'john.doe@example.org',
getenv('GROUP_ID')
Expand All @@ -160,5 +192,14 @@ public function testDeleteSubscriber()
],
]
);

self::$apiManager->deleteSubscriber(
'john.doe.2@example.org',
$groupId
);
self::$apiManager->deleteSubscriber(
'jane.doe.2@example.org',
$groupId
);
}
}