-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from networkteam/feature-fusion-form
Implement SendInBlue fusion form action
- Loading branch information
Showing
7 changed files
with
216 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Wegmeister\SendInBlue\Service\SendInBlueService: | ||
arguments: | ||
1: | ||
setting: 'Wegmeister.SendInBlue.apiKey' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,7 @@ Neos: | |
'Wegmeister.SendInBlue': | ||
- 'NodeTypes/*' | ||
- 'Main' | ||
|
||
Wegmeister: | ||
SendInBlue: | ||
apiKey: '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters