1: <?php
2: namespace Ctct\Services;
3:
4: use Ctct\Exceptions\CtctException;
5: use Ctct\Util\Config;
6: use Ctct\Components\Contacts\ContactList;
7: use GuzzleHttp\Exception\ClientException;
8: use GuzzleHttp\Stream\Stream;
9:
10: 11: 12: 13: 14: 15:
16: class ListService extends BaseService
17: {
18: 19: 20: 21: 22: 23: 24: 25: 26:
27: public function getLists($accessToken, Array $params = array())
28: {
29: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.lists');
30:
31: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
32: if ($params) {
33: $query = $request->getQuery();
34: foreach ($params as $name => $value) {
35: $query->add($name, $value);
36: }
37: }
38:
39: try {
40: $response = parent::getClient()->send($request);
41: } catch (ClientException $e) {
42: throw parent::convertException($e);
43: }
44:
45: $lists = array();
46: foreach ($response->json() as $contact) {
47: $lists[] = ContactList::create($contact);
48: }
49:
50: return $lists;
51: }
52:
53: 54: 55: 56: 57: 58: 59:
60: public function addList($accessToken, ContactList $list)
61: {
62: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.lists');
63:
64: $request = parent::createBaseRequest($accessToken, 'POST', $baseUrl);
65: $stream = Stream::factory(json_encode($list));
66: $request->setBody($stream);
67:
68: try {
69: $response = parent::getClient()->send($request);
70: } catch (ClientException $e) {
71: throw parent::convertException($e);
72: }
73:
74: return ContactList::create($response->json());
75: }
76:
77: 78: 79: 80: 81: 82: 83:
84: public function updateList($accessToken, ContactList $list)
85: {
86: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.list'), $list->id);
87:
88: $request = parent::createBaseRequest($accessToken, 'PUT', $baseUrl);
89: $stream = Stream::factory(json_encode($list));
90: $request->setBody($stream);
91:
92: try {
93: $response = parent::getClient()->send($request);
94: } catch (ClientException $e) {
95: throw parent::convertException($e);
96: }
97:
98: return ContactList::create($response->json());
99: }
100:
101: 102: 103: 104: 105: 106: 107:
108: public function deleteList($accessToken, $listId)
109: {
110: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.list'), $listId);
111:
112: $request = parent::createBaseRequest($accessToken, 'DELETE', $baseUrl);
113:
114: try {
115: $response = parent::getClient()->send($request);
116: } catch (ClientException $e) {
117: throw parent::convertException($e);
118: }
119:
120: return ($response->getStatusCode() == 204) ? true : false;
121: }
122:
123: 124: 125: 126: 127: 128: 129:
130: public function getList($accessToken, $listId)
131: {
132: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.list'), $listId);
133:
134: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
135:
136: try {
137: $response = parent::getClient()->send($request);
138: } catch (ClientException $e) {
139: throw parent::convertException($e);
140: }
141:
142: return ContactList::create($response->json());
143: }
144: }
145: