View source
<?php
namespace Drupal\sendinblue\Tools\Api;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\sendinblue\Tools\Http\SendinblueHttpClient;
use Drupal\sendinblue\Tools\Model\CreateSmtpEmail;
use Drupal\sendinblue\Tools\Model\GetAccount;
use Drupal\sendinblue\Tools\Model\GetAttributes;
use Drupal\sendinblue\Tools\Model\GetExtendedContactDetails;
use Drupal\sendinblue\Tools\Model\GetExtendedList;
use Drupal\sendinblue\Tools\Model\GetLists;
use Drupal\sendinblue\Tools\Model\GetSmtpDetails;
use Drupal\sendinblue\Tools\Model\GetSmtpTemplates;
class SendinblueApiV2 implements SendInBlueApiInterface {
const API_URL = 'https://api.sendinblue.com/v2.0';
public $apiKey;
public $baseUrl;
public $sIBHttpClient;
protected $loggerFactory;
public function __construct(LoggerChannelFactoryInterface $loggerFactory, SendinblueHttpClient $sIBHttpClient) {
$this->loggerFactory = $loggerFactory;
$this->sIBHttpClient = $sIBHttpClient;
$this->sIBHttpClient
->setApiKey($this->apiKey);
$this->sIBHttpClient
->setBaseUrl(self::API_URL);
}
public function setApiKey($apiKey) {
$this->apiKey = $apiKey;
$this->sIBHttpClient
->setApiKey($this->apiKey);
}
public function getAccount() {
$account = $this->sIBHttpClient
->get("account", "");
if ($account) {
$accountData = $account['data'];
return new GetAccount([
'firstName' => $accountData[2]['first_name'],
'lastName' => $accountData[2]['last_name'],
'email' => $accountData[2]['email'],
'companyName' => $accountData[2]['company'],
'address' => [
'city' => $accountData[2]['city'],
'zipCode' => $accountData[2]['zip_code'],
'country' => $accountData[2]['country'],
'street' => $accountData[2]['address'],
],
'plan' => [
[
'type' => $accountData[0]['plan_type'],
'credits' => $accountData[0]['credits'],
'creditsType' => $accountData[0]['credit_type'],
],
[
'type' => $accountData[1]['plan_type'],
'credits' => $accountData[1]['credits'],
'creditsType' => $accountData[1]['credit_type'],
],
],
]);
}
return null;
}
public function getTemplates() {
$response = $this->sIBHttpClient
->get("campaign/detailsv2", Json::encode([
"type" => 'template',
]));
$templates = [];
if ($response['code'] === 'success' && is_array($response['data'])) {
foreach ($response['data']['campaign_records'] as $template) {
$templates[] = [
'id' => $template['id'],
'name' => $template['campaign_name'],
'subject' => $template['subject'],
'htmlContent' => $template['html_content'],
'sender' => [
'email' => $template['from_email'],
'name' => $template['from_name'],
],
];
}
}
return new GetSmtpTemplates([
'templates' => $templates,
]);
}
public function getTemplate($id) {
$templates = $this
->getTemplates();
if ($templates) {
foreach ($templates
->getTemplates() as $template) {
if ($template
->getId() === $id) {
return $template;
}
}
}
return NULL;
}
public function getLists() {
$lists = $this->sIBHttpClient
->get("list", "");
return new GetLists($lists['data']);
}
public function getList($id) {
$list = $this->sIBHttpClient
->get("list/" . $id, "");
return new GetExtendedList([
'id' => $list['data']['id'],
'name' => $list['data']['name'],
'totalSubscribers' => $list['data']['total_subscribers'],
'totalBlacklisted' => $list['data']['total_blacklisted'],
'createdAt' => $list['data']['entered'],
'folderId' => $list['data']['list_parent'],
'dynamicList' => $list['data']['dynamic_list'],
]);
}
public function sendEmail(array $to, string $subject, string $html, string $text, array $from = [], array $replyto = [], array $cc = [], array $bcc = [], array $attachment = [], array $headers = []) {
$replyto += [
'email' => NULL,
'name' => NULL,
];
$to += [
'email' => NULL,
'name' => NULL,
];
$from += [
'email' => NULL,
'name' => NULL,
];
$emailData = [
"text" => $text,
"replyto" => [
$replyto['email'] => $replyto['name'],
],
"html" => $html,
"to" => [
$to['email'] => $to['name'],
],
"attachment" => $attachment,
"from" => [
$from['email'],
$from['name'],
],
"subject" => $subject,
"headers" => $headers,
];
if (!empty($cc)) {
$cc += [
'email' => NULL,
'name' => NULL,
];
$emailData['cc'] = [
$cc['email'] => $cc['name'],
];
}
if (!empty($bcc)) {
$bcc += [
'email' => NULL,
'name' => NULL,
];
$emailData['bcc'] = [
$bcc['email'] => $bcc['name'],
];
}
$message = $this->sIBHttpClient
->post("email", Json::encode($emailData));
if ($message['code'] === 'success') {
return new CreateSmtpEmail($message['data']['message-id']);
}
return NULL;
}
public function getUser($email) {
$contactInfo = $this->sIBHttpClient
->get("user/" . $email, "");
if (empty($contactInfo['data'])) {
return NULL;
}
return new GetExtendedContactDetails([
'email' => $contactInfo['data']['email'],
'emailBlacklisted' => $contactInfo['data']['blacklisted'],
'smsBlacklisted' => $contactInfo['data']['blacklisted_sms'],
'createdAt' => $contactInfo['data']['entered'],
'modifiedAt' => $contactInfo['data']['entered'],
'listIds' => $contactInfo['data']['listid'],
]);
}
public function createUpdateUser($email, array $attributes = [], array $blacklisted = [], $listid = '', $listid_unlink = '') {
$this->sIBHttpClient
->post("user/createdituser", Json::encode([
"email" => $email,
"attributes" => $attributes,
"blacklisted" => $blacklisted,
"listid" => $listid,
"listid_unlink" => $listid_unlink,
]));
}
public function getAttributes() {
$sibAttributes = $this->sIBHttpClient
->get("attribute/", "");
$attributes['attributes'] = [];
foreach ($sibAttributes['data']['normal_attributes'] as $attribute) {
$attributes['attributes'][] = [
'name' => $attribute['name'],
'type' => $attribute['type'],
'category' => 'normal',
];
}
return new GetAttributes($attributes);
}
public function getAccessTokens() {
return $this->sIBHttpClient
->get("account/token", "")['data']['access_token'];
}
public function getSmtpDetails() {
$smtpDetails = $this->sIBHttpClient
->get("account/smtpdetail", "");
if ($smtpDetails) {
$sibSmtpDetails = $smtpDetails['data']['relay_data']['data'];
$enabled = $smtpDetails['data']['relay_data']['status'] === 'enabled';
return new GetSmtpDetails($sibSmtpDetails['username'], $sibSmtpDetails['relay'], $sibSmtpDetails['port'], $enabled);
}
return null;
}
public function countUserlists(array $listids = []) {
$userLists = $this->sIBHttpClient
->post("list/display", Json::encode([
'listids' => $listids,
]));
return $userLists['data']['total_list_records'];
}
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);
}
}