You are here

class SendinblueApiV3 in SendinBlue 8

Same name and namespace in other branches
  1. 8.2 src/Tools/Api/SendinblueApiV3.php \Drupal\sendinblue\Tools\Api\SendinblueApiV3

Sendinblue REST client.

Hierarchy

Expanded class hierarchy of SendinblueApiV3

1 file declares its use of SendinblueApiV3
SendinblueManager.php in src/SendinblueManager.php
1 string reference to 'SendinblueApiV3'
sendinblue.services.yml in ./sendinblue.services.yml
sendinblue.services.yml
1 service uses SendinblueApiV3
sendinblue.api.v3 in ./sendinblue.services.yml
Drupal\sendinblue\Tools\Api\SendinblueApiV3

File

src/Tools/Api/SendinblueApiV3.php, line 39

Namespace

Drupal\sendinblue\Tools\Api
View source
class SendinblueApiV3 implements SendInBlueApiInterface {

  /**
   * Sib ApiKey.
   *
   * @var string
   */
  public $apiKey;

  /**
   * Logger Service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactory
   *   LoggerChannelFactory.
   */
  protected $loggerFactory;

  /**
   * Get the AccountApi from SendInBlue.
   *
   * @var \SendinBlue\Client\Api\AccountApi
   *   AccountApi.
   */
  private $sibAccountApi;

  /**
   * Get the EmailCampaignsApi from SendInBlue.
   *
   * @var \SendinBlue\Client\Api\EmailCampaignsApi
   *   EmailCampaignsApi.
   */
  private $sibEmailCampaignsApi;

  /**
   * Get the ListsApi from SendInBlue.
   *
   * @var \SendinBlue\Client\Api\ListsApi
   *   ListsApi.
   */
  private $sibListsApi;

  /**
   * Get the ContactsApi from SendInBlue.
   *
   * @var \SendinBlue\Client\Api\ContactsApi
   *   ContactsApi.
   */
  private $sibContactsApi;

  /**
   * Get the AttributesApi from SendInBlue.
   *
   * @var \SendinBlue\Client\Api\AttributesApi
   *   AttributesApi.
   */
  private $sibAttributesApi;

  /**
   * Get the SendersApi from SendInBlue.
   *
   * @var \SendinBlue\Client\Api\SendersApi
   *   SendersApi.
   */
  private $sibSendersApi;

  /**
   * Get the SMTPApi from SendInBlue.
   *
   * @var \SendinBlue\Client\Api\TransactionalEmailsApi
   *   TransactionalEmailsApi
   */
  private $sibTransactionalEmailsApi;

  /**
   * Get the Configuration from SendInBlue.
   *
   * @var \SendinBlue\Client\Configuration
   *   Configuration.
   */
  private $sibClientConfiguration;

  /**
   * Get the SendinblueHttpClient for HTTP cURL.
   *
   * @var \Drupal\sendinblue\Tools\Http\SendinblueHttpClient
   *   SendinblueHttpClient.
   */
  private $sIBHttpClient;

  /**
   * Get the ResellerApi from SendInBlue.
   *
   * @var \SendinBlue\Client\Api\ResellerApi
   *   ResellerApi.
   */
  private $sibResellerApi;

  /**
   * SendinblueMailin constructor.
   *
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   LoggerChannelFactory.
   * @param \Drupal\sendinblue\Tools\Http\SendinblueHttpClient $sIBHttpClient
   *   SendinblueHttpClient.
   */
  public function __construct(LoggerChannelFactoryInterface $logger_factory, SendinblueHttpClient $sIBHttpClient) {
    $sibClientConfiguration = Configuration::getDefaultConfiguration();
    $this->sibClientConfiguration = $sibClientConfiguration
      ->setApiKey("api-key", $this->apiKey);
    $this->sibAccountApi = new AccountApi(NULL, $this->sibClientConfiguration);
    $this->sibEmailCampaignsApi = new EmailCampaignsApi(NULL, $this->sibClientConfiguration);
    $this->sibListsApi = new ListsApi(NULL, $this->sibClientConfiguration);
    $this->sibContactsApi = new ContactsApi(NULL, $this->sibClientConfiguration);
    $this->sibAttributesApi = new AttributesApi(NULL, $this->sibClientConfiguration);
    $this->sibSendersApi = new SendersApi(NULL, $this->sibClientConfiguration);
    $this->sibTransactionalEmailsApi = new TransactionalEmailsApi(NULL, $this->sibClientConfiguration);
    $this->sibResellerApi = new ResellerApi(NULL, $this->sibClientConfiguration);
    $this->loggerFactory = $logger_factory;
    $this->sIBHttpClient = $sIBHttpClient;
  }

  /**
   * {@inheritdoc}
   */
  public function setApiKey($apiKey) {
    $this->apiKey = $apiKey;
    $this->sibClientConfiguration
      ->setApiKey("api-key", $this->apiKey);
  }

  /**
   * Get SendInBlueV3 Configuration.
   *
   * @return \SendinBlue\Client\Configuration
   *   SendInBlueV3 Configuration
   */
  public function getSibClientConfiguration() {
    return $this->sibClientConfiguration;
  }

  /**
   * {@inheritdoc}
   */
  public function getAccount() {
    $account = $this->sibAccountApi
      ->getAccount();
    return new GetAccount(Json::decode($account));
  }

  /**
   * {@inheritdoc}
   */
  public function getTemplates() {
    $templates = $this->sibTransactionalEmailsApi
      ->getSmtpTemplates();
    if ($templates && !empty($templates['templates'])) {
      return new GetSmtpTemplates(Json::decode($templates));
    }
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getTemplate($id) {
    return new GetSmtpTemplateOverview(Json::decode($this->sibTransactionalEmailsApi
      ->getSmtpTemplate($id)));
  }

  /**
   * {@inheritdoc}
   */
  public function getLists() {
    return new GetLists($this->sibListsApi
      ->getLists('50')
      ->getLists());
  }

  /**
   * {@inheritdoc}
   */
  public function getList($id) {
    $list = $this->sibListsApi
      ->getList($id);
    return new GetExtendedList(Json::decode($list));
  }

  /**
   * {@inheritdoc}
   */
  public function sendEmail(array $to, string $subject, string $html, string $text, array $from = [], array $replyto = [], array $cc = [], array $bcc = [], array $attachment = [], array $headers = []) {
    $to += [
      'email' => NULL,
      'name' => NULL,
    ];
    $sibTo = new SendSmtpEmailTo([
      'email' => $to['email'],
      'name' => $to['name'],
    ]);
    $emailData = [
      'to' => [
        $sibTo,
      ],
      'htmlContent' => $html,
      'textContent' => $text,
      'subject' => $subject,
    ];
    if (!empty($from)) {
      $from += [
        'email' => NULL,
        'name' => NULL,
      ];
      $emailData['sender'] = new SendSmtpEmailSender([
        'email' => $from['email'],
        'name' => $from['name'],
      ]);
    }
    if (!empty($bcc)) {
      $bcc += [
        'email' => NULL,
        'name' => NULL,
      ];
      $emailData['bcc'] = [
        new SendSmtpEmailBcc([
          'email' => $bcc['email'],
          'name' => $bcc['name'],
        ]),
      ];
    }
    if (!empty($cc)) {
      $cc += [
        'email' => NULL,
        'name' => NULL,
      ];
      $emailData['cc'] = [
        new SendSmtpEmailCc([
          'email' => $cc['email'],
          'name' => $cc['name'],
        ]),
      ];
    }
    if (!empty($replyto)) {
      $replyto += [
        'email' => NULL,
        'name' => NULL,
      ];
      $emailData['replyTo'] = new SendSmtpEmailReplyTo([
        'email' => $replyto['email'],
        'name' => $replyto['name'],
      ]);
    }
    if (!empty($headers)) {
      $emailData['headers'] = $headers;
    }
    if (!empty($attachment)) {
      $attachments = [];
      foreach ($attachment as $item) {
        $attachments[] = new SendSmtpEmailAttachment($item);
      }
      $emailData['attachment'] = $attachments;
    }
    $message = $this->sibTransactionalEmailsApi
      ->sendTransacEmail(new SendSmtpEmail($emailData));
    if ($message
      ->valid()) {
      return new CreateSmtpEmail($message
        ->getMessageId());
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getUser($email) {
    try {
      $contactInfo = $this->sibContactsApi
        ->getContactInfo($email);
      return new GetExtendedContactDetails(Json::decode($contactInfo));
    } catch (\Throwable $e) {
      return NULL;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function createUpdateUser($email, array $attributes = [], array $blacklisted = [], $listid = '', $listid_unlink = '') {
    $isContactExist = $this
      ->getUser($email) !== NULL;
    if ($isContactExist) {
      $updateContact = new UpdateContact([
        'attributes' => (object) $attributes,
        'emailBlacklisted' => (bool) $blacklisted,
        'listIds' => [
          (int) $listid[0],
        ],
        'unlinkListIds' => $listid_unlink,
      ]);
      $this->sibContactsApi
        ->updateContact($email, $updateContact);
    }
    else {
      $updateContact = new CreateContact([
        'email' => $email,
        'attributes' => (object) $attributes,
        'emailBlacklisted' => (bool) $blacklisted,
        'listIds' => [
          (int) $listid[0],
        ],
        'unlinkListIds' => $listid_unlink,
      ]);
      $this->sibContactsApi
        ->createContact($updateContact);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getAttributes() {
    return new GetAttributes(Json::decode($this->sibAttributesApi
      ->getAttributes()));
  }

  /**
   * {@inheritdoc}
   */
  public function getAccessTokens() {
    return $this->sibAccountApi
      ->getConfig()
      ->getAccessToken();
  }

  /**
   * {@inheritdoc}
   */
  public function getSmtpDetails() {
    $smtpDetails = $this->sibAccountApi
      ->getAccount()
      ->getRelay();
    if ($smtpDetails) {
      return new GetSmtpDetails($smtpDetails
        ->getData()
        ->getUserName(), $smtpDetails
        ->getData()
        ->getRelay(), $smtpDetails
        ->getData()
        ->getPort(), (bool) $smtpDetails
        ->getEnabled());
    }
    return null;
  }

  /**
   * {@inheritdoc}
   */
  public function countUserlists(array $listids = []) {
    $total = 0;
    foreach ($listids as $listid) {
      $userList = $this->sibListsApi
        ->getContactsFromList($listid);
      $total += $userList
        ->getCount();
    }
    return $total;
  }

  /**
   * {@inheritdoc}
   */
  public function partnerDrupal() {
    $data = [];
    $data['key'] = $this->apiKey;
    $data['webaction'] = 'MAILIN-PARTNER';
    $data['partner'] = 'DRUPAL';
    $data['source'] = 'Drupal';
    $options = [
      'headers' => [
        'Content-Type' => 'application/json',
      ],
      'form_params' => $data,
      'verify' => FALSE,
    ];
    return $this->sIBHttpClient
      ->doRequestDirect(SendinblueHttpClient::WEBHOOK_WS_SIB_URL, 'POST', $data, $options);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SendinblueApiV3::$apiKey public property Sib ApiKey.
SendinblueApiV3::$loggerFactory protected property Logger Service.
SendinblueApiV3::$sibAccountApi private property Get the AccountApi from SendInBlue.
SendinblueApiV3::$sibAttributesApi private property Get the AttributesApi from SendInBlue.
SendinblueApiV3::$sibClientConfiguration private property Get the Configuration from SendInBlue.
SendinblueApiV3::$sibContactsApi private property Get the ContactsApi from SendInBlue.
SendinblueApiV3::$sibEmailCampaignsApi private property Get the EmailCampaignsApi from SendInBlue.
SendinblueApiV3::$sIBHttpClient private property Get the SendinblueHttpClient for HTTP cURL.
SendinblueApiV3::$sibListsApi private property Get the ListsApi from SendInBlue.
SendinblueApiV3::$sibResellerApi private property Get the ResellerApi from SendInBlue.
SendinblueApiV3::$sibSendersApi private property Get the SendersApi from SendInBlue.
SendinblueApiV3::$sibTransactionalEmailsApi private property Get the SMTPApi from SendInBlue.
SendinblueApiV3::countUserlists public function Count all users of lists. Overrides SendInBlueApiInterface::countUserlists
SendinblueApiV3::createUpdateUser public function Create and update user. Overrides SendInBlueApiInterface::createUpdateUser
SendinblueApiV3::getAccessTokens public function Get the access token. Overrides SendInBlueApiInterface::getAccessTokens
SendinblueApiV3::getAccount public function Get the details of an account. Overrides SendInBlueApiInterface::getAccount
SendinblueApiV3::getAttributes public function Get attribute by type. Overrides SendInBlueApiInterface::getAttributes
SendinblueApiV3::getList public function Get list by id. Overrides SendInBlueApiInterface::getList
SendinblueApiV3::getLists public function Get lists of an account. Overrides SendInBlueApiInterface::getLists
SendinblueApiV3::getSibClientConfiguration public function Get SendInBlueV3 Configuration.
SendinblueApiV3::getSmtpDetails public function Get the details of smtp. Overrides SendInBlueApiInterface::getSmtpDetails
SendinblueApiV3::getTemplate public function Get template by id. Overrides SendInBlueApiInterface::getTemplate
SendinblueApiV3::getTemplates public function Get templates. Overrides SendInBlueApiInterface::getTemplates
SendinblueApiV3::getUser public function Get user by email. Overrides SendInBlueApiInterface::getUser
SendinblueApiV3::partnerDrupal public function Add the Partner's name in sendinblue. Overrides SendInBlueApiInterface::partnerDrupal
SendinblueApiV3::sendEmail public function Send email via sendinblue. Overrides SendInBlueApiInterface::sendEmail
SendinblueApiV3::setApiKey public function Set the SendinBlue APIKEY. Overrides SendInBlueApiInterface::setApiKey
SendinblueApiV3::__construct public function SendinblueMailin constructor.