-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Communication] Add support for Messages and Templates (#3)
[Communication] Add support for Messages and Templates
- Loading branch information
Anton Samofal
authored
Jun 14, 2023
1 parent
d9255bc
commit 826d3f1
Showing
14 changed files
with
441 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Getsno\Relesys\Api\Communication; | ||
|
||
use Getsno\Relesys\Api\Api; | ||
use Getsno\Relesys\Api\BatchResponse; | ||
use Getsno\Relesys\Api\ApiQueryParams; | ||
use Getsno\Relesys\Api\Communication\Entities\Template; | ||
use Getsno\Relesys\Api\Communication\Enums\MessageType; | ||
use Getsno\Relesys\Exceptions\RelesysHttpClientException; | ||
use Getsno\Relesys\Api\Communication\Enums\DeliveryMethod; | ||
use Getsno\Relesys\Api\Communication\ValueObjects\MessageBody; | ||
|
||
class Communication extends Api | ||
{ | ||
/** | ||
* @throws RelesysHttpClientException | ||
*/ | ||
public function sendMessage( | ||
string $userId, | ||
DeliveryMethod $deliveryMethod, | ||
?string $communicationTemplateId = null, | ||
?MessageType $messageType = null, | ||
?MessageBody $messageBody = null, | ||
): void | ||
{ | ||
$params = [ | ||
'deliveryMethod' => $deliveryMethod->value, | ||
]; | ||
|
||
if ($communicationTemplateId !== null) { | ||
$params['communicationTemplateId'] = $communicationTemplateId; | ||
} elseif ($messageType !== null) { | ||
$params['messageType'] = $messageType->value; | ||
} else { | ||
$params['messageBody'] = $messageBody; | ||
} | ||
|
||
$this->httpClient->post("communication/messages/$userId/send", $params); | ||
} | ||
|
||
/** | ||
* @throws RelesysHttpClientException | ||
*/ | ||
public function getTemplate(string $templateId): Template | ||
{ | ||
$responseData = $this->httpClient->get("communication/templates/$templateId")['data']; | ||
|
||
return Template::fromArray($responseData)->setSource($responseData); | ||
} | ||
|
||
/** | ||
* @throws RelesysHttpClientException | ||
*/ | ||
public function getTemplates(ApiQueryParams $queryParams = new ApiQueryParams(), int $page = 1): BatchResponse | ||
{ | ||
$queryParams->offset($queryParams->getLimit() * ($page - 1)); | ||
$params = $queryParams->toArray(); | ||
|
||
$response = $this->httpClient->get('communication/templates', $params); | ||
|
||
$users = array_map( | ||
static fn(array $template) => Template::fromArray($template)->setSource($template), | ||
$response['data'] | ||
); | ||
|
||
return new BatchResponse( | ||
$response['count'], | ||
$users, | ||
$response['nextUrl'] ?? null, | ||
$response['previousUrl'] ?? null, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Getsno\Relesys\Api\Communication\Entities; | ||
|
||
use Carbon\Carbon; | ||
use Getsno\Relesys\Api\ApiEntity; | ||
use Getsno\Relesys\Api\Communication\ValueObjects\TemplateCultures; | ||
|
||
class Template extends ApiEntity | ||
{ | ||
public string $id; | ||
public string $name; | ||
|
||
/** | ||
* @var TemplateCultures[] | ||
*/ | ||
public array $communicationTemplateCultures; | ||
public Carbon $creationDateTime; | ||
public ?Carbon $lastModifiedDateTime; | ||
|
||
protected function setCreationDateTime(string $dateTime): void | ||
{ | ||
$this->creationDateTime = Carbon::parse($dateTime); | ||
} | ||
|
||
protected function setLastModifiedDateTime(string $dateTime): void | ||
{ | ||
$this->lastModifiedDateTime = Carbon::parse($dateTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Getsno\Relesys\Api\Communication\Enums; | ||
|
||
enum DeliveryMethod: string | ||
{ | ||
case Sms = 'sms'; | ||
case Email = 'email'; | ||
case Both = 'both'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Getsno\Relesys\Api\Communication\Enums; | ||
|
||
enum MessageType: string | ||
{ | ||
case Welcome = 'welcome'; | ||
case LoginInfo = 'logininfo'; | ||
case ForgottenPassword = 'forgottenpassword'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Getsno\Relesys\Api\Communication\ValueObjects; | ||
|
||
use Getsno\Relesys\Traits\CreatableFromArray; | ||
use Getsno\Relesys\Traits\ConvertibleToArray; | ||
|
||
class MessageBody | ||
{ | ||
use CreatableFromArray; | ||
use ConvertibleToArray; | ||
|
||
public readonly ?TemplateSms $sms; | ||
public readonly ?TemplateEmail $email; | ||
|
||
protected function setSms(array $data): void | ||
{ | ||
$this->sms = TemplateSms::fromArray($data); | ||
} | ||
|
||
protected function setEmail(array $data): void | ||
{ | ||
$this->email = TemplateEmail::fromArray($data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Getsno\Relesys\Api\Communication\ValueObjects; | ||
|
||
use Getsno\Relesys\Traits\CreatableFromArray; | ||
use Getsno\Relesys\Traits\ConvertibleToArray; | ||
|
||
class TemplateCultures | ||
{ | ||
use CreatableFromArray; | ||
use ConvertibleToArray; | ||
|
||
public string $cultureCode; | ||
public TemplateSms $sms; | ||
public TemplateEmail $email; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Getsno\Relesys\Api\Communication\ValueObjects; | ||
|
||
use Getsno\Relesys\Traits\CreatableFromArray; | ||
use Getsno\Relesys\Traits\ConvertibleToArray; | ||
|
||
class TemplateEmail | ||
{ | ||
use CreatableFromArray; | ||
use ConvertibleToArray; | ||
|
||
public readonly string $subject; | ||
public readonly string $body; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Getsno\Relesys\Api\Communication\ValueObjects; | ||
|
||
use Getsno\Relesys\Traits\CreatableFromArray; | ||
use Getsno\Relesys\Traits\ConvertibleToArray; | ||
|
||
class TemplateSms | ||
{ | ||
use CreatableFromArray; | ||
use ConvertibleToArray; | ||
|
||
public readonly string $body; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.