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