diff --git a/.gitignore b/.gitignore index 4aaba6a..0ca7abd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ bin vendor docs +composer.lock diff --git a/spec/Subscriber/ListRepositorySpec.php b/spec/Subscriber/ListRepositorySpec.php index bcefd80..45daf2b 100644 --- a/spec/Subscriber/ListRepositorySpec.php +++ b/spec/Subscriber/ListRepositorySpec.php @@ -84,12 +84,68 @@ function it_finds_merge_tags(MailChimp $mailchimp) 'merge_fields' => [ ['tag' => 'EMAIL', 'name' => 'email'], ['tag' => 'FOO', 'name' => 'Bar'], - ] + ], + 'total_items' => 2 ]); $this->getMergeFields(123)->shouldReturn([['tag' => 'EMAIL', 'name' => 'email'], ['tag' => 'FOO', 'name' => 'Bar']]); } + function it_finds_merge_tags_more_than_10(MailChimp $mailchimp) + { + $mailchimp + ->get("lists/123/merge-fields") + ->willReturn( + [ + 'merge_fields' => [ + ['tag' => 'FOO1', 'name' => 'Bar'], + ['tag' => 'FOO2', 'name' => 'Bar'], + ['tag' => 'FOO3', 'name' => 'Bar'], + ['tag' => 'FOO4', 'name' => 'Bar'], + ['tag' => 'FOO5', 'name' => 'Bar'], + ['tag' => 'FOO6', 'name' => 'Bar'], + ['tag' => 'FOO8', 'name' => 'Bar'], + ['tag' => 'FOO9', 'name' => 'Bar'], + ['tag' => 'FOO10', 'name' => 'Bar'], + ], + 'total_items' => 13 + ]); + + $mailchimp + ->get("lists/123/merge-fields", array("count" => 13)) + ->willReturn( + [ + 'merge_fields' => [ + ['tag' => 'FOO1', 'name' => 'Bar'], + ['tag' => 'FOO2', 'name' => 'Bar'], + ['tag' => 'FOO3', 'name' => 'Bar'], + ['tag' => 'FOO4', 'name' => 'Bar'], + ['tag' => 'FOO5', 'name' => 'Bar'], + ['tag' => 'FOO6', 'name' => 'Bar'], + ['tag' => 'FOO8', 'name' => 'Bar'], + ['tag' => 'FOO9', 'name' => 'Bar'], + ['tag' => 'FOO10', 'name' => 'Bar'], + ['tag' => 'FOO12', 'name' => 'Bar'], + ['tag' => 'FOO13', 'name' => 'Bar'], + ], + 'total_items' => 13 + ]); + + $this->getMergeFields(123)->shouldReturn([ + ['tag' => 'FOO1', 'name' => 'Bar'], + ['tag' => 'FOO2', 'name' => 'Bar'], + ['tag' => 'FOO3', 'name' => 'Bar'], + ['tag' => 'FOO4', 'name' => 'Bar'], + ['tag' => 'FOO5', 'name' => 'Bar'], + ['tag' => 'FOO6', 'name' => 'Bar'], + ['tag' => 'FOO8', 'name' => 'Bar'], + ['tag' => 'FOO9', 'name' => 'Bar'], + ['tag' => 'FOO10', 'name' => 'Bar'], + ['tag' => 'FOO12', 'name' => 'Bar'], + ['tag' => 'FOO13', 'name' => 'Bar'], + ]); + } + function it_deletes_a_merge_tag(MailChimp $mailchimp) { $mailchimp->delete("lists/123/merge-fields/foo")->shouldBeCalled(); diff --git a/src/Subscriber/ListRepository.php b/src/Subscriber/ListRepository.php index 3319003..9fe0ca0 100644 --- a/src/Subscriber/ListRepository.php +++ b/src/Subscriber/ListRepository.php @@ -7,6 +7,7 @@ class ListRepository { const SUBSCRIBER_BATCH_SIZE = 300; + const MAILCHIMP_DEFAULT_COUNT_LIMIT = 10; public function __construct(MailChimp $mailchimp) { @@ -317,6 +318,11 @@ public function getMergeFields($listId) { $result = $this->mailchimp->get("lists/$listId/merge-fields"); + # Handle mailchimp default count limit + if($result['total_items'] > self::MAILCHIMP_DEFAULT_COUNT_LIMIT){ + $result = $this->mailchimp->get("lists/$listId/merge-fields", array("count" => $result['total_items'])); + } + if(!$this->mailchimp->success()){ throw new \RuntimeException($this->mailchimp->getLastError()); }