Skip to content

Commit 6be466f

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 60cfd78 + 30a3d90 commit 6be466f

28 files changed

+1835
-0
lines changed

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<testsuite name="verify2">
2424
<directory>test/Verify2</directory>
2525
</testsuite>
26+
<testsuite name="proactive_connect">
27+
<directory>test/ProactiveConnect</directory>
28+
</testsuite>
2629
<testsuite name="voice">
2730
<directory>test/Voice</directory>
2831
</testsuite>

src/Entity/IterableAPICollection.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ class IterableAPICollection implements ClientAwareInterface, Iterator, Countable
4444

4545
protected APIResource $api;
4646

47+
/**
48+
* This allows for override if the endpoint API uses a different query key
49+
*/
50+
protected string $pageIndexKey = 'page_index';
51+
52+
/**
53+
* This allows for override if the endpoint API uses a different query key
54+
*/
55+
protected string $pageSizeKey = 'page_size';
56+
4757
/**
4858
* Determines if the collection will automatically go to the next page
4959
*/
@@ -123,6 +133,30 @@ class IterableAPICollection implements ClientAwareInterface, Iterator, Countable
123133

124134
protected $hydrator;
125135

136+
public function getPageIndexKey(): string
137+
{
138+
return $this->pageIndexKey;
139+
}
140+
141+
public function setPageIndexKey(string $pageIndexKey): IterableAPICollection
142+
{
143+
$this->pageIndexKey = $pageIndexKey;
144+
145+
return $this;
146+
}
147+
148+
public function getPageSizeKey(): string
149+
{
150+
return $this->pageSizeKey;
151+
}
152+
153+
public function setPageSizeKey(string $pageSizeKey): IterableAPICollection
154+
{
155+
$this->pageSizeKey = $pageSizeKey;
156+
157+
return $this;
158+
}
159+
126160
/**
127161
* @param $hydrator
128162
*

src/ProactiveConnect/Client.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
namespace Vonage\ProactiveConnect;
4+
5+
use Laminas\Diactoros\Stream;
6+
use Vonage\Client\APIClient;
7+
use Vonage\Client\APIResource;
8+
use Vonage\Entity\IterableAPICollection;
9+
use Vonage\ProactiveConnect\Objects\ListBaseObject;
10+
use Vonage\ProactiveConnect\Objects\ListItem;
11+
12+
class Client implements APIClient
13+
{
14+
public function __construct(protected APIResource $api)
15+
{
16+
}
17+
18+
public function getAPIResource(): APIResource
19+
{
20+
return $this->api;
21+
}
22+
23+
public function getLists(?int $page = null, ?int $pageSize = null): IterableAPICollection
24+
{
25+
$this->api->setCollectionName('lists');
26+
$lists = $this->api->search(null, '/lists');
27+
$lists->setPageIndexKey('page');
28+
29+
if ($page) {
30+
$lists->setPage($page);
31+
}
32+
33+
if ($pageSize) {
34+
$lists->setSize($pageSize);
35+
}
36+
37+
// This API has the potential to have a lot of data. Defaulting to
38+
// Auto advance off, you can override in the return object
39+
$lists->setAutoAdvance(false);
40+
41+
return $lists;
42+
}
43+
44+
public function createList(ListBaseObject $request): ?array
45+
{
46+
return $this->api->create($request->toArray(), '/lists');
47+
}
48+
49+
public function getListById(string $id)
50+
{
51+
return $this->api->get('lists/' . $id);
52+
}
53+
54+
public function updateList(string $id, ListBaseObject $request): ?array
55+
{
56+
return $this->api->update('lists/' . $id, $request->toArray());
57+
}
58+
59+
public function deleteList(string $id): ?array
60+
{
61+
return $this->api->delete('lists/' . $id);
62+
}
63+
64+
public function clearListItemsById(string $id): ?array
65+
{
66+
return $this->api->create([], '/lists/' . $id . '/clear');
67+
}
68+
69+
public function fetchListItemsById(string $id): ?array
70+
{
71+
return $this->api->create([], '/lists/' . $id . '/fetch');
72+
}
73+
74+
public function getItemsByListId(string $id, ?int $page = null, ?int $pageSize = null): IterableAPICollection
75+
{
76+
$this->api->setCollectionName('items');
77+
$lists = $this->api->search(null, '/lists/' . $id . '/items');
78+
79+
$lists->setPageIndexKey('page');
80+
81+
if ($page) {
82+
$lists->setPage($page);
83+
}
84+
85+
if ($pageSize) {
86+
$lists->setSize($pageSize);
87+
}
88+
89+
// This API has the potential to have a lot of data. Defaulting to
90+
// Auto advance off, you can override in the return object
91+
$lists->setAutoAdvance(false);
92+
93+
return $lists;
94+
}
95+
96+
public function createItemOnListId(string $id, ListItem $listItem): ?array
97+
{
98+
return $this->api->create($listItem->toArray(), '/lists/' . $id . '/items');
99+
}
100+
101+
public function getListCsvFileByListId(string $id): mixed
102+
{
103+
return $this->api->get('lists/' . $id . '/items/download', [], ['Content-Type' => 'text/csv'], false);
104+
}
105+
106+
public function getItemByIdandListId(string $itemId, string $listId)
107+
{
108+
return $this->api->get('lists/' . $listId . '/items/' . $itemId);
109+
}
110+
111+
public function updateItemByIdAndListId(string $itemId, string $listId, ListItem $listItem): ?array
112+
{
113+
return $this->api->update('/lists' . $listId . '/items/' . $itemId, $listItem->toArray());
114+
}
115+
116+
public function deleteItemByIdAndListId(string $itemId, string $listId): ?array
117+
{
118+
return $this->api->delete('lists/' . $listId . '/items/' . $itemId);
119+
}
120+
121+
public function uploadCsvToList(string $filename, string $listId)
122+
{
123+
$stream = new Stream(fopen($filename, 'r'));
124+
125+
$multipart = [
126+
[
127+
'name' => 'file',
128+
'contents' => $stream,
129+
'filename' => basename($filename)
130+
]
131+
];
132+
133+
return $this->api->create(
134+
[$multipart],
135+
'/lists/' . $listId . '/items/import',
136+
['Content-Type' => 'multipart/form-data']
137+
);
138+
}
139+
140+
public function getEvents(?int $page = null, ?int $pageSize = null)
141+
{
142+
$this->api->setCollectionName('events');
143+
$lists = $this->api->search(null, '/events');
144+
145+
$lists->setPageIndexKey('page');
146+
147+
if ($page) {
148+
$lists->setPage($page);
149+
}
150+
151+
if ($pageSize) {
152+
$lists->setSize($pageSize);
153+
}
154+
155+
// This API has the potential to have a lot of data. Defaulting to
156+
// Auto advance off, you can override in the return object
157+
$lists->setAutoAdvance(false);
158+
159+
return $lists;
160+
}
161+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Vonage\ProactiveConnect;
4+
5+
use Psr\Container\ContainerInterface;
6+
use Vonage\Client\APIResource;
7+
use Vonage\Client\Credentials\Handler\KeypairHandler;
8+
9+
class ClientFactory
10+
{
11+
public function __invoke(ContainerInterface $container): Client
12+
{
13+
$api = $container->make(APIResource::class);
14+
$api->setIsHAL(false)
15+
->setErrorsOn200(false)
16+
->setAuthHandler([new KeypairHandler()])
17+
->setBaseUrl('https://api-eu.vonage.com/v0.1/bulk/');
18+
19+
return new Client($api);
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Vonage\ProactiveConnect\Objects;
4+
5+
abstract class ListBaseObject
6+
{
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Vonage\ProactiveConnect\Objects;
4+
5+
use Vonage\Entity\Hydrator\ArrayHydrateInterface;
6+
7+
class ListItem implements ArrayHydrateInterface
8+
{
9+
public function __construct(protected array $data)
10+
{
11+
}
12+
13+
public function set($name, $value): static
14+
{
15+
$this->data[$name] = $value;
16+
17+
return $this;
18+
}
19+
20+
public function get(string $name)
21+
{
22+
return $this->data[$name];
23+
}
24+
25+
public function fromArray(array $data): void
26+
{
27+
$this->data = $data;
28+
}
29+
30+
public function toArray(): array
31+
{
32+
return [
33+
'data' => $this->data
34+
];
35+
}
36+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Vonage\ProactiveConnect\Objects;
4+
5+
use Vonage\Entity\Hydrator\ArrayHydrateInterface;
6+
7+
class ManualList extends ListBaseObject implements ArrayHydrateInterface
8+
{
9+
protected ?string $description = null;
10+
protected ?array $tags = null;
11+
protected ?array $attributes = null;
12+
protected array $datasource = ['type' => 'manual'];
13+
14+
public function __construct(protected string $name)
15+
{
16+
}
17+
18+
public function getDescription(): ?string
19+
{
20+
return $this->description;
21+
}
22+
23+
public function setDescription(?string $description): ManualList
24+
{
25+
$this->description = $description;
26+
27+
return $this;
28+
}
29+
30+
public function getTags(): ?array
31+
{
32+
return $this->tags;
33+
}
34+
35+
public function setTags(?array $tags): ManualList
36+
{
37+
$this->tags = $tags;
38+
39+
return $this;
40+
}
41+
42+
public function getAttributes(): ?array
43+
{
44+
return $this->attributes;
45+
}
46+
47+
public function setAttributes(?array $attributes): ManualList
48+
{
49+
$this->attributes = $attributes;
50+
51+
return $this;
52+
}
53+
54+
public function getDatasource(): array
55+
{
56+
return $this->datasource;
57+
}
58+
59+
public function fromArray(array $data): static
60+
{
61+
return $this;
62+
}
63+
64+
/**
65+
* @return string
66+
*/
67+
public function getName(): string
68+
{
69+
return $this->name;
70+
}
71+
72+
public function setName(string $name): ManualList
73+
{
74+
$this->name = $name;
75+
76+
return $this;
77+
}
78+
79+
public function toArray(): array
80+
{
81+
$returnArray = [
82+
'name' => $this->getName(),
83+
'datasource' => $this->getDatasource(),
84+
'description' => $this->getDescription() ?: null,
85+
'tags' => $this->getTags() ?: null,
86+
'attributes' => $this->getAttributes() ?: null
87+
];
88+
89+
return array_filter($returnArray, function ($value) {
90+
return $value !== null;
91+
});
92+
}
93+
}

0 commit comments

Comments
 (0)