1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Risan\OAuth1\Request; |
4
|
|
|
|
5
|
|
|
use Risan\OAuth1\Credentials\TokenCredentials; |
6
|
|
|
use Risan\OAuth1\Credentials\TemporaryCredentials; |
7
|
|
|
|
8
|
|
|
class RequestFactory implements RequestFactoryInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The AuthorizationHeaderInterface instance. |
12
|
|
|
* |
13
|
|
|
* @var \Risan\OAuth1\Request\AuthorizationHeaderInterface |
14
|
|
|
*/ |
15
|
|
|
protected $authorizationHeader; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The UriParserInterface instance. |
19
|
|
|
* |
20
|
|
|
* @var \Risan\OAuth1\Request\UriParserInterface |
21
|
|
|
*/ |
22
|
|
|
protected $uriParser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create the new instance of RequestFactory class. |
26
|
|
|
* |
27
|
|
|
* @param \Risan\OAuth1\Request\AuthorizationHeaderInterface $authorizationHeader |
28
|
|
|
* @param \Risan\OAuth1\Request\UriParserInterface $uriParser |
29
|
|
|
*/ |
30
|
12 |
|
public function __construct(AuthorizationHeaderInterface $authorizationHeader, UriParserInterface $uriParser) |
31
|
|
|
{ |
32
|
12 |
|
$this->authorizationHeader = $authorizationHeader; |
33
|
12 |
|
$this->uriParser = $uriParser; |
34
|
12 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
3 |
|
public function getAuthorizationHeader() |
40
|
|
|
{ |
41
|
3 |
|
return $this->authorizationHeader; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
1 |
|
public function getConfig() |
48
|
|
|
{ |
49
|
1 |
|
return $this->authorizationHeader->getConfig(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function getUriParser() |
56
|
|
|
{ |
57
|
1 |
|
return $this->uriParser; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
1 |
|
public function createForTemporaryCredentials() |
64
|
|
|
{ |
65
|
1 |
|
return $this->create('POST', (string) $this->getConfig()->getTemporaryCredentialsUri(), [ |
66
|
|
|
'headers' => [ |
67
|
1 |
|
'Authorization' => $this->authorizationHeader->forTemporaryCredentials(), |
68
|
|
|
], |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
1 |
|
public function buildAuthorizationUri(TemporaryCredentials $temporaryCredentials) |
76
|
|
|
{ |
77
|
1 |
|
return $this->uriParser->appendQueryParameters( |
78
|
1 |
|
$this->getConfig()->getAuthorizationUri(), |
79
|
1 |
|
['oauth_token' => $temporaryCredentials->getIdentifier()] |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
1 |
|
public function createForTokenCredentials(TemporaryCredentials $temporaryCredentials, $verificationCode) |
87
|
|
|
{ |
88
|
1 |
|
return $this->create('POST', (string) $this->getConfig()->getTokenCredentialsUri(), [ |
89
|
|
|
'headers' => [ |
90
|
1 |
|
'Authorization' => $this->authorizationHeader->forTokenCredentials($temporaryCredentials, $verificationCode), |
91
|
|
|
], |
92
|
|
|
'form_params' => [ |
93
|
1 |
|
'oauth_verifier' => $verificationCode, |
94
|
|
|
], |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
1 |
|
public function createForProtectedResource(TokenCredentials $tokenCredentials, $method, $uri, array $options = []) |
102
|
|
|
{ |
103
|
1 |
|
return $this->create($method, (string) $this->getConfig()->buildUri($uri), array_replace_recursive([ |
104
|
|
|
'headers' => [ |
105
|
1 |
|
'Authorization' => $this->authorizationHeader->forProtectedResource($tokenCredentials, $method, $uri, $options), |
106
|
|
|
], |
107
|
1 |
|
], $options)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Create a new instance of Request class. |
112
|
|
|
* |
113
|
|
|
* @param string $method |
114
|
|
|
* @param string $uri |
115
|
|
|
* @param array $options [description] |
116
|
|
|
* |
117
|
|
|
* @return \Risan\OAuth1\Request\RequestInterface |
118
|
|
|
*/ |
119
|
|
|
public function create($method, $uri, array $options = []) |
120
|
|
|
{ |
121
|
|
|
return new Request($method, $uri, $options); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|