From c5085b1bb825abda6208d474a4bf59cd6d15f139 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Sun, 22 Dec 2024 23:38:12 +0100 Subject: [PATCH] complete subscriber endpoint --- src/Entities/Subscriber.php | 171 ++++++++++++++++++++++++++++++++++- src/Resources/Subscriber.php | 112 +++++++++++++++++++++++ 2 files changed, 280 insertions(+), 3 deletions(-) diff --git a/src/Entities/Subscriber.php b/src/Entities/Subscriber.php index 4c1d880..0d7a76a 100644 --- a/src/Entities/Subscriber.php +++ b/src/Entities/Subscriber.php @@ -2,18 +2,183 @@ namespace D3\KlicktippPhpClient\Entities; +use DateTime; use Doctrine\Common\Collections\ArrayCollection; class Subscriber extends ArrayCollection { + public const STATUS_SUBSCRIBED = 'subscribed'; + public const BOUNCE_NOTBOUNCED = 'Not Bounced'; + public function getId(): string { - return $this->get('id'); + return $this->get('id') ?? ''; + } + + public function getListId(): string + { + return $this->get('listid') ?? ''; + } + + public function getOptinTime(): string + { + return $this->get('optin') ?? ''; + } + + public function getOptinIp(): string + { + return $this->get('optin_ip') ?? ''; + } + + public function getEmailAddress(): string + { + return $this->get('email') ?? ''; + } + + public function getStatus(): string + { + return $this->get('status') ?? ''; + } + + public function getBounce(): string + { + return $this->get('bounce') ?? ''; + } + + public function getDate(): string + { + return $this->get('date') ?? ''; + } + + public function getIp(): string + { + return $this->get('ip') ?? ''; + } + + public function getUnsubscription(): string + { + return $this->get('unsubscription') ?? ''; + } + + public function getUnsubscriptionIp(): string + { + return $this->get('unsubscription_ip') ?? ''; + } + + public function getReferrer(): string + { + return $this->get('referrer') ?? ''; + } + + public function getSmsPhone(): string + { + return $this->get('sms_phone') ?? ''; + } + + public function getSmsStatus(): string + { + return $this->get('sms_status') ?? ''; + } + + public function getSmsBounce(): string + { + return $this->get('sms_bounce') ?? ''; + } + + public function getSmsUnsubscription(): string + { + return $this->get('sms_unsubscription') ?? ''; + } + + public function getSmsReferrer(): string + { + return $this->get('sms_referrer') ?? ''; + } + + public function getField(string $fieldId): string + { + // ToDo: should we throw fieldNotSetException + return $this->get('field'.trim($fieldId)) ?? ''; + } + + public function getTags(): array + { + return $this->get('tags') ?? []; + } + + public function getManualTags(): array + { + return $this->get('manual_tags') ?? []; + } + + public function getManualTag(string $tagId): string + { + return $this->getManualTags()[$tagId] ?? ''; + } + + public function getSmartTags(): array + { + return $this->get('smart_tags') ?? []; + } + + public function getSmartTag(string $tagId): string + { + return $this->getSmartTags()[$tagId] ?? ''; + } + + public function getStartedCampaigns(): array + { + return $this->get('campaigns_started') ?? []; + } + + public function getStartedCampaign(string $campaignId): string + { + return $this->getStartedCampaigns()[$campaignId] ?? ''; + } + + public function getFinishedCampaigns(): array + { + return $this->get('campaigns_finished') ?? []; + } + + public function getFinishedCampaign(string $campaignId): string + { + return $this->getFinishedCampaigns()[$campaignId] ?? ''; + } + + public function getSentNotificationEmails(): array + { + return $this->get('notification_emails_sent') ?? []; + } + + public function getOutbound(): array + { + return $this->get('outbound') ?? []; + } + + public function getOpenedNotificationEmails(): array + { + return $this->get('notification_emails_opened') ?? []; } public function isSubscribed(): bool { - // ToDo: adjust request - return $this->get('isSubscribed'); + return $this->getStatus() === self::STATUS_SUBSCRIBED; + } + + public function isOptedIn(): bool + { + return $this->getOptinTime() != '0000-00-00 00:00:00' && + new DateTime($this->getOptinTime()) < new DateTime(); + } + + public function isBounced(): bool + { + return $this->getBounce() != self::BOUNCE_NOTBOUNCED; + } + + public function isTagSet(string $tagId): bool + { + return in_array($tagId, $this->getTags()); } } \ No newline at end of file diff --git a/src/Resources/Subscriber.php b/src/Resources/Subscriber.php index 606d68b..15c94c9 100644 --- a/src/Resources/Subscriber.php +++ b/src/Resources/Subscriber.php @@ -137,4 +137,116 @@ class Subscriber extends Model ) ); } + + /** + * @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 + */ + public function update(string $subscriberId, array $fields, string $newEmail, string $newSmsNumber): string + { + 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), + ] + ] + ) + ); + } }