394 regels
12 KiB
PHP

2024-12-20 23:46:48 +01:00
<?php
/**
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
2025-01-05 15:20:56 +01:00
declare(strict_types=1);
2024-12-20 23:46:48 +01:00
namespace D3\KlicktippPhpClient\Resources;
use D3\KlicktippPhpClient\Entities\Subscriber as SubscriberEntity;
use D3\KlicktippPhpClient\Entities\SubscriberList;
2025-01-08 15:29:20 +01:00
use D3\KlicktippPhpClient\Exceptions\CommunicationException;
use D3\KlicktippPhpClient\Exceptions\ResponseContentException;
2024-12-21 19:25:33 +01:00
use GuzzleHttp\RequestOptions;
2024-12-20 23:46:48 +01:00
class Subscriber extends Model
{
2025-01-03 00:05:00 +01:00
public const ID = 'id';
public const LISTID = 'listid';
public const OPTIN = 'optin';
public const OPTIN_IP = 'optin_ip';
public const EMAIL = 'email';
public const STATUS = 'status';
public const BOUNCE = 'bounce';
public const IP = 'ip';
public const UNSUBSCRIPTION = 'unsubscription';
public const UNSUBSCRIPTION_IP = 'unsubscription_ip';
public const SMS_PHONE = 'sms_phone';
public const SMS_STATUS = 'sms_status';
public const SMS_BOUNCE = 'sms_bounce';
public const SMS_DATE = 'sms_date';
public const SMS_UNSUBSCRIPTION = 'sms_unsubscription';
public const SMS_REFERRER = 'sms_referrer';
public const REFERRER = 'referrer';
public const DATE = 'date';
public const FIELD_FIRSTNAME = 'fieldFirstName';
public const FIELD_LASTNAME = 'fieldLastName';
public const FIELD_COMPANYNAME = 'fieldCompanyName';
public const FIELD_STREET1 = 'fieldStreet1';
public const FIELD_STREET2 = 'fieldStreet2';
public const FIELD_CITY = 'fieldCity';
public const FIELD_STATE = 'fieldState';
public const FIELD_ZIP = 'fieldZip';
public const FIELD_COUNTRY = 'fieldCountry';
public const FIELD_PRIVATEPHONE = 'fieldPrivatePhone';
public const FIELD_MOBILEPHONE = 'fieldMobilePhone';
public const FIELD_PHONE = 'fieldPhone';
public const FIELD_FAX = 'fieldFax';
public const FIELD_WEBSITE = 'fieldWebsite';
public const FIELD_BIRTHDAY = 'fieldBirthday';
public const FIELD_LEADVALUE = 'fieldLeadValue';
public const TAGS = 'tags';
public const MANUALTAGS = 'manual_tags';
public const SMARTTAGS = 'smart_tags';
public const CAMPAIGNSSTARTED = 'campaigns_started';
public const CAMPAIGNSFINISHED = 'campaigns_finished';
public const NOTIFICATIONEMAILSSENT = 'notification_emails_sent';
public const NOTIFICATIONEMAILSOPENED = 'notification_emails_opened';
public const NOTIFICATIONEMAILSCLICKED = 'notification_emails_clicked';
public const NOTIFICATIONEMAILSVIEWED = 'notification_emails_viewed';
public const OUTBOUND = 'outbound';
public const PARAM_TAGID = 'tagid';
public const PARAM_TAGIDS = 'tagids';
public const PARAM_FIELDS = 'fields';
public const PARAM_SMS_NUMBER = 'smsnumber';
public const PARAM_NEW_EMAIL = 'newemail';
public const PARAM_NEW_SMSNUMBER = 'newsmsnumber';
public const PARAM_APIKEY = 'apikey';
2024-12-20 23:46:48 +01:00
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-20 23:46:48 +01:00
*/
public function index(): SubscriberList
2024-12-20 23:46:48 +01:00
{
$data = $this->connection->requestAndParse(
2024-12-20 23:46:48 +01:00
'GET',
'subscriber.json'
2024-12-20 23:46:48 +01:00
);
return new SubscriberList($data);
2024-12-20 23:46:48 +01:00
}
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-20 23:46:48 +01:00
*/
public function get(string $subscriberId): array
2024-12-20 23:46:48 +01:00
{
return $this->connection->requestAndParse(
2024-12-20 23:46:48 +01:00
'GET',
'subscriber/'.urlencode(trim($subscriberId)).'.json'
2024-12-20 23:46:48 +01:00
);
}
2024-12-20 23:46:48 +01:00
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
*/
public function getEntity(string $subscriberId): SubscriberEntity
{
return new SubscriberEntity($this->get($subscriberId), $this);
2024-12-20 23:46:48 +01:00
}
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-20 23:46:48 +01:00
*/
public function search(string $mailAddress): string
{
2025-01-05 15:20:56 +01:00
return (string) current(
2024-12-20 23:46:48 +01:00
$this->connection->requestAndParse(
'POST',
'subscriber/search.json',
2024-12-20 23:46:48 +01:00
[
2024-12-21 19:25:33 +01:00
RequestOptions::FORM_PARAMS => [
2025-01-03 00:05:00 +01:00
self::EMAIL => trim($mailAddress),
],
2024-12-20 23:46:48 +01:00
]
)
);
}
2024-12-22 13:54:42 +01:00
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-22 13:54:42 +01:00
*/
public function subscribe(
string $mailAddress,
?string $listId = null,
?string $tagId = null,
?array $fields = null,
?string $smsNumber = null,
): string {
2024-12-22 13:54:42 +01:00
return current(
$this->connection->requestAndParse(
'POST',
'subscriber.json',
2024-12-22 13:54:42 +01:00
[
RequestOptions::FORM_PARAMS => array_filter(
2025-01-02 23:51:20 +01:00
[
2025-01-03 00:05:00 +01:00
self::EMAIL => trim($mailAddress),
self::LISTID => trim($listId ?? ''),
self::PARAM_TAGID => trim($tagId ?? ''),
self::PARAM_FIELDS => array_filter(
2025-01-02 23:51:20 +01:00
array_map('trim', $fields ?? [])
),
2025-01-03 00:05:00 +01:00
self::PARAM_SMS_NUMBER => trim($smsNumber ?? ''),
2025-01-02 23:51:20 +01:00
]
),
2024-12-22 13:54:42 +01:00
]
)
);
}
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-22 13:54:42 +01:00
*/
public function unsubscribe(string $mailAddress): bool
2024-12-22 13:54:42 +01:00
{
return (bool) current(
2024-12-22 13:54:42 +01:00
$this->connection->requestAndParse(
'POST',
'subscriber/unsubscribe.json',
2024-12-22 13:54:42 +01:00
[
RequestOptions::FORM_PARAMS => [
2025-01-03 00:05:00 +01:00
self::EMAIL => trim($mailAddress),
],
2024-12-22 13:54:42 +01:00
]
)
);
}
/**
* add tag
2025-01-08 15:29:20 +01:00
*
* @throws CommunicationException
* @throws ResponseContentException
2024-12-22 13:54:42 +01:00
*/
public function tag(string $mailAddress, array $tagIds): bool
2024-12-22 13:54:42 +01:00
{
return (bool) current(
2024-12-22 13:54:42 +01:00
$this->connection->requestAndParse(
'POST',
'subscriber/tag.json',
2024-12-22 13:54:42 +01:00
[
2025-01-02 23:51:20 +01:00
RequestOptions::FORM_PARAMS => array_filter(
[
2025-01-03 00:05:00 +01:00
self::EMAIL => trim($mailAddress),
self::PARAM_TAGIDS => array_filter(
2025-01-02 23:51:20 +01:00
array_map('trim', $tagIds)
),
]
),
2024-12-22 13:54:42 +01:00
]
)
);
}
/**
* remove tag
2025-01-08 15:29:20 +01:00
*
* @throws CommunicationException
* @throws ResponseContentException
2024-12-22 13:54:42 +01:00
*/
public function untag(string $mailAddress, string $tagId): bool
2024-12-22 13:54:42 +01:00
{
return (bool) current(
2024-12-22 13:54:42 +01:00
$this->connection->requestAndParse(
'POST',
'subscriber/untag.json',
2024-12-22 13:54:42 +01:00
[
RequestOptions::FORM_PARAMS => [
2025-01-03 00:05:00 +01:00
self::EMAIL => trim($mailAddress),
self::PARAM_TAGID => trim($tagId),
],
2024-12-22 13:54:42 +01:00
]
)
);
}
2024-12-22 23:38:12 +01:00
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-22 23:38:12 +01:00
*/
public function tagged(string $tagId): SubscriberList
2024-12-22 23:38:12 +01:00
{
$data = $this->connection->requestAndParse(
'POST',
'subscriber/tagged.json',
[
RequestOptions::FORM_PARAMS => [
2025-01-03 00:05:00 +01:00
self::PARAM_TAGID => trim($tagId),
],
]
) ?? [];
return new SubscriberList($data);
2024-12-22 23:38:12 +01:00
}
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-22 23:38:12 +01:00
*/
public function update(
string $subscriberId,
2024-12-29 23:31:22 +01:00
?array $fields = null,
?string $newEmail = null,
?string $newSmsNumber = null
): bool {
return (bool) current(
2024-12-22 23:38:12 +01:00
$this->connection->requestAndParse(
'PUT',
'subscriber/'.urlencode(trim($subscriberId)).'.json',
2024-12-22 23:38:12 +01:00
[
2024-12-29 23:31:22 +01:00
RequestOptions::FORM_PARAMS => array_filter(
2025-01-02 23:51:20 +01:00
[
2025-01-03 00:05:00 +01:00
self::PARAM_NEW_EMAIL => trim($newEmail ?? ''),
self::PARAM_NEW_SMSNUMBER => trim($newSmsNumber ?? ''),
self::PARAM_FIELDS => array_filter(
2025-01-02 23:51:20 +01:00
array_map('trim', $fields ?? [])
),
]
2024-12-29 23:31:22 +01:00
),
2024-12-22 23:38:12 +01:00
]
)
);
}
/**
2024-12-29 23:31:22 +01:00
* @return true
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-22 23:38:12 +01:00
*/
2024-12-29 23:31:22 +01:00
public function delete(string $subscriberId): bool
2024-12-22 23:38:12 +01:00
{
2024-12-29 23:31:22 +01:00
return (bool) current(
2024-12-22 23:38:12 +01:00
$this->connection->requestAndParse(
'DELETE',
'subscriber/'.urlencode(trim($subscriberId)).'.json'
2024-12-22 23:38:12 +01:00
)
);
}
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-22 23:38:12 +01:00
*/
2025-01-02 23:51:20 +01:00
public function signin(
string $apikey,
string $emailAddress,
?array $fields = null,
?string $smsNumber = null
): string {
2024-12-22 23:38:12 +01:00
return current(
$this->connection->requestAndParse(
'POST',
'subscriber/signin.json',
2024-12-22 23:38:12 +01:00
[
2024-12-29 23:31:22 +01:00
RequestOptions::FORM_PARAMS => array_filter(
2025-01-02 23:51:20 +01:00
[
2025-01-03 00:05:00 +01:00
self::PARAM_APIKEY => trim($apikey),
self::EMAIL => trim($emailAddress),
self::PARAM_SMS_NUMBER => trim($smsNumber ?? ''),
self::PARAM_FIELDS => array_filter(
2025-01-02 23:51:20 +01:00
array_map('trim', $fields ?? [])
),
]
2024-12-29 23:31:22 +01:00
),
2024-12-22 23:38:12 +01:00
]
)
);
}
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-22 23:38:12 +01:00
*/
public function signout(string $apikey, string $emailAddress): string
{
return current(
$this->connection->requestAndParse(
'POST',
'subscriber/signout.json',
2024-12-22 23:38:12 +01:00
[
RequestOptions::FORM_PARAMS => [
2025-01-03 00:05:00 +01:00
self::PARAM_APIKEY => trim($apikey),
self::EMAIL => trim($emailAddress),
],
2024-12-22 23:38:12 +01:00
]
)
);
}
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
* @throws ResponseContentException
2024-12-22 23:38:12 +01:00
*/
2025-01-05 15:20:56 +01:00
public function signoff(string $apikey, string $emailAddress): bool
2024-12-22 23:38:12 +01:00
{
2025-01-05 15:20:56 +01:00
return (bool) current(
2024-12-22 23:38:12 +01:00
$this->connection->requestAndParse(
'POST',
'subscriber/signoff.json',
2024-12-22 23:38:12 +01:00
[
RequestOptions::FORM_PARAMS => [
2025-01-03 00:05:00 +01:00
self::PARAM_APIKEY => trim($apikey),
self::EMAIL => trim($emailAddress),
],
2024-12-22 23:38:12 +01:00
]
)
);
}
2024-12-31 00:27:14 +01:00
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
2024-12-31 00:27:14 +01:00
*/
public function setSubscriber(
string $mailAddress,
?string $newMailAddress = null,
?string $smsNumber = null,
?array $fields = null
): SubscriberEntity {
try {
$id = $this->search($mailAddress);
$this->update($id, $fields, $newMailAddress ?? $mailAddress, $smsNumber);
2025-01-08 15:29:20 +01:00
} catch (CommunicationException) {
2024-12-31 00:27:14 +01:00
$id = $this->subscribe($newMailAddress ?? $mailAddress, null, null, $fields, $smsNumber);
}
return $this->getEntity($id);
2024-12-31 00:27:14 +01:00
}
/**
2025-01-08 15:29:20 +01:00
* @throws CommunicationException
2024-12-31 00:27:14 +01:00
*/
public function getSubscriberByMailAddress(string $mailAddress): SubscriberEntity
{
return $this->getEntity(
2024-12-31 00:27:14 +01:00
$this->search($mailAddress)
);
}
2024-12-20 23:46:48 +01:00
}