254 regels
6.7 KiB
PHP

2024-12-20 23:46:48 +01:00
<?php
namespace D3\KlicktippPhpClient\Resources;
use D3\KlicktippPhpClient\Entities\Subscriber as SubscriberEntity;
use D3\KlicktippPhpClient\Entities\SubscriberList;
2024-12-20 23:46:48 +01:00
use D3\KlicktippPhpClient\Exceptions\BaseException;
use GuzzleHttp\Exception\GuzzleException;
2024-12-21 19:25:33 +01:00
use GuzzleHttp\RequestOptions;
2024-12-20 23:46:48 +01:00
class Subscriber extends Model
{
/**
* @throws BaseException|GuzzleException
*/
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',
2024-12-21 19:25:33 +01:00
'subscriber'
2024-12-20 23:46:48 +01:00
);
return new SubscriberList($data);
2024-12-20 23:46:48 +01:00
}
/**
* @throws BaseException|GuzzleException
*/
public function get(string $subscriberId): SubscriberEntity
{
$data = $this->connection->requestAndParse(
'GET',
2024-12-21 19:25:33 +01:00
'subscriber/'.urlencode(trim($subscriberId))
2024-12-20 23:46:48 +01:00
);
return new SubscriberEntity($data);
}
/**
* @throws BaseException|GuzzleException
*/
public function search(string $mailAddress): string
{
return current(
$this->connection->requestAndParse(
'POST',
'subscriber/search',
[
2024-12-21 19:25:33 +01:00
RequestOptions::FORM_PARAMS => [
2024-12-20 23:46:48 +01:00
'email' => trim($mailAddress)
]
]
)
);
}
2024-12-22 13:54:42 +01:00
/**
* @throws BaseException|GuzzleException
*/
public function subscribe(
string $mailAddress,
string $listId,
string $tagId,
array $fields,
string $smsNumber
): string
{
return current(
$this->connection->requestAndParse(
'POST',
'subscriber',
[
RequestOptions::FORM_PARAMS => [
'email' => trim($mailAddress),
'listid' => trim($listId),
'tagid' => trim($tagId),
'fields' => array_filter($fields),
'smsnumber' => trim($smsNumber),
]
]
)
);
}
/**
* @throws BaseException|GuzzleException
*/
public function unsubscribe(string $mailAddress): string
{
return current(
$this->connection->requestAndParse(
'POST',
'subscriber/unsubscribe',
[
RequestOptions::FORM_PARAMS => [
'email' => trim($mailAddress)
]
]
)
);
}
/**
* @throws BaseException|GuzzleException
*/
2024-12-23 15:20:02 +01:00
public function tag(string $mailAddress, array $tagIds): string
2024-12-22 13:54:42 +01:00
{
return current(
$this->connection->requestAndParse(
'POST',
'subscriber/tag',
[
RequestOptions::FORM_PARAMS => [
'email' => trim($mailAddress),
2024-12-23 15:20:02 +01:00
//ToDo: apply trim to array
'tagids' => $tagIds
2024-12-22 13:54:42 +01:00
]
]
)
);
}
/**
* @throws BaseException|GuzzleException
*/
public function untag(string $mailAddress, string $tagId): string
{
return current(
$this->connection->requestAndParse(
'POST',
'subscriber/search',
[
RequestOptions::FORM_PARAMS => [
'email' => trim($mailAddress),
'tagid' => trim($tagId)
]
]
)
);
}
2024-12-22 23:38:12 +01:00
/**
* @throws BaseException|GuzzleException
*/
public function tagged(string $tagId): string
{
return current(
$this->connection->requestAndParse(
'POST',
'subscriber/tagged',
[
RequestOptions::FORM_PARAMS => [
'tagid' => trim($tagId)
]
]
)
);
}
/**
* @throws BaseException|GuzzleException
*/
2024-12-23 15:20:02 +01:00
public function update(string $subscriberId, array $fields, string $newEmail = '', string $newSmsNumber = ''): string
2024-12-22 23:38:12 +01:00
{
return current(
$this->connection->requestAndParse(
'PUT',
'subscriber/'.urlencode(trim($subscriberId)),
[
RequestOptions::FORM_PARAMS => [
//ToDo: apply trim to array
'fields' => $fields,
'newemail' => trim($newEmail),
'newsmsnumber' => trim($newSmsNumber),
]
]
)
);
}
/**
* @throws BaseException|GuzzleException
*/
public function delete(string $subscriberId): string
{
return current(
$this->connection->requestAndParse(
'DELETE',
'subscriber/'.urlencode(trim($subscriberId))
)
);
}
/**
* @throws BaseException|GuzzleException
*/
public function signin(string $apikey, string $emailAddress, array $fields, string $smsNumber): string
{
return current(
$this->connection->requestAndParse(
'POST',
'subscriber/signin',
[
RequestOptions::FORM_PARAMS => [
'apikey' => trim($apikey),
'email' => trim($emailAddress),
//ToDo: apply trim to array
'fields' => $fields,
'smsnumber' => trim($smsNumber),
]
]
)
);
}
/**
* @throws BaseException|GuzzleException
*/
public function signout(string $apikey, string $emailAddress): string
{
return current(
$this->connection->requestAndParse(
'POST',
'subscriber/signout',
[
RequestOptions::FORM_PARAMS => [
'apikey' => trim($apikey),
'email' => trim($emailAddress),
]
]
)
);
}
/**
* @throws BaseException|GuzzleException
*/
public function signoff(string $apikey, string $emailAddress): string
{
return current(
$this->connection->requestAndParse(
'POST',
'subscriber/signoff',
[
RequestOptions::FORM_PARAMS => [
'apikey' => trim($apikey),
'email' => trim($emailAddress),
]
]
)
);
}
2024-12-20 23:46:48 +01:00
}