Completed
Pull Request — master (#4)
by Risan Bagja
02:18 queued 51s
created

CredentialsFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B createTemporaryCredentialsFromResponse() 0 24 4
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
    public function createTemporaryCredentialsFromResponse(ResponseInterface $response)
14
    {
15
        $contents = $response->getBody()->getContents();
16
17
        parse_str($contents, $responseParameters);
18
19
        $requiredParames = [
20
            'oauth_token',
21
            'oauth_token_secret',
22
            'oauth_callback_confirmed',
23
        ];
24
25
        foreach ($requiredParames as $param) {
26
            if (! isset($responseParameters[$param])) {
27
                throw new CredentialsException("Unable to parse temporary credentials response. Missing parameter: {$param}.");
28
            }
29
        }
30
31
        if ($responseParameters['oauth_callback_confirmed'] !== 'true') {
32
            throw new CredentialsException('Unable to parse temporary credentials response. Callback URI is not valid.');
33
        }
34
35
        return new TemporaryCredentials($responseParameters['oauth_token'], $responseParameters['oauth_token_secret']);
36
    }
37
}
38