From 22f3b2113bfed0fc5658f8359d7cd468ac022de9 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Thu, 6 Dec 2018 14:48:03 +0100 Subject: [PATCH 1/4] Add addCreateSubscriber() and flush() for batch-subscriptions. --- src/ApiManager.php | 82 +++++++++++++++++++++++++++++++++---- src/ApiManagerInterface.php | 24 +++++++++++ 2 files changed, 97 insertions(+), 9 deletions(-) diff --git a/src/ApiManager.php b/src/ApiManager.php index 68e2314..c7c7650 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,48 @@ public function createSubscriber( array $attributes = [], array $globalAttributes = [] ) { - $now = time(); + $subscription = $this->buildSubscribtion($email, $groupId, $active = false, $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 = false, $attributes = [], $globalAttributes); + if (!isset($this->subscriptions[$subscription['group']])) { + $this->subscriptions[$subscription['group']] = []; + } + $this->subscriptions[$subscription['group']][] = $subscription['user']; + } + + /** + * @inheritDoc + */ + public function flush() + { + foreach ($this->subscriptions as $group => $subscriptions) { + $response = $this->adapter->action( + 'post', + "/v3/groups.json/{$group}/receivers/upsert", + $subscriptions + ); + } + $this->subscriptions = []; + return $response; + } + /** * {@inheritdoc} */ @@ -131,4 +163,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..7186e73 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 mixed + */ + public function flush(); + /** * Returns a subscriber. * From 8a507c47e880b2c97f6bf6258d0c2032b4a42821 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Fri, 7 Dec 2018 10:46:48 +0100 Subject: [PATCH 2/4] Fix passing arguments. --- src/ApiManager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ApiManager.php b/src/ApiManager.php index c7c7650..fede47b 100644 --- a/src/ApiManager.php +++ b/src/ApiManager.php @@ -36,7 +36,7 @@ public function createSubscriber( array $attributes = [], array $globalAttributes = [] ) { - $subscription = $this->buildSubscribtion($email, $groupId, $active = false, $attributes = [], $globalAttributes); + $subscription = $this->buildSubscribtion($email, $groupId, $active, $attributes, $globalAttributes); return $this->adapter->action( 'post', @@ -55,7 +55,7 @@ public function addCreateSubscriber( array $attributes = [], array $globalAttributes = [] ) { - $subscription = $this->buildSubscribtion($email, $groupId, $active = false, $attributes = [], $globalAttributes); + $subscription = $this->buildSubscribtion($email, $groupId, $active, $attributes, $globalAttributes); if (!isset($this->subscriptions[$subscription['group']])) { $this->subscriptions[$subscription['group']] = []; } From 322f16385554ccb9b5b66ce7bed8b4016ae03ad0 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Fri, 7 Dec 2018 10:47:08 +0100 Subject: [PATCH 3/4] Fix returning flush() response(s). --- src/ApiManager.php | 3 ++- src/ApiManagerInterface.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ApiManager.php b/src/ApiManager.php index fede47b..5e7828e 100644 --- a/src/ApiManager.php +++ b/src/ApiManager.php @@ -67,8 +67,9 @@ public function addCreateSubscriber( */ public function flush() { + $response = []; foreach ($this->subscriptions as $group => $subscriptions) { - $response = $this->adapter->action( + $response[] = $this->adapter->action( 'post', "/v3/groups.json/{$group}/receivers/upsert", $subscriptions diff --git a/src/ApiManagerInterface.php b/src/ApiManagerInterface.php index 7186e73..2a70091 100644 --- a/src/ApiManagerInterface.php +++ b/src/ApiManagerInterface.php @@ -43,7 +43,7 @@ public function addCreateSubscriber( /** * Flushes all batched subscription requests. * - * @return mixed + * @return array */ public function flush(); From a19126a4963b303547b927840814a1c987ebefa5 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Fri, 7 Dec 2018 11:08:06 +0100 Subject: [PATCH 4/4] Add unit test for batch subscriptions. --- src/ApiManager.php | 2 +- tests/ApiTest.php | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/ApiManager.php b/src/ApiManager.php index 5e7828e..88ab958 100644 --- a/src/ApiManager.php +++ b/src/ApiManager.php @@ -69,7 +69,7 @@ public function flush() { $response = []; foreach ($this->subscriptions as $group => $subscriptions) { - $response[] = $this->adapter->action( + $response[$group] = $this->adapter->action( 'post', "/v3/groups.json/{$group}/receivers/upsert", $subscriptions 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 + ); } }