1: <?php
2: namespace Ctct\Services;
3:
4: use Ctct\Exceptions\CtctException;
5: use Ctct\Util\Config;
6: use Ctct\Components\Account\VerifiedEmailAddress;
7: use Ctct\Components\Account\AccountInfo;
8: use GuzzleHttp\Exception\ClientException;
9: use GuzzleHttp\Stream\Stream;
10:
11: 12: 13: 14: 15: 16:
17: class AccountService extends BaseService
18: {
19: 20: 21: 22: 23: 24: 25: 26: 27:
28: public function getVerifiedEmailAddresses($accessToken, Array $params = array())
29: {
30: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.account_verified_addresses');
31:
32: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
33: if ($params) {
34: $query = $request->getQuery();
35: foreach ($params as $name => $value) {
36: $query->add($name, $value);
37: }
38: }
39:
40: try {
41: $response = parent::getClient()->send($request);
42: } catch (ClientException $e) {
43: throw parent::convertException($e);
44: }
45:
46: $verifiedAddresses = array();
47: foreach ($response->json() as $verifiedAddress) {
48: $verifiedAddresses[] = VerifiedEmailAddress::create($verifiedAddress);
49: }
50:
51: return $verifiedAddresses;
52: }
53:
54: 55: 56: 57: 58: 59: 60: 61:
62: public function createVerifiedEmailAddress($accessToken, $emailAddress)
63: {
64: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.account_verified_addresses');
65:
66: $request = parent::createBaseRequest($accessToken, 'POST', $baseUrl);
67: $stream = Stream::factory(json_encode(array(array("email_address" => $emailAddress))));
68: $request->setBody($stream);
69:
70: try {
71: $response = parent::getClient()->send($request);
72: } catch (ClientException $e) {
73: throw parent::convertException($e);
74: }
75:
76: $verifiedAddresses = array();
77: foreach ($response->json() as $verifiedAddress) {
78: $verifiedAddresses[] = VerifiedEmailAddress::create($verifiedAddress);
79: }
80:
81: return $verifiedAddresses;
82: }
83:
84: 85: 86: 87: 88: 89:
90: public function getAccountInfo($accessToken)
91: {
92: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.account_info');
93:
94: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
95:
96: try {
97: $response = parent::getClient()->send($request);
98: } catch (ClientException $e) {
99: throw parent::convertException($e);
100: }
101:
102: return AccountInfo::create($response->json());
103: }
104:
105: 106: 107: 108: 109: 110: 111:
112: public function updateAccountInfo($accessToken, AccountInfo $accountInfo)
113: {
114: $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.account_info');
115:
116: $request = parent::createBaseRequest($accessToken, 'PUT', $baseUrl);
117: $stream = Stream::factory(json_encode($accountInfo));
118: $request->setBody($stream);
119:
120: try {
121: $response = parent::getClient()->send($request);
122: } catch (ClientException $e) {
123: throw parent::convertException($e);
124: }
125:
126: return AccountInfo::create($response->json());
127: }
128: }
129: