Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pagination handling for Sendinblue requests #317

Merged
merged 2 commits into from
Feb 8, 2022
Merged
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
32 changes: 30 additions & 2 deletions Dynamic/Helper/SendinblueListSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
class SendinblueListSelect
{
/**
* @var ContactsApi
* @var ContactsApi|null
*/
private $contactsApi;

public function __construct(?string $apiKey)
{
if (!$apiKey) {
return;
}

$config = new Configuration();
$config->setApiKey('api-key', $apiKey);

Expand All @@ -41,9 +45,33 @@ public function __construct(?string $apiKey)
*/
public function getValues(): array
{
if (!$this->contactsApi) {
return [];
}

$limit = 50;
$offset = 0;
Copy link
Contributor

@niklasnatter niklasnatter Feb 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you could use count($listObjects) instead of storing this in a separate variable. but dont have a strong opionion here 🙂

$total = null;
$listObjects = [];

do {
$response = $this->contactsApi->getLists($limit, $offset);

if (null === $total) {
$total = $response->getCount();
}

$newListObjects = $response->getLists();
if (0 === \count($newListObjects)) {
break;
}

$listObjects = array_merge($listObjects, $newListObjects);
$offset += $limit;
} while (count($listObjects) < $total);

$lists = [];

$listObjects = $this->contactsApi->getLists()->getLists();
foreach ($listObjects as $list) {
$lists[] = [
'name' => $list['id'],
Expand Down
32 changes: 30 additions & 2 deletions Dynamic/Helper/SendinblueMailTemplateSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
class SendinblueMailTemplateSelect
{
/**
* @var TransactionalEmailsApi
* @var TransactionalEmailsApi|null
*/
private $transactionalEmailsApi;

public function __construct(?string $apiKey)
{
if (!$apiKey) {
return;
}

$config = new Configuration();
$config->setApiKey('api-key', $apiKey);

Expand All @@ -41,9 +45,33 @@ public function __construct(?string $apiKey)
*/
public function getValues(): array
{
if (!$this->transactionalEmailsApi) {
return [];
}

$limit = 50;
$offset = 0;
$total = null;
$mailTemplateObjects = [];

do {
$response = $this->transactionalEmailsApi->getSmtpTemplates('true', $limit, $offset);

if (null === $total) {
$total = $response->getCount();
}

$newMailTemplateObjects = $response->getTemplates();
if (0 === \count($newMailTemplateObjects)) {
break;
}

$mailTemplateObjects = array_merge($mailTemplateObjects, $newMailTemplateObjects);
$offset += $limit;
} while (count($mailTemplateObjects) < $total);

$mailTemplates = [];

$mailTemplateObjects = $this->transactionalEmailsApi->getSmtpTemplates('true')->getTemplates();
foreach ($mailTemplateObjects as $template) {
if (($template['tag'] ?? null) !== 'optin') {
continue;
Expand Down
10 changes: 9 additions & 1 deletion Event/SendinblueListSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ class SendinblueListSubscriber implements EventSubscriberInterface
private $requestStack;

/**
* @var ContactsApi
* @var ContactsApi|null
*/
private $contactsApi;

public function __construct(RequestStack $requestStack, ?string $apiKey)
{
$this->requestStack = $requestStack;

if (!$apiKey) {
return;
}

$config = new Configuration();
$config->setApiKey('api-key', $apiKey);

Expand All @@ -57,6 +61,10 @@ public static function getSubscribedEvents()

public function listSubscribe(FormSavePostEvent $event): void
{
if (!$this->contactsApi) {
return;
}

$dynamic = $event->getData();
$request = $this->requestStack->getCurrentRequest();

Expand Down
15 changes: 0 additions & 15 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,6 @@ parameters:
count: 1
path: Dynamic/Helper/SendinblueListSelect.php

-
message: "#^Parameter \\#2 \\$key of method SendinBlue\\\\Client\\\\Configuration\\:\\:setApiKey\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: Dynamic/Helper/SendinblueListSelect.php

-
message: "#^Cannot assign offset 'tag' to SendinBlue\\\\Client\\\\Model\\\\GetSmtpTemplateOverview\\.$#"
count: 1
Expand All @@ -420,11 +415,6 @@ parameters:
count: 1
path: Dynamic/Helper/SendinblueMailTemplateSelect.php

-
message: "#^Parameter \\#2 \\$key of method SendinBlue\\\\Client\\\\Configuration\\:\\:setApiKey\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: Dynamic/Helper/SendinblueMailTemplateSelect.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
Expand Down Expand Up @@ -1060,11 +1050,6 @@ parameters:
count: 4
path: Event/SendinblueListSubscriber.php

-
message: "#^Parameter \\#2 \\$key of method SendinBlue\\\\Client\\\\Configuration\\:\\:setApiKey\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: Event/SendinblueListSubscriber.php

-
message: "#^Cannot access offset 'checksum' on array\\|bool\\|float\\|int\\|string\\.$#"
count: 1
Expand Down