1: <?php
2: namespace Ctct\Services;
3:
4: use Ctct\Exceptions\CtctException;
5: use Ctct\Util\Config;
6: use Ctct\Components\Activities\Activity;
7: use Ctct\Components\Activities\AddContacts;
8: use Ctct\Components\Activities\ExportContacts;
9: use GuzzleHttp\Exception\ClientException;
10: use GuzzleHttp\Post\PostBody;
11: use GuzzleHttp\Post\PostFile;
12: use GuzzleHttp\Stream\Stream;
13:
14: 15: 16: 17: 18: 19:
20: class ActivityService extends BaseService
21: {
22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33: public function getActivities($accessToken, Array $params = array())
34: {
35: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.activities');
36:
37: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
38: if ($params) {
39: $query = $request->getQuery();
40: foreach ($params as $name => $value) {
41: $query->add($name, $value);
42: }
43: }
44:
45: try {
46: $response = parent::getClient()->send($request);
47: } catch (ClientException $e) {
48: throw parent::convertException($e);
49: }
50:
51: $activities = array();
52: foreach ($response->json() as $activity) {
53: $activities[] = Activity::create($activity);
54: }
55: return $activities;
56: }
57:
58: 59: 60: 61: 62: 63: 64:
65: public function getActivity($accessToken, $activityId)
66: {
67: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.activity'), $activityId);
68:
69: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
70:
71: try {
72: $response = parent::getClient()->send($request);
73: } catch (ClientException $e) {
74: throw parent::convertException($e);
75: }
76:
77: return Activity::create($response->json());
78: }
79:
80: 81: 82: 83: 84: 85: 86:
87: public function createAddContactsActivity($accessToken, AddContacts $addContacts)
88: {
89: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.add_contacts_activity');
90:
91: $request = parent::createBaseRequest($accessToken, 'POST', $baseUrl);
92: $stream = Stream::factory(json_encode($addContacts));
93: $request->setBody($stream);
94:
95: try {
96: $response = parent::getClient()->send($request);
97: } catch (ClientException $e) {
98: throw parent::convertException($e);
99: }
100:
101: return Activity::create($response->json());
102: }
103:
104: 105: 106: 107: 108: 109: 110: 111: 112:
113: public function createAddContactsActivityFromFile($accessToken, $fileName, $fileLocation, $lists)
114: {
115: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.add_contacts_activity');
116: $request = parent::createBaseRequest($accessToken, "POST", $baseUrl);
117: $request->setHeader("Content-Type", "multipart/form-data");
118:
119: $body = new PostBody();
120: $body->setField("lists", $lists);
121: $body->setField("file_name", $fileName);
122: $body->addFile(new PostFile("data", fopen($fileLocation, 'r'), $fileName));
123: $request->setBody($body);
124:
125: try {
126: $response = parent::getClient()->send($request);
127: } catch (ClientException $e) {
128: throw parent::convertException($e);
129: }
130:
131: return Activity::create($response->json());
132: }
133:
134: 135: 136: 137: 138: 139: 140: 141:
142: public function addClearListsActivity($accessToken, Array $lists)
143: {
144: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.clear_lists_activity');
145: $request = parent::createBaseRequest($accessToken, "POST", $baseUrl);
146: $stream = Stream::factory(json_encode(array("lists" => $lists)));
147: $request->setBody($stream);
148:
149: try {
150: $response = parent::getClient()->send($request);
151: } catch (ClientException $e) {
152: throw parent::convertException($e);
153: }
154:
155: return Activity::create($response->json());
156: }
157:
158: 159: 160: 161: 162: 163: 164:
165: public function addExportContactsActivity($accessToken, ExportContacts $exportContacts)
166: {
167: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.export_contacts_activity');
168:
169: $request = parent::createBaseRequest($accessToken, 'POST', $baseUrl);
170: $stream = Stream::factory(json_encode($exportContacts));
171: $request->setBody($stream);
172:
173: try {
174: $response = parent::getClient()->send($request);
175: } catch (ClientException $e) {
176: throw parent::convertException($e);
177: }
178:
179: return Activity::create($response->json());
180: }
181:
182: 183: 184: 185: 186: 187: 188: 189:
190: public function addRemoveContactsFromListsActivity($accessToken, Array $emailAddresses, Array $lists)
191: {
192: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.remove_from_lists_activity');
193: $request = parent::createBaseRequest($accessToken, "POST", $baseUrl);
194:
195: $payload = array(
196: 'import_data' => array(),
197: 'lists' => $lists
198: );
199: foreach($emailAddresses as $emailAddress) {
200: $payload['import_data'][] = array('email_addresses' => array($emailAddress));
201: }
202:
203: $stream = Stream::factory(json_encode($payload));
204: $request->setBody($stream);
205:
206: try {
207: $response = parent::getClient()->send($request);
208: } catch (ClientException $e) {
209: throw parent::convertException($e);
210: }
211:
212: return Activity::create($response->json());
213: }
214:
215: 216: 217: 218: 219: 220: 221: 222: 223:
224: public function addRemoveContactsFromListsActivityFromFile($accessToken, $fileName, $fileLocation, $lists)
225: {
226: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.remove_from_lists_activity');
227: $request = parent::createBaseRequest($accessToken, "POST", $baseUrl);
228: $request->setHeader("Content-Type", "multipart/form-data");
229:
230: $body = new PostBody();
231: $body->setField("lists", $lists);
232: $body->setField("file_name", $fileName);
233: $body->addFile(new PostFile("data", fopen($fileLocation, 'r'), $fileName));
234: $request->setBody($body);
235:
236: try {
237: $response = parent::getClient()->send($request);
238: } catch (ClientException $e) {
239: throw parent::convertException($e);
240: }
241:
242: return Activity::create($response->json());
243: }
244: }
245: