1: <?php
2: namespace Ctct\Services;
3:
4: use Ctct\Exceptions\CtctException;
5: use Ctct\Util\Config;
6: use Ctct\Components\Contacts\Contact;
7: use Ctct\Components\ResultSet;
8: use GuzzleHttp\Exception\ClientException;
9: use GuzzleHttp\Stream\Stream;
10:
11: 12: 13: 14: 15: 16:
17: class ContactService extends BaseService
18: {
19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
32: public function getContacts($accessToken, Array $params = array())
33: {
34: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.contacts');
35:
36: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
37: if ($params) {
38: $query = $request->getQuery();
39: foreach ($params as $name => $value) {
40: $query->add($name, $value);
41: }
42: }
43:
44: try {
45: $response = parent::getClient()->send($request);
46: } catch (ClientException $e) {
47: throw parent::convertException($e);
48: }
49:
50: $body = $response->json();
51: $contacts = array();
52: foreach ($body['results'] as $contact) {
53: $contacts[] = Contact::create($contact);
54: }
55: return new ResultSet($contacts, $body['meta']);
56: }
57:
58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71:
72: public function getContactsFromList($accessToken, $listId, Array $params = array())
73: {
74: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.list_contacts'), $listId);
75:
76: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
77: if ($params) {
78: $query = $request->getQuery();
79: foreach ($params as $name => $value) {
80: $query->add($name, $value);
81: }
82: }
83:
84: try {
85: $response = parent::getClient()->send($request);
86: } catch (ClientException $e) {
87: throw parent::convertException($e);
88: }
89:
90: $body = $response->json();
91: $contacts = array();
92: foreach ($body['results'] as $contact) {
93: $contacts[] = Contact::create($contact);
94: }
95: return new ResultSet($contacts, $body['meta']);
96: }
97:
98: 99: 100: 101: 102: 103: 104:
105: public function getContact($accessToken, $contactId)
106: {
107: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contactId);
108:
109: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
110:
111: try {
112: $response = parent::getClient()->send($request);
113: } catch (ClientException $e) {
114: throw parent::convertException($e);
115: }
116:
117: return Contact::create($response->json());
118: }
119:
120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130:
131: public function addContact($accessToken, Contact $contact, Array $params = array())
132: {
133: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.contacts');
134:
135: $request = parent::createBaseRequest($accessToken, 'POST', $baseUrl);
136: if ($params) {
137: $query = $request->getQuery();
138: foreach ($params as $name => $value) {
139: $query->add($name, $value);
140: }
141: }
142: $stream = Stream::factory(json_encode($contact));
143: $request->setBody($stream);
144:
145: try {
146: $response = parent::getClient()->send($request);
147: } catch (ClientException $e) {
148: throw parent::convertException($e);
149: }
150:
151: return Contact::create($response->json());
152: }
153:
154: 155: 156: 157: 158: 159: 160:
161: public function unsubscribeContact($accessToken, $contactId) {
162: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contactId);
163:
164: $request = parent::createBaseRequest($accessToken, 'DELETE', $baseUrl);
165:
166: try {
167: $response = parent::getClient()->send($request);
168: } catch (ClientException $e) {
169: throw parent::convertException($e);
170: }
171:
172: return ($response->getStatusCode() == 204) ? true : false;
173: }
174:
175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185:
186: public function updateContact($accessToken, Contact $contact, Array $params = array())
187: {
188: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact->id);
189:
190: $request = parent::createBaseRequest($accessToken, 'PUT', $baseUrl);
191: if ($params) {
192: $query = $request->getQuery();
193: foreach ($params as $name => $value) {
194: $query->add($name, $value);
195: }
196: }
197:
198: try {
199: $response = parent::getClient()->send($request);
200: } catch (ClientException $e) {
201: throw parent::convertException($e);
202: }
203:
204: return Contact::create($response->json());
205: }
206: }
207: