1: <?php
2: namespace Ctct\Services;
3:
4: use Ctct\Exceptions\CtctException;
5: use Ctct\Util\Config;
6: use Ctct\Components\EmailMarketing\Schedule;
7: use Ctct\Components\EmailMarketing\TestSend;
8: use GuzzleHttp\Exception\ClientException;
9: use GuzzleHttp\Stream\Stream;
10:
11: 12: 13: 14: 15: 16:
17: class CampaignScheduleService extends BaseService
18: {
19: 20: 21: 22: 23: 24: 25: 26:
27: public function addSchedule($accessToken, $campaignId, Schedule $schedule)
28: {
29: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_schedules'), $campaignId);
30:
31: $request = parent::createBaseRequest($accessToken, 'POST', $baseUrl);
32: $stream = Stream::factory(json_encode($schedule));
33: $request->setBody($stream);
34:
35: try {
36: $response = parent::getClient()->send($request);
37: } catch (ClientException $e) {
38: throw parent::convertException($e);
39: }
40:
41: return Schedule::create($response->json());
42: }
43:
44: 45: 46: 47: 48: 49: 50:
51: public function getSchedules($accessToken, $campaignId)
52: {
53: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_schedules'), $campaignId);
54:
55: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
56:
57: try {
58: $response = parent::getClient()->send($request);
59: } catch (ClientException $e) {
60: throw parent::convertException($e);
61: }
62:
63: $schedules = array();
64: foreach ($response->json() as $schedule) {
65: $schedules[] = Schedule::create($schedule);
66: }
67: return $schedules;
68: }
69:
70: 71: 72: 73: 74: 75: 76: 77:
78: public function getSchedule($accessToken, $campaignId, $scheduleId)
79: {
80: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_schedule'), $campaignId, $scheduleId);
81:
82: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
83:
84: try {
85: $response = parent::getClient()->send($request);
86: } catch (ClientException $e) {
87: throw parent::convertException($e);
88: }
89:
90: return Schedule::create($response->json());
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100:
101: public function updateSchedule($accessToken, $campaignId, Schedule $schedule)
102: {
103: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_schedule'), $campaignId, $schedule->id);
104:
105: $request = parent::createBaseRequest($accessToken, 'PUT', $baseUrl);
106: $stream = Stream::factory(json_encode($schedule));
107: $request->setBody($stream);
108:
109: try {
110: $response = parent::getClient()->send($request);
111: } catch (ClientException $e) {
112: throw parent::convertException($e);
113: }
114:
115: return Schedule::create($response->json());
116: }
117:
118: 119: 120: 121: 122: 123: 124: 125:
126: public function deleteSchedule($accessToken, $campaignId, $scheduleId)
127: {
128: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_schedule'), $campaignId, $scheduleId);
129:
130: $request = parent::createBaseRequest($accessToken, 'DELETE', $baseUrl);
131:
132: try {
133: $response = parent::getClient()->send($request);
134: } catch (ClientException $e) {
135: throw parent::convertException($e);
136: }
137:
138: return ($response->getStatusCode() == 204) ? true : false;
139: }
140:
141: 142: 143: 144: 145: 146: 147: 148:
149: public function sendTest($accessToken, $campaignId, TestSend $testSend)
150: {
151: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_test_sends'), $campaignId);
152:
153: $request = parent::createBaseRequest($accessToken, 'POST', $baseUrl);
154: $stream = Stream::factory(json_encode($testSend));
155: $request->setBody($stream);
156:
157: try {
158: $response = parent::getClient()->send($request);
159: } catch (ClientException $e) {
160: throw parent::convertException($e);
161: }
162:
163: return TestSend::create($response->json());
164: }
165: }
166: