1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Risan\OAuth1\Credentials; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Risan\OAuth1\Credentials\CredentialsFactoryInterface; |
7
|
|
|
|
8
|
|
|
class CredentialsFactory implements CredentialsFactoryInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* {@inheritDoc} |
12
|
|
|
*/ |
13
|
5 |
|
public function createTemporaryCredentialsFromResponse(ResponseInterface $response) |
14
|
|
|
{ |
15
|
5 |
|
$parameters = $this->getParametersFromResponse($response); |
16
|
|
|
|
17
|
5 |
|
$missingParameterKey = $this->getMissingParameterKey($parameters, [ |
|
|
|
|
18
|
5 |
|
'oauth_token', |
19
|
|
|
'oauth_token_secret', |
20
|
|
|
'oauth_callback_confirmed', |
21
|
|
|
]); |
22
|
|
|
|
23
|
5 |
|
if (null !== $missingParameterKey) { |
24
|
3 |
|
throw new CredentialsException("Unable to parse temporary credentials response. Missing parameter: {$missingParameterKey}."); |
25
|
|
|
} |
26
|
|
|
|
27
|
2 |
|
if ($parameters['oauth_callback_confirmed'] !== 'true') { |
28
|
1 |
|
throw new CredentialsException('Unable to parse temporary credentials response. Callback URI is not valid.'); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
return new TemporaryCredentials($parameters['oauth_token'], $parameters['oauth_token_secret']); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
3 |
|
public function createTokenCredentialsFromResponse(ResponseInterface $response) |
38
|
|
|
{ |
39
|
3 |
|
$parameters = $this->getParametersFromResponse($response); |
40
|
|
|
|
41
|
3 |
|
$missingParameterKey = $this->getMissingParameterKey($parameters, [ |
|
|
|
|
42
|
3 |
|
'oauth_token', |
43
|
|
|
'oauth_token_secret', |
44
|
|
|
]); |
45
|
|
|
|
46
|
3 |
|
if (null !== $missingParameterKey) { |
47
|
2 |
|
throw new CredentialsException("Unable to parse token credentials response. Missing parameter: {$missingParameterKey}."); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return new TokenCredentials($parameters['oauth_token'], $parameters['oauth_token_secret']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get parameters from response. |
55
|
|
|
* |
56
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
9 |
|
public function getParametersFromResponse(ResponseInterface $response) |
60
|
|
|
{ |
61
|
9 |
|
$contents = $response->getBody()->getContents(); |
62
|
|
|
|
63
|
9 |
|
parse_str($contents, $parameters); |
64
|
|
|
|
65
|
9 |
|
return $parameters; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get missing parameter's key. |
70
|
|
|
* |
71
|
|
|
* @param array $parameters |
72
|
|
|
* @param array $requiredKeys |
73
|
|
|
* @return string|null |
74
|
|
|
*/ |
75
|
9 |
|
public function getMissingParameterKey(array $parameters, array $requiredKeys = []) |
76
|
|
|
{ |
77
|
9 |
|
foreach ($requiredKeys as $key) { |
78
|
9 |
|
if (! isset($parameters[$key])) { |
79
|
9 |
|
return $key; |
80
|
|
|
} |
81
|
|
|
} |
82
|
4 |
|
} |
83
|
|
|
} |
84
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.