|
| 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 | +} |
0 commit comments