|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sources. |
|
4
|
|
|
* Date: 14/08/15 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Mailxpert; |
|
8
|
|
|
|
|
9
|
|
|
use Mailxpert\Authentication\AccessToken; |
|
10
|
|
|
use Mailxpert\Authentication\OAuth2Client; |
|
11
|
|
|
use Mailxpert\Exceptions\MailxpertSDKException; |
|
12
|
|
|
use Mailxpert\Helpers\MailxpertLoginHelper; |
|
13
|
|
|
use Mailxpert\HttpClients\MailxpertHttpClientInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Mailxpert |
|
17
|
|
|
* |
|
18
|
|
|
* @package Mailxpert |
|
19
|
|
|
*/ |
|
20
|
|
|
class Mailxpert |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @const string The name of the environment variable that contains the app ID. |
|
24
|
|
|
*/ |
|
25
|
|
|
const APP_ID_ENV_NAME = 'MAILXPERT_APP_ID'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @const string The name of the environment variable that contains the app secret. |
|
29
|
|
|
*/ |
|
30
|
|
|
const APP_SECRET_ENV_NAME = 'MAILXPERT_APP_SECRET'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var MailxpertApp |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $app; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var MailxpertClient |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $client; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $oauthBaseUrl; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $apiBaseUrl; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var OAuth2Client |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $oAuth2Client; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var MailxpertResponse |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $lastResponse; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var AccessToken |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $accessToken; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array $config |
|
69
|
|
|
* |
|
70
|
|
|
* @throws MailxpertSDKException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __construct(array $config = []) |
|
73
|
|
|
{ |
|
74
|
|
|
$appId = isset($config['app_id']) ? $config['app_id'] : getenv(static::APP_ID_ENV_NAME); |
|
75
|
|
|
if (!$appId) { |
|
76
|
|
|
throw new MailxpertSDKException( |
|
77
|
|
|
'Required "app_id" key not supplied in config and could not find fallback environment variable "'.static::APP_ID_ENV_NAME.'"' |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$appSecret = isset($config['app_secret']) ? $config['app_secret'] : getenv(static::APP_SECRET_ENV_NAME); |
|
82
|
|
|
if (!$appSecret) { |
|
83
|
|
|
throw new MailxpertSDKException( |
|
84
|
|
|
'Required "app_secret" key not supplied in config and could not find fallback environment variable "'.static::APP_SECRET_ENV_NAME.'"' |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->app = new MailxpertApp($appId, $appSecret); |
|
89
|
|
|
|
|
90
|
|
|
if (isset($config['api_base_url'])) { |
|
91
|
|
|
$this->apiBaseUrl = $config['api_base_url']; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$httpClientHandler = null; |
|
95
|
|
|
if (isset($config['http_client'])) { |
|
96
|
|
|
$httpClientHandler = $config['http_client']; |
|
97
|
|
|
|
|
98
|
|
|
if (!$httpClientHandler instanceof MailxpertHttpClientInterface) { |
|
99
|
|
|
throw new MailxpertSDKException('Config "http_client" should be an instance of MailxpertHttpClientInterface'); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$this->client = new MailxpertClient($httpClientHandler, $this->apiBaseUrl); |
|
104
|
|
|
|
|
105
|
|
|
if (isset($config['oauth_base_url'])) { |
|
106
|
|
|
$this->oauthBaseUrl = $config['oauth_base_url']; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (isset($config['access_token']) && $config['access_token'] instanceof AccessToken) { |
|
110
|
|
|
$this->accessToken = $config['access_token']; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return MailxpertApp |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getApp() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->app; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return MailxpertClient |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getClient() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->client; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return MailxpertLoginHelper |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getLoginHelper() |
|
134
|
|
|
{ |
|
135
|
|
|
return new MailxpertLoginHelper( |
|
136
|
|
|
$this->getOAuth2Client() |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $method |
|
142
|
|
|
* @param string $endpoint |
|
143
|
|
|
* @param array $params |
|
144
|
|
|
* @param string|null $accessToken |
|
145
|
|
|
* @param string|null $body |
|
146
|
|
|
* |
|
147
|
|
|
* @return MailxpertResponse |
|
148
|
|
|
* @throws MailxpertSDKException |
|
149
|
|
|
*/ |
|
150
|
|
|
public function sendRequest($method, $endpoint, array $params = [], $accessToken = null, $body = null) |
|
151
|
|
|
{ |
|
152
|
|
|
$accessToken = $accessToken ? $accessToken : $this->accessToken; |
|
153
|
|
|
|
|
154
|
|
|
$request = $this->request($method, $endpoint, $params, $accessToken, $body); |
|
155
|
|
|
|
|
156
|
|
|
return $this->lastResponse = $this->client->sendRequest($request); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string $method |
|
161
|
|
|
* @param string $endpoint |
|
162
|
|
|
* @param array $params |
|
163
|
|
|
* @param AccessToken|string|null $accessToken |
|
164
|
|
|
* @param string|null $body |
|
165
|
|
|
* |
|
166
|
|
|
* @return MailxpertRequest |
|
167
|
|
|
*/ |
|
168
|
|
|
public function request($method, $endpoint, array $params = [], $accessToken = null, $body = null) |
|
169
|
|
|
{ |
|
170
|
|
|
return new MailxpertRequest($this->app, $accessToken, $method, $endpoint, $params, $body); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param AccessToken $accessToken |
|
175
|
|
|
*/ |
|
176
|
|
|
public function setAccessToken(AccessToken $accessToken) |
|
177
|
|
|
{ |
|
178
|
|
|
$this->accessToken = $accessToken; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return AccessToken |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getAccessToken() |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->accessToken; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
private function getOAuth2Client() |
|
190
|
|
|
{ |
|
191
|
|
|
if (!$this->oAuth2Client instanceof OAuth2Client) { |
|
192
|
|
|
$app = $this->getApp(); |
|
193
|
|
|
$client = $this->getClient(); |
|
194
|
|
|
$this->oAuth2Client = new OAuth2Client($app, $client, $this->oauthBaseUrl); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $this->oAuth2Client; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|