1: <?php
2: namespace Ctct\Services;
3:
4: use Ctct\Exceptions\CtctException;
5: use Ctct\Components\Library\File;
6: use Ctct\Components\Library\FileUploadStatus;
7: use Ctct\Components\Library\Folder;
8: use Ctct\Components\ResultSet;
9: use Ctct\Exceptions\IllegalArgumentException;
10: use Ctct\Util\Config;
11: use GuzzleHttp\Exception\ClientException;
12: use GuzzleHttp\Post\PostBody;
13: use GuzzleHttp\Post\PostFile;
14:
15: class LibraryService extends BaseService
16: {
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
31: public function getLibraryFiles($accessToken, Array $params = array())
32: {
33: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.library_files');
34:
35: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
36: if ($params) {
37: $query = $request->getQuery();
38: foreach ($params as $name => $value) {
39: $query->add($name, $value);
40: }
41: }
42:
43: try {
44: $response = parent::getClient()->send($request);
45: } catch (ClientException $e) {
46: throw parent::convertException($e);
47: }
48:
49: $body = $response->json();
50: $libraryFiles = array();
51: foreach ($body['results'] as $file) {
52: $libraryFiles[] = File::create($file);
53: }
54:
55: return new ResultSet($libraryFiles, $body['meta']);
56: }
57:
58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72:
73: public function getLibraryFilesByFolder($accessToken, $folderId, Array $params = array())
74: {
75: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.library_files_by_folder'), $folderId);
76:
77: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
78: if ($params) {
79: $query = $request->getQuery();
80: foreach ($params as $name => $value) {
81: $query->add($name, $value);
82: }
83: }
84:
85: try {
86: $response = parent::getClient()->send($request);
87: } catch (ClientException $e) {
88: throw parent::convertException($e);
89: }
90:
91: $body = $response->json();
92: $libraryFiles = array();
93: foreach ($body['results'] as $file) {
94: $libraryFiles[] = File::create($file);
95: }
96:
97: return new ResultSet($libraryFiles, $body['meta']);
98: }
99:
100: 101: 102: 103: 104: 105: 106:
107: public function getLibraryFile($accessToken, $fileId)
108: {
109: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.library_file'), $fileId);
110:
111: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
112:
113: try {
114: $response = parent::getClient()->send($request);
115: } catch (ClientException $e) {
116: throw parent::convertException($e);
117: }
118:
119: return File::create($response->json());
120: }
121:
122: 123: 124: 125: 126: 127: 128:
129: public function deleteLibraryFile($accessToken, $fileId)
130: {
131: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.library_file'), $fileId);
132:
133: $request = parent::createBaseRequest($accessToken, 'DELETE', $baseUrl);
134:
135: try {
136: $response = parent::getClient()->send($request);
137: } catch (ClientException $e) {
138: throw parent::convertException($e);
139: }
140:
141: return ($response->getStatusCode() == 204) ? true : false;
142: }
143:
144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154:
155: public function getLibraryFolders($accessToken, Array $params = array())
156: {
157: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.library_folders');
158:
159: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
160: if ($params) {
161: $query = $request->getQuery();
162: foreach ($params as $name => $value) {
163: $query->add($name, $value);
164: }
165: }
166:
167: try {
168: $response = parent::getClient()->send($request);
169: } catch (ClientException $e) {
170: throw parent::convertException($e);
171: }
172:
173: $body = $response->json();
174: $libraryFolders = array();
175: foreach ($body['results'] as $folder) {
176: $libraryFolders[] = Folder::create($folder);
177: }
178:
179: return new ResultSet($libraryFolders, $body['meta']);
180: }
181:
182: 183: 184: 185: 186: 187: 188:
189: public function getLibraryFolder($accessToken, $folderId)
190: {
191: $baseUrl = Config::get('endpoints.base_url') . Config::get(sprintf('endpoints.library_folder', $folderId));
192:
193: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
194:
195: try {
196: $response = parent::getClient()->send($request);
197: } catch (ClientException $e) {
198: throw parent::convertException($e);
199: }
200:
201: $body = $response->json();
202: return Folder::create($body);
203: }
204:
205: 206: 207: 208: 209: 210: 211:
212: public function deleteLibraryFolder($accessToken, $folderId)
213: {
214: $baseUrl = Config::get('endpoints.base_url') . Config::get(sprintf('endpoints.library_folder', $folderId));
215:
216: $request = parent::createBaseRequest($accessToken, 'DELETE', $baseUrl);
217:
218: try {
219: $response = parent::getClient()->send($request);
220: } catch (ClientException $e) {
221: throw parent::convertException($e);
222: }
223:
224: return ($response->getStatusCode() == 204) ? true : false;
225: }
226:
227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239:
240: public function uploadFile($accessToken, $fileName, $fileLocation, $description, $source, $folderId)
241: {
242: $finfo = finfo_open(FILEINFO_MIME_TYPE);
243: $mime = finfo_file($finfo, $fileLocation);
244: finfo_close($finfo);
245:
246: if ($mime == "image/png") {
247: $fileType = "PNG";
248: } elseif ($mime = "image/jpeg") {
249: $fileType = "JPG";
250: } elseif ($mime = "image/gif") {
251: $fileType = "GIF";
252: } elseif ($mime ="application/pdf") {
253: $fileType = "PDF";
254: } else {
255: throw new IllegalArgumentException(sprintf(Config::get('errors.file_extension'), "PNG, JPG, JPEG, GIF, PDF was " . $mime));
256: }
257:
258: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.library_files');
259: $request = parent::createBaseRequest($accessToken, "POST", $baseUrl);
260: $request->setHeader("Content-Type", "multipart/form-data");
261:
262: $body = new PostBody();
263: $body->setField("folderId", $folderId);
264: $body->setField("file_name", $fileName);
265: $body->setField("file_type", $fileType);
266: $body->setField("description", $description);
267: $body->setField("source", $source);
268: $body->addFile(new PostFile("data", fopen($fileLocation, 'r'), $fileName));
269: $request->setBody($body);
270:
271: try {
272: $response = parent::getClient()->send($request);
273: } catch (ClientException $e) {
274: throw parent::convertException($e);
275: }
276:
277: return $response->getHeader("Id");
278: }
279:
280: 281: 282: 283: 284: 285: 286:
287: public function getFileUploadStatus($accessToken, $uploadStatusIds)
288: {
289: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.library_file_upload_status'), $uploadStatusIds);
290: $request = parent::createBaseRequest($accessToken, "GET", $baseUrl);
291:
292: try {
293: $response = parent::getClient()->send($request);
294: } catch (ClientException $e) {
295: throw parent::convertException($e);
296: }
297:
298: $fileUploadStatuses = array();
299: foreach ($response->json() as $fileUploadStatus) {
300: $fileUploadStatuses[] = FileUploadStatus::create($fileUploadStatus);
301: }
302: return $fileUploadStatuses;
303: }
304: }