Skip to content

Commit

Permalink
Add Brevo support and deprecate Sendinblue usage
Browse files Browse the repository at this point in the history
  • Loading branch information
gruberro committed Jun 10, 2024
1 parent 5886053 commit 85d2bd3
Show file tree
Hide file tree
Showing 20 changed files with 861 additions and 3 deletions.
3 changes: 2 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('Enable csrf protection for dynamic forms.')
->defaultFalse()
->end()
->scalarNode('sendinblue_api_key')->defaultValue(null)->end()
->scalarNode('sendinblue_api_key')->setDeprecated('sendinblue/api-v3-sdk', '1.0.0', 'The Sendinblue integration is deprecated, use Brevo integration instead (brevo_api_key).')->defaultValue(null)->end()
->scalarNode('brevo_api_key')->defaultValue(null)->end()
->scalarNode('mailchimp_api_key')->defaultValue(null)->end()
->scalarNode('mailchimp_subscribe_status')->defaultValue('subscribed')->end()
->enumNode('media_collection_strategy')
Expand Down
8 changes: 8 additions & 0 deletions DependencyInjection/SuluFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('type_sendinblue.xml');
}

if ($config['brevo_api_key']) {
if (!\class_exists(\Brevo\Client\Configuration::class)) {
throw new \LogicException('You need to install the "getbrevo/brevo-php" package to use the Brevo type.');
}

$loader->load('type_brevo.xml');
}

if ($config['mailchimp_api_key']) {
if (!\class_exists(\DrewM\MailChimp\MailChimp::class)) {
throw new \LogicException('You need to install the "drewm/mailchimp-api" package to use the mailchimp type.');
Expand Down
84 changes: 84 additions & 0 deletions Dynamic/Helper/BrevoListSelect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\FormBundle\Dynamic\Helper;

use Brevo\Client\Api\ContactsApi;
use Brevo\Client\Configuration;

/**
* @final
*
* @internal
*/
class BrevoListSelect
{
/**
* @var ContactsApi|null
*/
private $contactsApi;

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

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

$this->contactsApi = new ContactsApi(null, $config);
}

/**
* Returns array of Brevo lists of given account defined by the API key.
*
* @return mixed[]
*/
public function getValues(): array
{
if (!$this->contactsApi) {
return [];
}

$limit = 50;
$offset = 0;
$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 = [];

foreach ($listObjects as $list) {
$lists[] = [
'name' => $list['id'],
'title' => $list['name'],
];
}

return $lists;
}
}
88 changes: 88 additions & 0 deletions Dynamic/Helper/BrevoMailTemplateSelect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\FormBundle\Dynamic\Helper;

use Brevo\Client\Api\TransactionalEmailsApi;
use Brevo\Client\Configuration;

/**
* @final
*
* @internal
*/
class BrevoMailTemplateSelect
{
/**
* @var TransactionalEmailsApi|null
*/
private $transactionalEmailsApi;

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

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

$this->transactionalEmailsApi = new TransactionalEmailsApi(null, $config);
}

/**
* Returns array of Brevo mail templates of given account defined by the API key.
*
* @return mixed[]
*/
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 = [];

foreach ($mailTemplateObjects as $template) {
if (($template['tag'] ?? null) !== 'optin') {
continue;
}

$mailTemplates[] = [
'name' => $template['id'],
'title' => $template['name'],
];
}

return $mailTemplates;
}
}
2 changes: 2 additions & 0 deletions Dynamic/Helper/SendinblueListSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @final
*
* @internal
*
* @deprecated
*/
class SendinblueListSelect
{
Expand Down
2 changes: 2 additions & 0 deletions Dynamic/Helper/SendinblueMailTemplateSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @final
*
* @internal
*
* @deprecated
*/
class SendinblueMailTemplateSelect
{
Expand Down
44 changes: 44 additions & 0 deletions Dynamic/Types/BrevoType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\FormBundle\Dynamic\Types;

use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeConfiguration;
use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeInterface;
use Sulu\Bundle\FormBundle\Entity\FormField;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType as TypeCheckboxType;
use Symfony\Component\Form\FormBuilderInterface;

/**
* The Brevo form field type.
*/
class BrevoType implements FormFieldTypeInterface
{
public function getConfiguration(): FormFieldTypeConfiguration
{
return new FormFieldTypeConfiguration(
'sulu_form.type.brevo',
__DIR__ . '/../../Resources/config/form-fields/field_brevo.xml',
'special'
);
}

public function build(FormBuilderInterface $builder, FormField $field, string $locale, array $options): void
{
$type = TypeCheckboxType::class;
$builder->add($field->getKey(), $type, $options);
}

public function getDefaultValue(FormField $field, string $locale)
{
return $field->getTranslation($locale)->getDefaultValue();
}
}
2 changes: 2 additions & 0 deletions Dynamic/Types/SendinblueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

/**
* The Sendinblue form field type.
*
* @deprecated
*/
class SendinblueType implements FormFieldTypeInterface
{
Expand Down
Loading

0 comments on commit 85d2bd3

Please sign in to comment.