1 | <?php |
||
35 | class Authorization |
||
36 | { |
||
37 | const CACHE_KEY_ACCESS_TOKEN = 'easywechat.open_platform.authorizer_access_token'; |
||
38 | const CACHE_KEY_REFRESH_TOKEN = 'easywechat.open_platform.authorizer_refresh_token'; |
||
39 | |||
40 | /** |
||
41 | * Cache. |
||
42 | * |
||
43 | * @var \Doctrine\Common\Cache\Cache |
||
44 | */ |
||
45 | protected $cache; |
||
46 | |||
47 | /** |
||
48 | * Base API. |
||
49 | * |
||
50 | * @var \EasyWeChat\OpenPlatform\Api\BaseApi |
||
51 | */ |
||
52 | private $api; |
||
53 | |||
54 | /** |
||
55 | * Open Platform App Id, aka, Component App Id. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | private $appId; |
||
60 | |||
61 | /** |
||
62 | * Authorizer App Id. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | private $authorizerAppId; |
||
67 | |||
68 | /** |
||
69 | * Auth code. |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | private $authCode; |
||
74 | |||
75 | /** |
||
76 | * Authorization Constructor. |
||
77 | * |
||
78 | * Users need not concern the details. |
||
79 | * |
||
80 | * @param \EasyWeChat\OpenPlatform\Api\BaseApi $api |
||
81 | * @param string $appId |
||
82 | * @param \Doctrine\Common\Cache\Cache $cache |
||
83 | */ |
||
84 | 13 | public function __construct(BaseApi $api, $appId, Cache $cache) |
|
90 | |||
91 | /** |
||
92 | * Gets the base api. |
||
93 | * |
||
94 | * @return \EasyWeChat\OpenPlatform\Api\BaseApi |
||
95 | */ |
||
96 | public function getApi() |
||
100 | |||
101 | /** |
||
102 | * Sets the authorizer app id. |
||
103 | * |
||
104 | * @param string $authorizerAppId |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | 11 | public function setAuthorizerAppId($authorizerAppId) |
|
114 | |||
115 | /** |
||
116 | * Gets the authorizer app id, or throws if not found. |
||
117 | * |
||
118 | * @return string |
||
119 | * |
||
120 | * @throws \EasyWeChat\Core\Exception |
||
121 | */ |
||
122 | 9 | public function getAuthorizerAppId() |
|
132 | |||
133 | /** |
||
134 | * Sets the auth code. |
||
135 | * |
||
136 | * @param $code |
||
137 | */ |
||
138 | public function setAuthCode($code) |
||
142 | |||
143 | /** |
||
144 | * Gets the auth code. |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | 4 | public function getAuthCode() |
|
152 | |||
153 | /** |
||
154 | * Sets the auth info from the message of the auth event sent by WeChat. |
||
155 | * |
||
156 | * @param \EasyWeChat\Support\Collection $message |
||
157 | */ |
||
158 | public function setFromAuthMessage(Collection $message) |
||
167 | |||
168 | /** |
||
169 | * Handles authorization: calls the API, saves the tokens. |
||
170 | * |
||
171 | * @return Collection |
||
172 | */ |
||
173 | 2 | public function handleAuthorization() |
|
190 | |||
191 | /** |
||
192 | * Gets the authorization information. |
||
193 | * Like authorizer app id, access token, refresh token, function scope, etc. |
||
194 | * |
||
195 | * @return \EasyWeChat\Support\Collection |
||
196 | */ |
||
197 | 4 | public function getAuthorizationInfo() |
|
201 | |||
202 | /** |
||
203 | * Gets the authorizer information. |
||
204 | * Like authorizer name, logo, business, etc. |
||
205 | * |
||
206 | * @return \EasyWeChat\Support\Collection |
||
207 | */ |
||
208 | 4 | public function getAuthorizerInfo() |
|
212 | |||
213 | /** |
||
214 | * Saves the authorizer access token in cache. |
||
215 | * |
||
216 | * @param string $token |
||
217 | * |
||
218 | * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise |
||
219 | */ |
||
220 | 4 | public function setAuthorizerAccessToken($token, $expires = 7200) |
|
224 | |||
225 | /** |
||
226 | * Gets the authorizer access token. |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | 4 | public function getAuthorizerAccessToken() |
|
234 | |||
235 | /** |
||
236 | * Saves the authorizer refresh token in cache. |
||
237 | * |
||
238 | * @param string $refreshToken |
||
239 | * |
||
240 | * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise |
||
241 | */ |
||
242 | 5 | public function setAuthorizerRefreshToken($refreshToken) |
|
246 | |||
247 | /** |
||
248 | * Gets the authorizer refresh token. |
||
249 | * |
||
250 | * @return string |
||
251 | * |
||
252 | * @throws Exception when refresh token is not present |
||
253 | */ |
||
254 | 4 | public function getAuthorizerRefreshToken() |
|
264 | |||
265 | /** |
||
266 | * Removes the authorizer access token from cache. |
||
267 | * |
||
268 | * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. |
||
269 | * Deleting a non-existing entry is considered successful |
||
270 | */ |
||
271 | public function removeAuthorizerAccessToken() |
||
275 | |||
276 | /** |
||
277 | * Removes the authorizer refresh token from cache. |
||
278 | * |
||
279 | * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. |
||
280 | * Deleting a non-existing entry is considered successful |
||
281 | */ |
||
282 | public function removeAuthorizerRefreshToken() |
||
286 | |||
287 | /** |
||
288 | * Gets the authorizer access token cache key. |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | 4 | public function getAuthorizerAccessTokenKey() |
|
296 | |||
297 | /** |
||
298 | * Gets the authorizer refresh token cache key. |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | 5 | public function getAuthorizerRefreshTokenKey() |
|
306 | } |
||
307 |