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
|
13 |
|
public function __construct(HttpClientInterface $httpClient, RequestFactoryInterface $requestFactory, CredentialsFactoryInterface $credentialsFactory) |
50
|
|
|
{ |
51
|
13 |
|
$this->httpClient = $httpClient; |
52
|
13 |
|
$this->requestFactory = $requestFactory; |
53
|
13 |
|
$this->credentialsFactory = $credentialsFactory; |
54
|
13 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
1 |
|
public function getHttpClient() |
60
|
|
|
{ |
61
|
1 |
|
return $this->httpClient; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
1 |
|
public function getRequestFactory() |
68
|
|
|
{ |
69
|
1 |
|
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
|
2 |
|
public function request($method, $uri, array $options = []) |
144
|
|
|
{ |
145
|
2 |
|
if (null === $this->getTokenCredentials()) { |
146
|
1 |
|
throw new CredentialsException('No token credential has been set.'); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
return $this->httpClient->send( |
150
|
1 |
|
$this->requestFactory->createForProtectedResource($this->getTokenCredentials(), $method, $uri, $options) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|