Skip to content

Commit 4c10290

Browse files
authored
Feature/verify custom templates (#502)
* Custom template methods, list iterators pending * Iterators completed * Add the template ID in sms and voice
1 parent f094c4a commit 4c10290

21 files changed

+951
-2
lines changed

src/Verify2/Client.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66
use Vonage\Client\APIResource;
77
use Vonage\Client\Exception\Exception;
88
use Vonage\Client\Exception\Request;
9+
use Vonage\Entity\Hydrator\ArrayHydrator;
10+
use Vonage\Entity\IterableAPICollection;
11+
use Vonage\Verify2\Filters\TemplateFilter;
912
use Vonage\Verify2\Request\BaseVerifyRequest;
13+
use Vonage\Verify2\Request\CreateCustomTemplateFragmentRequest;
1014
use Vonage\Verify2\Request\SilentAuthRequest;
15+
use Vonage\Verify2\Request\UpdateCustomTemplateRequest;
16+
use Vonage\Verify2\VerifyObjects\Template;
17+
use Vonage\Verify2\VerifyObjects\TemplateFragment;
1118
use Vonage\Verify2\VerifyObjects\VerificationWorkflow;
1219

1320
class Client implements APIClient
@@ -74,4 +81,107 @@ public static function isSilentAuthRequest(BaseVerifyRequest $request): bool
7481

7582
return false;
7683
}
84+
85+
public function listCustomTemplates(TemplateFilter $filter = null): IterableAPICollection
86+
{
87+
$collection = $this->api->search($filter, '/templates');
88+
$collection->setNaiveCount(true);
89+
$collection->setPageIndexKey('page');
90+
91+
if (is_null($filter)) {
92+
$collection->setNoQueryParameters(true);
93+
}
94+
95+
$hydrator = new ArrayHydrator();
96+
$hydrator->setPrototype(new Template());
97+
$collection->setHydrator($hydrator);
98+
99+
return $collection;
100+
}
101+
102+
public function createCustomTemplate(string $name): Template
103+
{
104+
$response = $this->api->create([
105+
'name' => $name,
106+
], '/templates');
107+
108+
$template = new Template();
109+
return $template->fromArray($response);
110+
}
111+
112+
public function getCustomTemplate(string $templateId): Template
113+
{
114+
$response = $this->api->get('templates/' . $templateId);
115+
$template = new Template();
116+
return $template->fromArray($response);
117+
}
118+
119+
public function deleteCustomTemplate(string $templateId): bool
120+
{
121+
$this->api->delete('templates/' . $templateId);
122+
return true;
123+
}
124+
125+
public function updateCustomTemplate($templateId, UpdateCustomTemplateRequest $request): Template
126+
{
127+
$response = $this->api->partiallyUpdate('templates/' . $templateId, $request->toArray());
128+
$template = new Template();
129+
return $template->fromArray($response);
130+
}
131+
132+
public function createCustomTemplateFragment(string $templateId, CreateCustomTemplateFragmentRequest $createTemplateFragmentRequest): TemplateFragment
133+
{
134+
$response = $this->api->create($createTemplateFragmentRequest->toArray(), '/templates/' . $templateId . '/template_fragments');
135+
$templateFragment = new TemplateFragment();
136+
$templateFragment->fromArray($response);
137+
138+
return $templateFragment;
139+
}
140+
141+
public function getCustomTemplateFragment(string $templateId, string $fragmentId): TemplateFragment
142+
{
143+
$response = $this->api->get('templates/' . $templateId . '/template_fragments/' . $fragmentId);
144+
$templateFragment = new TemplateFragment();
145+
return $templateFragment->fromArray($response);
146+
}
147+
148+
public function updateCustomTemplateFragment(string $templateId, string $fragmentId, string $text): TemplateFragment
149+
{
150+
$response = $this->api->partiallyUpdate('templates/' . $templateId . '/template_fragments/' . $fragmentId, ['text' => $text]);
151+
$templateFragment = new TemplateFragment();
152+
return $templateFragment->fromArray($response);
153+
}
154+
155+
public function deleteCustomTemplateFragment(string $templateId, string $fragmentId): bool
156+
{
157+
$this->api->delete('templates/' . $templateId . '/template_fragments/' . $fragmentId);
158+
return true;
159+
}
160+
161+
public function listTemplateFragments(string $templateId, TemplateFilter $filter = null): IterableAPICollection
162+
{
163+
$api = clone $this->getAPIResource();
164+
$api->setCollectionName('template_fragments');
165+
166+
$collection = $api->search($filter, '/templates/' . $templateId . '/template_fragments');
167+
$collection->setNaiveCount(true);
168+
$collection->setPageIndexKey('page');
169+
170+
if (is_null($filter)) {
171+
$collection->setNoQueryParameters(true);
172+
}
173+
174+
if (!is_null($filter)) {
175+
if ($filter->getQuery()['page']) {
176+
$collection->setAutoAdvance(false);
177+
$collection->setIndex($filter->getQuery()['page']);
178+
}
179+
}
180+
181+
$hydrator = new ArrayHydrator();
182+
$hydrator->setPrototype(new TemplateFragment());
183+
$collection->setHydrator($hydrator);
184+
185+
return $collection;
186+
}
77187
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vonage\Verify2\Filters;
6+
7+
use Vonage\Entity\Filter\FilterInterface;
8+
9+
class TemplateFilter implements FilterInterface
10+
{
11+
protected ?int $pageSize = null;
12+
protected ?int $page = null;
13+
14+
public function getQuery(): array
15+
{
16+
$return = [];
17+
18+
if ($this->getPage()) {
19+
$return['page'] = $this->getPage();
20+
}
21+
22+
if ($this->getPageSize()) {
23+
$return['page_size'] = $this->getPageSize();
24+
}
25+
26+
return $return;
27+
}
28+
29+
public function getPageSize(): ?int
30+
{
31+
return $this->pageSize;
32+
}
33+
34+
public function setPageSize(int $pageSize): self
35+
{
36+
$this->pageSize = $pageSize;
37+
38+
return $this;
39+
}
40+
41+
public function getPage(): ?int
42+
{
43+
return $this->page;
44+
}
45+
46+
public function setPage(int $page): self
47+
{
48+
$this->page = $page;
49+
50+
return $this;
51+
}
52+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Vonage\Verify2\Request;
4+
5+
use Vonage\Entity\Hydrator\ArrayHydrateInterface;
6+
7+
class CreateCustomTemplateFragmentRequest implements ArrayHydrateInterface
8+
{
9+
protected const SMS_CHANNEL = 'sms';
10+
protected const VOICE_CHANNEL = 'voice';
11+
protected const EMAIL_CHANNEL = 'email';
12+
13+
protected array $permittedChannels = [
14+
self::SMS_CHANNEL,
15+
self::VOICE_CHANNEL,
16+
self::EMAIL_CHANNEL,
17+
];
18+
19+
public function __construct(
20+
protected string $channel,
21+
protected string $locale,
22+
protected string $text,
23+
) {
24+
if (!in_array($channel, $this->permittedChannels)) {
25+
throw new \InvalidArgumentException('Given channel not supported');
26+
}
27+
}
28+
29+
public function getChannel(): string
30+
{
31+
return $this->channel;
32+
}
33+
34+
public function setChannel(string $channel): CreateCustomTemplateFragmentRequest
35+
{
36+
if (!in_array($channel, $this->permittedChannels)) {
37+
throw new \InvalidArgumentException('Given channel not supported');
38+
}
39+
40+
$this->channel = $channel;
41+
return $this;
42+
}
43+
44+
public function getLocale(): string
45+
{
46+
return $this->locale;
47+
}
48+
49+
public function setLocale(string $locale): CreateCustomTemplateFragmentRequest
50+
{
51+
$this->locale = $locale;
52+
return $this;
53+
}
54+
55+
public function getText(): string
56+
{
57+
return $this->text;
58+
}
59+
60+
public function setText(string $text): CreateCustomTemplateFragmentRequest
61+
{
62+
$this->text = $text;
63+
return $this;
64+
}
65+
66+
public function fromArray(array $data): static
67+
{
68+
$this->setChannel($data['channel']);
69+
$this->setLocale($data['locale']);
70+
$this->setText($data['text']);
71+
72+
return $this;
73+
}
74+
75+
public function toArray(): array
76+
{
77+
return [
78+
'channel' => $this->getChannel(),
79+
'locale' => $this->getLocale(),
80+
'text' => $this->getText(),
81+
];
82+
}
83+
}

src/Verify2/Request/SMSRequest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace Vonage\Verify2\Request;
44

55
use InvalidArgumentException;
6+
use Vonage\Verify2\Traits\CustomTemplateTrait;
67
use Vonage\Verify2\VerifyObjects\VerificationLocale;
78
use Vonage\Verify2\VerifyObjects\VerificationWorkflow;
89

910
class SMSRequest extends BaseVerifyRequest
1011
{
12+
use CustomTemplateTrait;
13+
1114
public function __construct(
1215
protected string $to,
1316
protected string $brand,
@@ -36,6 +39,12 @@ public function __construct(
3639

3740
public function toArray(): array
3841
{
39-
return $this->getBaseVerifyUniversalOutputArray();
42+
$return = $this->getBaseVerifyUniversalOutputArray();
43+
44+
if (!is_null($this->getTemplateId())) {
45+
$return['template_id'] = $this->getTemplateId();
46+
}
47+
48+
return $return;
4049
}
4150
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Vonage\Verify2\Request;
4+
5+
use InvalidArgumentException;
6+
use Vonage\Verify2\VerifyObjects\VerificationLocale;
7+
use Vonage\Verify2\VerifyObjects\VerificationWorkflow;
8+
9+
class UpdateCustomTemplateRequest extends BaseVerifyRequest
10+
{
11+
public function __construct(
12+
protected ?string $name = null,
13+
protected ?bool $isDefault = null,
14+
) {
15+
}
16+
17+
public function getName(): ?string
18+
{
19+
return $this->name;
20+
}
21+
22+
public function setName(?string $name): UpdateCustomTemplateRequest
23+
{
24+
$this->name = $name;
25+
return $this;
26+
}
27+
28+
public function getIsDefault(): ?bool
29+
{
30+
return $this->isDefault;
31+
}
32+
33+
public function setIsDefault(?bool $isDefault): UpdateCustomTemplateRequest
34+
{
35+
$this->isDefault = $isDefault;
36+
return $this;
37+
}
38+
39+
public function toArray(): array
40+
{
41+
$return = [];
42+
43+
if ($this->getName()) {
44+
$return['name'] = $this->getName();
45+
}
46+
47+
if ($this->getIsDefault()) {
48+
$return['is_default'] = $this->getIsDefault();
49+
}
50+
51+
return $return;
52+
}
53+
}

src/Verify2/Request/VoiceRequest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace Vonage\Verify2\Request;
44

55
use InvalidArgumentException;
6+
use Vonage\Verify2\Traits\CustomTemplateTrait;
67
use Vonage\Verify2\VerifyObjects\VerificationLocale;
78
use Vonage\Verify2\VerifyObjects\VerificationWorkflow;
89

910
class VoiceRequest extends BaseVerifyRequest
1011
{
12+
use CustomTemplateTrait;
13+
1114
public function __construct(
1215
protected string $to,
1316
protected string $brand,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Vonage\Verify2\Traits;
4+
5+
trait CustomTemplateTrait
6+
{
7+
protected ?string $templateId = null;
8+
9+
public function getTemplateId(): ?string
10+
{
11+
return $this->templateId;
12+
}
13+
14+
public function setTemplateId(string $templateId): string
15+
{
16+
return $this->templateId = $templateId;
17+
}
18+
}

0 commit comments

Comments
 (0)