diff --git a/src/ApiManager.php b/src/ApiManager.php index 68e2314..88ab958 100644 --- a/src/ApiManager.php +++ b/src/ApiManager.php @@ -11,6 +11,11 @@ class ApiManager implements ApiManagerInterface */ protected $adapter; + /** + * @var array + */ + protected $subscriptions = []; + /** * ApiManager constructor. * @@ -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} */ @@ -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, + ] + ]; + } } diff --git a/src/ApiManagerInterface.php b/src/ApiManagerInterface.php index 7df0389..2a70091 100644 --- a/src/ApiManagerInterface.php +++ b/src/ApiManagerInterface.php @@ -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. * diff --git a/tests/ApiTest.php b/tests/ApiTest.php index d46453f..4469a9e 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -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( @@ -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') @@ -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 + ); } }