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