1 | <?php |
||
30 | final class Authentication implements ClientInterface |
||
31 | { |
||
32 | /** |
||
33 | * The API base uri. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | const BASE_URI = 'https://api.pinterest.com'; |
||
38 | |||
39 | /** |
||
40 | * The API version. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | const API_VERSION = 'v1'; |
||
45 | |||
46 | /** |
||
47 | * The HTTP client. |
||
48 | * |
||
49 | * @var ClientInterface |
||
50 | */ |
||
51 | private $http; |
||
52 | |||
53 | /** |
||
54 | * The client ID. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | private $clientId; |
||
59 | |||
60 | /** |
||
61 | * The client secret. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | private $clientSecret; |
||
66 | |||
67 | /** |
||
68 | * The access token. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | private $accessToken; |
||
73 | |||
74 | /** |
||
75 | * The Constructor. |
||
76 | * |
||
77 | * @param ClientInterface $client The http client. |
||
78 | * @param string $clientId The client id. |
||
79 | * @param string $clientSecret The client secret. |
||
80 | */ |
||
81 | 60 | public function __construct(ClientInterface $client, $clientId, $clientSecret) |
|
87 | |||
88 | /** |
||
89 | * Alternative constructor for when we already have an accessToken. |
||
90 | * |
||
91 | * @param ClientInterface $client The (un-authenticated) HTTP client. |
||
92 | * @param string $clientId The client id. |
||
93 | * @param string $clientSecret The client secret. |
||
94 | * @param string $accessToken The OAuth access token. |
||
95 | * |
||
96 | * @return static |
||
97 | */ |
||
98 | 56 | public static function withAccessToken( |
|
109 | |||
110 | /** |
||
111 | * Alternative constructor for when we only have an accessToken. |
||
112 | * |
||
113 | * ATTENTION: only the execute method will work, as the others need client id and secret. |
||
114 | * |
||
115 | * @param ClientInterface $client The HTTP client. |
||
116 | * @param string $accessToken The OAuth access token. |
||
117 | * |
||
118 | * @return static |
||
119 | */ |
||
120 | 2 | public static function onlyAccessToken( |
|
129 | |||
130 | 34 | private function getBaseUriWithVersion() |
|
134 | |||
135 | /** |
||
136 | * First step of the OAuth process. |
||
137 | * |
||
138 | * @param string $redirectUrl The OAuth redirect url (where code gets sent). |
||
139 | * @param array $scopes An array of scopes (see assertValidScopes). |
||
140 | * @param string $state A unique code you can use to check if the redirect is not spoofed. |
||
141 | * |
||
142 | * @return string The redirect url. |
||
143 | */ |
||
144 | 2 | public function getAuthenticationUrl($redirectUrl, array $scopes, $state) |
|
161 | |||
162 | /** |
||
163 | * Checks if an array of given scopes contains only valid scopes (and at least one). |
||
164 | * |
||
165 | * @param array $scopes The array of scopes to check. |
||
166 | * |
||
167 | * @throws InvalidScopeException When invalid scope in the given array. |
||
168 | * @throws AtLeastOneScopeNeeded When no scopes given. |
||
169 | * @throws TooManyScopesGiven When double scopes in the list. |
||
170 | */ |
||
171 | 2 | private function assertValidScopes(array $scopes) |
|
194 | |||
195 | /** |
||
196 | * Second step of the OAuth process. |
||
197 | * |
||
198 | * @param string $code The OAuth code, caught from the redirect page. |
||
199 | * |
||
200 | * @return string The OAuth access token. |
||
201 | * |
||
202 | * @throws TokenMissing |
||
203 | * @throws RateLimitedReached |
||
204 | */ |
||
205 | public function requestAccessToken($code) |
||
235 | |||
236 | /** |
||
237 | * Executes a http request. |
||
238 | * |
||
239 | * @param Request $request The http request. |
||
240 | * |
||
241 | * @return Http\Response The http response. |
||
242 | */ |
||
243 | 34 | public function execute(Request $request) |
|
257 | |||
258 | /** |
||
259 | * Returns the access token for persisting in some storage. |
||
260 | * |
||
261 | * @return string The OAuth access token. |
||
262 | */ |
||
263 | 4 | public function getAccessToken() |
|
267 | } |
||
268 |