CredentialsFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createTemporaryCredentialsFromResponse() 0 19 3
A createTokenCredentialsFromResponse() 0 14 2
A getParametersFromResponse() 0 9 1
A getMissingParameterKey() 0 5 3
1
<?php
2
3
namespace Risan\OAuth1\Credentials;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
class CredentialsFactory implements CredentialsFactoryInterface
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12 5
    public function createTemporaryCredentialsFromResponse(ResponseInterface $response)
13
    {
14 5
        $parameters = $this->getParametersFromResponse($response);
15
16 5
        $missingParameterKey = $this->getMissingParameterKey($parameters, [
17 5
            'oauth_token',
18
            'oauth_token_secret',
19
            'oauth_callback_confirmed',
20
        ]);
21
22 5
        if (null !== $missingParameterKey) {
23 3
            throw new CredentialsException("Unable to parse temporary credentials response. Missing parameter: {$missingParameterKey}.");
24
        }
25
26 2
        if ('true' !== $parameters['oauth_callback_confirmed']) {
27 1
            throw new CredentialsException('Unable to parse temporary credentials response. Callback URI is not valid.');
28
        }
29
30 1
        return new TemporaryCredentials($parameters['oauth_token'], $parameters['oauth_token_secret']);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 3
    public function createTokenCredentialsFromResponse(ResponseInterface $response)
37
    {
38 3
        $parameters = $this->getParametersFromResponse($response);
39
40 3
        $missingParameterKey = $this->getMissingParameterKey($parameters, [
41 3
            'oauth_token',
42
            'oauth_token_secret',
43
        ]);
44
45 3
        if (null !== $missingParameterKey) {
46 2
            throw new CredentialsException("Unable to parse token credentials response. Missing parameter: {$missingParameterKey}.");
47
        }
48
49 1
        return new TokenCredentials($parameters['oauth_token'], $parameters['oauth_token_secret']);
50
    }
51
52
    /**
53
     * Get parameters from response.
54
     *
55
     * @param \Psr\Http\Message\ResponseInterface $response
56
     *
57
     * @return array
58
     */
59 9
    public function getParametersFromResponse(ResponseInterface $response)
60
    {
61 9
        $contents = $response->getBody()->getContents();
62
63 9
        $parameters = [];
64
65 9
        parse_str($contents, $parameters);
66
67 9
        return $parameters;
68
    }
69
70
    /**
71
     * Get missing parameter's key.
72
     *
73
     * @param array $parameters
74
     * @param array $requiredKeys
75
     *
76
     * @return string|null
77
     */
78 9
    public function getMissingParameterKey(array $parameters, array $requiredKeys = [])
79
    {
80 9
        foreach ($requiredKeys as $key) {
81 9
            if (! isset($parameters[$key])) {
82 9
                return $key;
83
            }
84
        }
85 4
    }
86
}
87