2024-12-20 23:46:48 +01:00
|
|
|
<?php
|
|
|
|
|
2024-12-29 00:46:28 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2024-12-20 23:46:48 +01:00
|
|
|
namespace D3\KlicktippPhpClient\Resources;
|
|
|
|
|
|
|
|
use D3\KlicktippPhpClient\Entities\Subscriber as SubscriberEntity;
|
2024-12-22 23:36:29 +01:00
|
|
|
use D3\KlicktippPhpClient\Entities\SubscriberList;
|
2024-12-20 23:46:48 +01:00
|
|
|
use D3\KlicktippPhpClient\Exceptions\BaseException;
|
2024-12-21 19:25:33 +01:00
|
|
|
use GuzzleHttp\RequestOptions;
|
2024-12-20 23:46:48 +01:00
|
|
|
|
|
|
|
class Subscriber extends Model
|
|
|
|
{
|
|
|
|
/**
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-20 23:46:48 +01:00
|
|
|
*/
|
2024-12-22 23:36:29 +01:00
|
|
|
public function index(): SubscriberList
|
2024-12-20 23:46:48 +01:00
|
|
|
{
|
2024-12-22 23:36:29 +01:00
|
|
|
$data = $this->connection->requestAndParse(
|
2024-12-20 23:46:48 +01:00
|
|
|
'GET',
|
2024-12-29 00:46:28 +01:00
|
|
|
'subscriber.json'
|
2024-12-20 23:46:48 +01:00
|
|
|
);
|
2024-12-22 23:36:29 +01:00
|
|
|
|
|
|
|
return new SubscriberList($data);
|
2024-12-20 23:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-20 23:46:48 +01:00
|
|
|
*/
|
|
|
|
public function get(string $subscriberId): SubscriberEntity
|
|
|
|
{
|
|
|
|
$data = $this->connection->requestAndParse(
|
|
|
|
'GET',
|
2024-12-29 00:46:28 +01:00
|
|
|
'subscriber/'.urlencode(trim($subscriberId)).'.json'
|
2024-12-20 23:46:48 +01:00
|
|
|
);
|
|
|
|
|
2024-12-31 00:27:14 +01:00
|
|
|
return new SubscriberEntity($data, $this);
|
2024-12-20 23:46:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-20 23:46:48 +01:00
|
|
|
*/
|
|
|
|
public function search(string $mailAddress): string
|
|
|
|
{
|
|
|
|
return current(
|
|
|
|
$this->connection->requestAndParse(
|
|
|
|
'POST',
|
2024-12-29 00:46:28 +01:00
|
|
|
'subscriber/search.json',
|
2024-12-20 23:46:48 +01:00
|
|
|
[
|
2024-12-21 19:25:33 +01:00
|
|
|
RequestOptions::FORM_PARAMS => [
|
2024-12-29 00:46:28 +01:00
|
|
|
'email' => trim($mailAddress),
|
|
|
|
],
|
2024-12-20 23:46:48 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-12-22 13:54:42 +01:00
|
|
|
|
|
|
|
/**
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-22 13:54:42 +01:00
|
|
|
*/
|
|
|
|
public function subscribe(
|
|
|
|
string $mailAddress,
|
2024-12-29 00:46:28 +01:00
|
|
|
?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',
|
2024-12-29 00:46:28 +01:00
|
|
|
'subscriber.json',
|
2024-12-22 13:54:42 +01:00
|
|
|
[
|
2024-12-29 00:46:28 +01:00
|
|
|
RequestOptions::FORM_PARAMS => array_filter(
|
2024-12-29 23:31:22 +01:00
|
|
|
array_merge(
|
|
|
|
$this->convertDataArrayToUrlParameters($fields ?? []),
|
|
|
|
[
|
|
|
|
'email' => trim($mailAddress),
|
|
|
|
'listid' => trim($listId ?? ''),
|
|
|
|
'tagid' => trim($tagId ?? ''),
|
|
|
|
'fields' => array_filter($fields ?? []),
|
|
|
|
'smsnumber' => trim($smsNumber ?? ''),
|
|
|
|
]
|
|
|
|
)
|
2024-12-29 00:46:28 +01:00
|
|
|
),
|
2024-12-22 13:54:42 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-22 13:54:42 +01:00
|
|
|
*/
|
2024-12-29 00:46:28 +01:00
|
|
|
public function unsubscribe(string $mailAddress): bool
|
2024-12-22 13:54:42 +01:00
|
|
|
{
|
2024-12-29 00:46:28 +01:00
|
|
|
return (bool) current(
|
2024-12-22 13:54:42 +01:00
|
|
|
$this->connection->requestAndParse(
|
|
|
|
'POST',
|
2024-12-29 00:46:28 +01:00
|
|
|
'subscriber/unsubscribe.json',
|
2024-12-22 13:54:42 +01:00
|
|
|
[
|
|
|
|
RequestOptions::FORM_PARAMS => [
|
2024-12-29 00:46:28 +01:00
|
|
|
'email' => trim($mailAddress),
|
|
|
|
],
|
2024-12-22 13:54:42 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2025-01-02 13:52:38 +01:00
|
|
|
* add tag
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-22 13:54:42 +01:00
|
|
|
*/
|
2025-01-02 13:52:38 +01:00
|
|
|
public function tag(string $mailAddress, array $tagIds): bool
|
2024-12-22 13:54:42 +01:00
|
|
|
{
|
2025-01-02 13:52:38 +01:00
|
|
|
return (bool) current(
|
2024-12-22 13:54:42 +01:00
|
|
|
$this->connection->requestAndParse(
|
|
|
|
'POST',
|
2024-12-29 00:46:28 +01:00
|
|
|
'subscriber/tag.json',
|
2024-12-22 13:54:42 +01:00
|
|
|
[
|
|
|
|
RequestOptions::FORM_PARAMS => [
|
|
|
|
'email' => trim($mailAddress),
|
2024-12-29 00:46:28 +01:00
|
|
|
'tagids' => implode(',', array_filter(
|
|
|
|
array_map('trim', $tagIds)
|
|
|
|
)),
|
|
|
|
],
|
2024-12-22 13:54:42 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2025-01-02 13:52:38 +01:00
|
|
|
* remove tag
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-22 13:54:42 +01:00
|
|
|
*/
|
2025-01-02 13:52:38 +01:00
|
|
|
public function untag(string $mailAddress, string $tagId): bool
|
2024-12-22 13:54:42 +01:00
|
|
|
{
|
2025-01-02 13:52:38 +01:00
|
|
|
return (bool) current(
|
2024-12-22 13:54:42 +01:00
|
|
|
$this->connection->requestAndParse(
|
|
|
|
'POST',
|
2024-12-29 00:46:28 +01:00
|
|
|
'subscriber/untag.json',
|
2024-12-22 13:54:42 +01:00
|
|
|
[
|
|
|
|
RequestOptions::FORM_PARAMS => [
|
|
|
|
'email' => trim($mailAddress),
|
2024-12-29 00:46:28 +01:00
|
|
|
'tagid' => trim($tagId),
|
|
|
|
],
|
2024-12-22 13:54:42 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-12-22 23:38:12 +01:00
|
|
|
|
|
|
|
/**
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-22 23:38:12 +01:00
|
|
|
*/
|
2024-12-29 00:46:28 +01:00
|
|
|
public function tagged(string $tagId): array
|
2024-12-22 23:38:12 +01:00
|
|
|
{
|
2024-12-29 00:46:28 +01:00
|
|
|
return $this->connection->requestAndParse(
|
|
|
|
'POST',
|
|
|
|
'subscriber/tagged.json',
|
|
|
|
[
|
|
|
|
RequestOptions::FORM_PARAMS => [
|
|
|
|
'tagid' => trim($tagId),
|
|
|
|
],
|
|
|
|
]
|
|
|
|
) ?? [];
|
2024-12-22 23:38:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-22 23:38:12 +01:00
|
|
|
*/
|
2024-12-29 00:46:28 +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',
|
2024-12-29 00:46:28 +01:00
|
|
|
'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(
|
|
|
|
array_merge(
|
|
|
|
$this->convertDataArrayToUrlParameters($fields ?? []),
|
|
|
|
[
|
|
|
|
'newemail' => trim($newEmail ?? ''),
|
|
|
|
'newsmsnumber' => trim($newSmsNumber ?? ''),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
2024-12-22 23:38:12 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-29 23:31:22 +01:00
|
|
|
* @return true
|
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',
|
2024-12-29 00:46:28 +01:00
|
|
|
'subscriber/'.urlencode(trim($subscriberId)).'.json'
|
2024-12-22 23:38:12 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-22 23:38:12 +01:00
|
|
|
*/
|
2024-12-29 23:31:22 +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',
|
2024-12-29 00:46:28 +01:00
|
|
|
'subscriber/signin.json',
|
2024-12-22 23:38:12 +01:00
|
|
|
[
|
2024-12-29 23:31:22 +01:00
|
|
|
RequestOptions::FORM_PARAMS => array_filter(
|
|
|
|
array_merge(
|
|
|
|
$this->convertDataArrayToUrlParameters($fields ?? []),
|
|
|
|
[
|
|
|
|
'apikey' => trim($apikey),
|
|
|
|
'email' => trim($emailAddress),
|
|
|
|
'smsnumber' => trim($smsNumber ?? ''),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
2024-12-22 23:38:12 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-22 23:38:12 +01:00
|
|
|
*/
|
|
|
|
public function signout(string $apikey, string $emailAddress): string
|
|
|
|
{
|
|
|
|
return current(
|
|
|
|
$this->connection->requestAndParse(
|
|
|
|
'POST',
|
2024-12-29 00:46:28 +01:00
|
|
|
'subscriber/signout.json',
|
2024-12-22 23:38:12 +01:00
|
|
|
[
|
|
|
|
RequestOptions::FORM_PARAMS => [
|
|
|
|
'apikey' => trim($apikey),
|
|
|
|
'email' => trim($emailAddress),
|
2024-12-29 00:46:28 +01:00
|
|
|
],
|
2024-12-22 23:38:12 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-29 00:46:28 +01:00
|
|
|
* @throws BaseException
|
2024-12-22 23:38:12 +01:00
|
|
|
*/
|
|
|
|
public function signoff(string $apikey, string $emailAddress): string
|
|
|
|
{
|
|
|
|
return current(
|
|
|
|
$this->connection->requestAndParse(
|
|
|
|
'POST',
|
2024-12-29 00:46:28 +01:00
|
|
|
'subscriber/signoff.json',
|
2024-12-22 23:38:12 +01:00
|
|
|
[
|
|
|
|
RequestOptions::FORM_PARAMS => [
|
|
|
|
'apikey' => trim($apikey),
|
|
|
|
'email' => trim($emailAddress),
|
2024-12-29 00:46:28 +01:00
|
|
|
],
|
2024-12-22 23:38:12 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-12-31 00:27:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws BaseException
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
} catch (BaseException) {
|
|
|
|
$id = $this->subscribe($newMailAddress ?? $mailAddress, null, null, $fields, $smsNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->get($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws BaseException
|
|
|
|
*/
|
|
|
|
public function getSubscriberByMailAddress(string $mailAddress): SubscriberEntity
|
|
|
|
{
|
|
|
|
return $this->get(
|
|
|
|
$this->search($mailAddress)
|
|
|
|
);
|
|
|
|
}
|
2024-12-20 23:46:48 +01:00
|
|
|
}
|