ldc-crm/app/Lib/Helpers/MailjetHelper.php
2026-06-04 15:34:29 +02:00

204 lines
5.1 KiB
PHP

<?php
namespace App\Lib\Helpers;
use App\Models\Brand;
use App\Models\CustomerInfo;
use App\Models\SingleOptin;
use Mailjet\Resources;
class MailjetHelper
{
/**
* Undocumented function
*
* @param integer $campaignId
* @return array
*/
public static function getCampaign(int $campaignId): array
{
$response = app('mailjetClient')->get(Resources::$Campaign, [
'id' => $campaignId
]);
if ($response->success()) {
return $response->getData();
} else {
return [];
}
}
/**
* Retrieves contact lists
*
* @return array
*/
public static function getContactLists(): array
{
$filters = [
'Limit' => 1000,
'Sort' => 'CreatedAt DESC',
];
$response = app('mailjetClient')->get(Resources::$Contactslist, [
'filters' => $filters
]);
if ($response->success()) {
return $response->getData();
} else {
return [];
}
}
/**
* Get unsubscribed contacts for a given contact list
*
* @param integer $contactListId
* @param \Carbon\Carbon|null $lastActivity
* @return array
*/
public static function getUnsubscribedContacts(int $contactListId, \Carbon\Carbon|null $lastActivity = null): array
{
$filters = [
'ContactsList' => $contactListId,
'Unsub' => true,
'Limit' => 1000,
];
if (!is_null($lastActivity)) {
$filters = array_merge(
$filters,
[
'LastActivityAt' => $lastActivity->toRfc3339String()
]
);
}
$response = app('mailjetClient')->get(Resources::$Listrecipient, [
'filters' => $filters
]);
if ($response->success()) {
return $response->getData();
} else {
return [];
}
}
/**
* Get contact for a given contact id
*
* @param integer $contactId
* @return array
*/
public static function getContact(int $contactId): array
{
$response = app('mailjetClient')->get(Resources::$Contact, [
'id' => $contactId
]);
if ($response->success()) {
return $response->getData();
} else {
return [];
}
}
/**
* Creates a new Mailjet contact list
*
* @param string $listName
* @return array
*/
public static function createContactList(string $listName): array
{
$response = app('mailjetClient')->post(Resources::$Contactslist, [
'body' => [
'Name' => $listName,
]
]);
if ($response->success()) {
return $response->getData();
} else {
return [];
}
}
/**
* Uploads a CSV file
*
* @param integer $mailjetListId
* @param string $csvFile
* @return array
*/
public static function uploadCsvFile(int $mailjetListId, string $csvFile): array
{
$response = app('mailjetClient')->post(Resources::$ContactslistCsvdata, [
'id' => $mailjetListId,
'body' => $csvFile,
]);
if ($response->success()) {
return $response->getData();
} else {
return [];
}
}
/**
* Imports contacts into a Mailjet contact list
*
* @param integer $listId
* @param integer $dataId
* @return array
*/
public static function importContacts(int $listId, int $dataId): array
{
$response = app('mailjetClient')->post(Resources::$Csvimport, [
'body' => [
'ContactsListID' => $listId,
'DataID' => $dataId,
'Method' => "addnoforce"
]
]);
if ($response->success()) {
return $response->getData();
} else {
return [];
}
}
/**
* Undocumented function
*
* @param \App\Models\Brand $brandModel
* @param string $email
* @return void
*/
public static function unsubscribeContact(Brand $brandModel, string $email): void
{
$crmCustomer = CustomerInfo::getFromEmail($brandModel, $email)->first();
if ($crmCustomer) {
$crmCustomer->opt_in = false;
if (!$crmCustomer->saveOrFail()) {
throw new \Exception(sprintf('Optin status not changed for customer (%s) and brand (%d).', $crmCustomer->hashcode, $brandModel->name));
}
} else {
$singleOptin = SingleOptin::query()->where('email', $email)
->whereHas('brand', function ($q) use ($brandModel) {
$q->whereId($brandModel->getKey());
})->first();
if ($singleOptin) {
if (!$singleOptin->delete()) {
throw new \Exception(sprintf('SingleOptin (%s) not deleted for brand (%d).', $singleOptin->hashcode, $brandModel->name));
}
}
}
}
}