Skip to content

Commit

Permalink
Add source files to the repo
Browse files Browse the repository at this point in the history
  • Loading branch information
mpakhniushchyi committed Oct 7, 2019
1 parent 8e4cdf8 commit ef48e0e
Show file tree
Hide file tree
Showing 9 changed files with 550 additions and 0 deletions.
Binary file added Assets/img/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions Config/config.php
@@ -0,0 +1,36 @@
<?php

return [
'name' => 'Sendinblue integration',
'description' => 'Allows to send E-mails with Sendinblue',
'version' => '1.0',
'author' => 'Dazzle',
'services' => [
'other' => [
'mautic.transport.sendinblue_api' => [
'class' => \MauticPlugin\MauticSendinblueBundle\Swiftmailer\Transport\SendinblueApiTransport::class,
'arguments' => [
'%mautic.mailer_api_key%',
'translator',
'mautic.transport.sendinblue_api.callback',
],
'tags' => [
'mautic.email_transport',
],
'tagArguments' => [
[
'transport_alias' => 'mautic.email.config.mailer_transport.sendinblue',
'field_api_key' => true,
],
],
],
'mautic.transport.sendinblue_api.callback' => [
'class' => \MauticPlugin\MauticSendinblueBundle\Swiftmailer\Callback\SendinblueApiCallback::class,
'arguments' => [
'mautic.email.model.transport_callback',
'monolog.logger.mautic',
],
],
],
],
];
9 changes: 9 additions & 0 deletions MauticSendinblueBundle.php
@@ -0,0 +1,9 @@
<?php

namespace MauticPlugin\MauticSendinblueBundle;

use Mautic\PluginBundle\Bundle\PluginBundleBase;

class MauticSendinblueBundle extends PluginBundleBase
{
}
87 changes: 87 additions & 0 deletions Swiftmailer/Callback/CallbackEnum.php
@@ -0,0 +1,87 @@
<?php

namespace MauticPlugin\MauticSendinblueBundle\Swiftmailer\Callback;

use Mautic\LeadBundle\Entity\DoNotContact;

/**
* Class CallbackEnum.
*/
class CallbackEnum
{

const ERROR = 'error';
const SOFT_BOUNCE = 'soft_bounce';
const HARD_BOUNCE = 'hard_bounce';
const INVALID_EMAIL = 'invalid_email';
const SPAM = 'spam';
const BLOCKED = 'blocked';
const UNSUBSCRIBED = 'unsubscribed';

/**
* Checks if the event have to be processed.
*
* @param string $event
*
* @return bool
*/
public static function shouldBeEventProcessed($event)
{
return in_array($event, self::getSupportedEvents(), true);
}

/**
* Converts an event name to DNC reason.
*
* @param $event
*
* @return string|null
*/
public static function convertEventToDncReason($event)
{
if (!self::shouldBeEventProcessed($event)) {
return null;
}

$mapping = self::eventMappingToDncReason();

return $mapping[$event];
}

/**
* Returns an array of supported Sendinblue events.
*
* @return array
*/
private static function getSupportedEvents()
{
return [
self::ERROR,
self::SOFT_BOUNCE,
self::HARD_BOUNCE,
self::INVALID_EMAIL,
self::SPAM,
self::BLOCKED,
self::UNSUBSCRIBED,
];
}

/**
* Mapping Sendinblue events and DNC reasons.
*
* @return array
*/
private static function eventMappingToDncReason()
{
return [
self::ERROR => DoNotContact::BOUNCED,
self::SOFT_BOUNCE => DoNotContact::BOUNCED,
self::HARD_BOUNCE => DoNotContact::BOUNCED,
self::INVALID_EMAIL => DoNotContact::BOUNCED,
self::SPAM => DoNotContact::BOUNCED,
self::BLOCKED => DoNotContact::BOUNCED,
self::UNSUBSCRIBED => DoNotContact::UNSUBSCRIBED,
];
}

}
76 changes: 76 additions & 0 deletions Swiftmailer/Callback/ResponseItem.php
@@ -0,0 +1,76 @@
<?php

namespace MauticPlugin\MauticSendinblueBundle\Swiftmailer\Callback;

use MauticPlugin\MauticSendinblueBundle\Swiftmailer\Exception\ResponseItemException;

/**
* Class ResponseItem.
*/
class ResponseItem
{

/**
* @var string
*/
private $email;

/**
* @var string
*/
private $reason;

/**
* @var int
*/
private $dncReason;

/**
* ResponseItem constructor.
*
* @param array $item
*
* @throws ResponseItemException
*/
public function __construct(array $item)
{
if (!isset($item['email']) || (isset($item['email']) && empty($item['email']))) {
throw new ResponseItemException('Email must not be empty.');
}

$this->email = is_array($item['email']) ? reset($item['email']) : $item['email'];
$this->reason = isset($item['reason']) ? $item['reason'] : null;
$this->dncReason = CallbackEnum::convertEventToDncReason($item['event']);
}

/**
* Gets an email of the event.
*
* @return string
*/
public function getEmail()
{
return $this->email;
}

/**
* Gets a reason of the event.
*
* @return string
*/
public function getReason()
{
return $this->reason;
}

/**
* Gets DNC reason of the event.
*
* @return int
*/
public function getDncReason()
{
return $this->dncReason;
}

}
58 changes: 58 additions & 0 deletions Swiftmailer/Callback/SendinblueApiCallback.php
@@ -0,0 +1,58 @@
<?php

namespace MauticPlugin\MauticSendinblueBundle\Swiftmailer\Callback;

use Mautic\EmailBundle\Model\TransportCallback;
use MauticPlugin\MauticSendinblueBundle\Swiftmailer\Exception\ResponseItemException;
use Symfony\Component\HttpFoundation\Request;
use Monolog\Logger;

/**
* Class SendinblueApiCallback.
*/
class SendinblueApiCallback
{

/**
* @var TransportCallback
*/
private $transportCallback;

/**
* @var Logger
*/
protected $logger;

/**
* SendinblueApiCallback constructor.
*
* @param TransportCallback $transportCallback
* @param Logger $logger
*/
public function __construct(TransportCallback $transportCallback, Logger $logger)
{
$this->transportCallback = $transportCallback;
$this->logger = $logger;
}

/**
* Processes Sendinblue API callback request.
*
* @param Request $request
*/
public function processCallbackRequest(Request $request)
{
$parameters = $request->request->all();

if (isset($parameters['event']) && CallbackEnum::shouldBeEventProcessed($parameters['event'])) {
try {
$item = new ResponseItem($parameters);
$this->transportCallback->addFailureByAddress($item->getEmail(), $item->getReason(), $item->getDncReason());
}
catch (ResponseItemException $e) {
$this->logger->log('error', $e->getMessage());
}
}
}

}
9 changes: 9 additions & 0 deletions Swiftmailer/Exception/ResponseItemException.php
@@ -0,0 +1,9 @@
<?php

namespace MauticPlugin\MauticSendinblueBundle\Swiftmailer\Exception;

use Exception;

class ResponseItemException extends Exception
{
}

0 comments on commit ef48e0e

Please sign in to comment.