1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Risan\OAuth1; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Risan\OAuth1\Credentials\TokenCredentials; |
7
|
|
|
use Risan\OAuth1\Request\RequestFactoryInterface; |
8
|
|
|
use Risan\OAuth1\Credentials\CredentialsException; |
9
|
|
|
use Risan\OAuth1\Credentials\TemporaryCredentials; |
10
|
|
|
use Risan\OAuth1\Credentials\CredentialsFactoryInterface; |
11
|
|
|
|
12
|
|
|
class OAuth1 implements OAuth1Interface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The HttpClientInterface instance. |
16
|
|
|
* |
17
|
|
|
* @var \Risan\OAuth1\HttpClientInterface |
18
|
|
|
*/ |
19
|
|
|
protected $httpClient; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The RequestFactoryInterface instance. |
23
|
|
|
* |
24
|
|
|
* @var \Risan\OAuth1\Request\RequestFactoryInterface |
25
|
|
|
*/ |
26
|
|
|
protected $requestFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The CredentialsFactoryInterface instance. |
30
|
|
|
* |
31
|
|
|
* @var \Risan\OAuth1\Credentials\CredentialsFactoryInterface |
32
|
|
|
*/ |
33
|
|
|
protected $credentialsFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The TokenCredentials instance. |
37
|
|
|
* |
38
|
|
|
* @var \Risan\OAuth1\Credentials\TokenCredentials |
39
|
|
|
*/ |
40
|
|
|
protected $tokenCredentials; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new OAuth1 instance. |
44
|
|
|
* |
45
|
|
|
* @param \Risan\OAuth1\HttpClientInterface $httpClient |
46
|
|
|
* @param \Risan\OAuth1\Request\RequestFactoryInterface $requestFactory |
47
|
|
|
* @param \Risan\OAuth1\Credentials\CredentialsFactoryInterface $credentialsFactory |
48
|
|
|
*/ |
49
|
21 |
|
public function __construct(HttpClientInterface $httpClient, RequestFactoryInterface $requestFactory, CredentialsFactoryInterface $credentialsFactory) |
50
|
|
|
{ |
51
|
21 |
|
$this->httpClient = $httpClient; |
52
|
21 |
|
$this->requestFactory = $requestFactory; |
53
|
21 |
|
$this->credentialsFactory = $credentialsFactory; |
54
|
21 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
1 |
|
public function getHttpClient() |
60
|
|
|
{ |
61
|
1 |
|
return $this->httpClient; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
3 |
|
public function getRequestFactory() |
68
|
|
|
{ |
69
|
3 |
|
return $this->requestFactory; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
1 |
|
public function getCredentialsFactory() |
76
|
|
|
{ |
77
|
1 |
|
return $this->credentialsFactory; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
1 |
|
public function getConfig() |
84
|
|
|
{ |
85
|
1 |
|
return $this->requestFactory->getConfig(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
3 |
|
public function getTokenCredentials() |
92
|
|
|
{ |
93
|
3 |
|
return $this->tokenCredentials; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
2 |
|
public function setTokenCredentials(TokenCredentials $tokenCredentials) |
100
|
|
|
{ |
101
|
2 |
|
$this->tokenCredentials = $tokenCredentials; |
102
|
|
|
|
103
|
2 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
1 |
|
public function requestTemporaryCredentials() |
110
|
|
|
{ |
111
|
1 |
|
$response = $this->httpClient->send($this->requestFactory->createForTemporaryCredentials()); |
112
|
|
|
|
113
|
1 |
|
return $this->credentialsFactory->createTemporaryCredentialsFromResponse($response); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
1 |
|
public function buildAuthorizationUri(TemporaryCredentials $temporaryCredentials) |
120
|
|
|
{ |
121
|
1 |
|
return (string) $this->requestFactory->buildAuthorizationUri($temporaryCredentials); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
2 |
|
public function requestTokenCredentials(TemporaryCredentials $temporaryCredentials, $temporaryIdentifier, $verificationCode) |
128
|
|
|
{ |
129
|
2 |
|
if ($temporaryCredentials->getIdentifier() !== $temporaryIdentifier) { |
130
|
1 |
|
throw new InvalidArgumentException('The given temporary credentials identifier does not match the temporary credentials.'); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
$response = $this->httpClient->send( |
134
|
1 |
|
$this->requestFactory->createForTokenCredentials($temporaryCredentials, $verificationCode) |
135
|
|
|
); |
136
|
|
|
|
137
|
1 |
|
return $this->credentialsFactory->createTokenCredentialsFromResponse($response); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
1 |
|
public function get($uri, array $options = []) |
144
|
|
|
{ |
145
|
1 |
|
return $this->request('GET', $uri, $options); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
1 |
|
public function post($uri, array $options = []) |
152
|
|
|
{ |
153
|
1 |
|
return $this->request('POST', $uri, $options); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
1 |
|
public function put($uri, array $options = []) |
160
|
|
|
{ |
161
|
1 |
|
return $this->request('PUT', $uri, $options); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
1 |
|
public function patch($uri, array $options = []) |
168
|
|
|
{ |
169
|
1 |
|
return $this->request('PATCH', $uri, $options); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
1 |
|
public function delete($uri, array $options = []) |
176
|
|
|
{ |
177
|
1 |
|
return $this->request('DELETE', $uri, $options); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
2 |
|
public function request($method, $uri, array $options = []) |
184
|
|
|
{ |
185
|
2 |
|
if (null === $this->getTokenCredentials()) { |
186
|
1 |
|
throw new CredentialsException('No token credential has been set.'); |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
return $this->httpClient->send( |
190
|
1 |
|
$this->requestFactory->createForProtectedResource($this->getTokenCredentials(), $method, $uri, $options) |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|