Skip to content

Commit

Permalink
Merge pull request #3 from networkteam/feature-fusion-form
Browse files Browse the repository at this point in the history
Implement SendInBlue fusion form action
  • Loading branch information
Benjamin-K authored Apr 21, 2023
2 parents 885718a + 5bbd210 commit 11649e4
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 28 deletions.
92 changes: 92 additions & 0 deletions Classes/Action/SendInBlueAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
namespace Wegmeister\SendInBlue\Action;

/*
* This file is part of the Wegmeister.Form.FormElements package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Flow\Mvc\ActionResponse;
use Neos\Fusion\Form\Runtime\Action\AbstractAction;
use Neos\Fusion\Form\Runtime\Domain\Exception\ActionException;
use Wegmeister\SendInBlue\Service\SendInBlueService;
use Neos\Flow\Annotations as Flow;

class SendInBlueAction extends AbstractAction
{

/**
* @var SendInBlueService
* @Flow\Inject
*/
protected $sendInBlueService;

protected $options = [
'apiKey' => '',
'includeListIds' => [],
'templateId' => null,
'redirectionUrl' => '',
'email' => '',
'attributes' => []
];

public function perform(): ?ActionResponse
{
$apiKey = $this->options['apiKey'];
$includeListIds = $this->options['includeListIds'];
$templateId = $this->options['templateId'];
$redirectionUrl = $this->options['redirectionUrl'];
$email = $this->options['email'];
$attributes = $this->options['attributes'] ?? [];

// override via settings configured api key
if (!empty($apiKey)) {
$this->sendInBlueService->setApiKey($apiKey);
}
if (empty($includeListIds)) {
throw new ActionException(
'The option "includeListIds" must be set for the SendInBlueAction.',
1680709674
);
}
if (!is_int($templateId)) {
throw new ActionException(
'The option "templateId" must be set with integer value for the SendInBlueAction.',
1680709675
);
}
if ($email === null || trim($email) === '') {
throw new ActionException(
'The option "email" must be set for the SendInBlueAction.',
1680709676
);
}
if (!is_array($includeListIds)) {
$includeListIds = [$includeListIds];
}
$includeListIds = array_map('intval', $includeListIds);

try {
$this->sendInBlueService->createDoiContact(
$email,
$attributes,
$includeListIds,
(int)$templateId,
$redirectionUrl
);
} catch (\Exception $exception) {
$response = new ActionResponse();
$response->setContent(
$exception->getMessage()
);
return $response;
}

return null;
}
}
17 changes: 17 additions & 0 deletions Classes/Exception/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Wegmeister\SendInBlue\Exception;

/*
* This file is part of the Wegmeister.Form.FormElements package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

class Exception extends \Exception
{

}
48 changes: 20 additions & 28 deletions Classes/Finishers/SendInBlueFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@

use Neos\Form\Core\Model\AbstractFinisher;
use Neos\Form\Exception\FinisherException;
use SendinBlue\Client\Configuration as SendinBlueConfiguration;
use SendinBlue\Client\Api\ContactsApi;
use SendinBlue\Client\Model\CreateDoiContact;
use Wegmeister\SendInBlue\Exception\Exception;
use Wegmeister\SendInBlue\Service\SendInBlueService;
use Neos\FLow\Annotations as Flow;

/**
* This finisher adds new contacts to a SendInBlue group
*/
class SendInBlueFinisher extends AbstractFinisher
{

/**
* @var SendInBlueService
* @Flow\Inject
*/
protected $sendInBlueService;

/**
* Executes this finisher
* @see AbstractFinisher::execute()
*
* @return void
* @throws FinisherException
* @throws Exception
* @throws FinisherException|Exception
*/
protected function executeInternal()
{
Expand All @@ -39,6 +44,8 @@ protected function executeInternal()

if ($apiKey === null || $apiKey === '') {
throw new FinisherException('The option "apiKey" must be set for the SendInBlueFinisher.', 1603986560);
} else {
$this->sendInBlueService->setApiKey($apiKey);
}
if (empty($includeListIds)) {
throw new FinisherException(
Expand Down Expand Up @@ -86,27 +93,12 @@ protected function executeInternal()
$formRuntime = $this->finisherContext->getFormRuntime();
$referrer = $formRuntime->getRequest()->getHttpRequest()->getUri()->__toString();

$config = SendinBlueConfiguration::getDefaultConfiguration()->setApiKey('api-key', $apiKey);

$apiInstance = new ContactsApi(
new \GuzzleHttp\Client(),
$config
$this->sendInBlueService->createDoiContact(
$email,
$formValues,
$includeListIds,
(int)$templateId,
$redirectionUrl ?: $referrer
);

$newContact = new CreateDoiContact();
$newContact['email'] = $email;
$newContact['attributes'] = $formValues;
$newContact['includeListIds'] = $includeListIds;
$newContact['templateId'] = (int)$templateId;
$newContact['redirectionUrl'] = $redirectionUrl ?: $referrer;

try {
$apiInstance->createDoiContact($newContact);
} catch (\Exception $e) {
throw new FinisherException(
'Could not create new SendinBlue contact. API-Exception: ' . $e->getMessage(),
1603988495
);
}
}
}
76 changes: 76 additions & 0 deletions Classes/Service/SendInBlueService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
namespace Wegmeister\SendInBlue\Service;

/*
* This file is part of the Wegmeister.Form.FormElements package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use SendinBlue\Client\Api\ContactsApi;
use SendinBlue\Client\Configuration as SendinBlueConfiguration;
use SendinBlue\Client\Model\CreateDoiContact;
use Wegmeister\SendInBlue\Exception\Exception;

class SendInBlueService
{

/**
* @var \SendinBlue\Client\Api\ContactsApi
*/
protected $contactsApi;

protected string $apiKey;

public function __construct(string $apiKey)
{
$this->setApiKey($apiKey);
}

public function setApiKey(string $apiKey): void
{
$config = SendinBlueConfiguration::getDefaultConfiguration()->setApiKey('api-key', $apiKey);
$this->contactsApi = new ContactsApi(
new \GuzzleHttp\Client(),
$config
);
}

/**
* @param string $email Email address where the confirmation email will be sent. This email address will be the identifier for all other contact attributes.
* @param array $attributes Pass the set of attributes and their values. These attributes must be present in your SendinBlue account. For eg. {'FNAME':'Elly', 'LNAME':'Roger'}
* @param array $includeListIds Lists under user account where contact should be added
* @param int $templateId ID of the Double opt-in (DOI) template
* @param string $redirectionUrl URL of the web page that user will be redirected to after clicking on the double opt in URL. When editing your DOI template you can reference this URL by using the tag {{ params.DOIurl }}.
* @throws Exception
*/
public function createDoiContact(
string $email,
array $attributes,
array $includeListIds,
int $templateId,
string $redirectionUrl
): void
{
$newContact = new CreateDoiContact([
'email' => $email,
'attributes' => empty($attributes) ? null: $attributes,
'includeListIds' => $includeListIds,
'templateId' => $templateId,
'redirectionUrl' => $redirectionUrl
]);

try {
$this->contactsApi->createDoiContact($newContact);
} catch (\Exception $e) {
throw new Exception(
'Could not create new SendInBlue contact. API-Exception: ' . $e->getMessage(),
1680711227
);
}
}
}
4 changes: 4 additions & 0 deletions Configuration/Objects.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Wegmeister\SendInBlue\Service\SendInBlueService:
arguments:
1:
setting: 'Wegmeister.SendInBlue.apiKey'
4 changes: 4 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ Neos:
'Wegmeister.SendInBlue':
- 'NodeTypes/*'
- 'Main'

Wegmeister:
SendInBlue:
apiKey: ''
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
]
}
}
},
"suggest": {
"neos/fusion-form": "Needed when using \\Wegmeister\\SendInBlue\\FusionForm\\Action\\SendInBlueAction"
}
}

0 comments on commit 11649e4

Please sign in to comment.