FiveHundredPx   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 2
A getRequestTokenEndpoint() 0 4 1
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A parseRequestTokenResponse() 0 14 5
A parseAccessTokenResponse() 0 25 4
1
<?php
2
/**
3
 * 500px service.
4
 *
5
 * @author  Pedro Amorim <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
7
 *
8
 * @see    https://developers.500px.com/
9
 */
10
11
namespace OAuth\OAuth1\Service;
12
13
use OAuth\Common\Consumer\CredentialsInterface;
14
use OAuth\Common\Http\Client\ClientInterface;
15
use OAuth\Common\Http\Exception\TokenResponseException;
16
use OAuth\Common\Http\Uri\Uri;
17
use OAuth\Common\Http\Uri\UriInterface;
18
use OAuth\Common\Storage\TokenStorageInterface;
19
use OAuth\OAuth1\Signature\SignatureInterface;
20
use OAuth\OAuth1\Token\StdOAuth1Token;
21
22
/**
23
 * 500px service.
24
 *
25
 * @author  Pedro Amorim <[email protected]>
26
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
27
 *
28
 * @see    https://developers.500px.com/
29
 */
30
class FiveHundredPx extends AbstractService
31
{
32
    public function __construct(
33
        CredentialsInterface $credentials,
34
        ClientInterface $httpClient,
35
        TokenStorageInterface $storage,
36
        SignatureInterface $signature,
37
        ?UriInterface $baseApiUri = null
38
    ) {
39
        parent::__construct(
40
            $credentials,
41
            $httpClient,
42
            $storage,
43
            $signature,
44
            $baseApiUri
45
        );
46
47
        if (null === $baseApiUri) {
48
            $this->baseApiUri = new Uri('https://api.500px.com/v1/');
49
        }
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getRequestTokenEndpoint()
56
    {
57
        return new Uri('https://api.500px.com/v1/oauth/request_token');
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getAuthorizationEndpoint()
64
    {
65
        return new Uri('https://api.500px.com/v1/oauth/authorize');
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getAccessTokenEndpoint()
72
    {
73
        return new Uri('https://api.500px.com/v1/oauth/access_token');
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected function parseRequestTokenResponse($responseBody)
80
    {
81
        parse_str($responseBody, $data);
82
83
        if (null === $data || !is_array($data)) {
84
            throw new TokenResponseException('Unable to parse response.');
85
        } elseif (!isset($data['oauth_callback_confirmed'])
86
            || $data['oauth_callback_confirmed'] !== 'true'
87
        ) {
88
            throw new TokenResponseException('Error in retrieving token.');
89
        }
90
91
        return $this->parseAccessTokenResponse($responseBody);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function parseAccessTokenResponse($responseBody)
98
    {
99
        parse_str($responseBody, $data);
100
101
        if (null === $data || !is_array($data)) {
102
            throw new TokenResponseException('Unable to parse response.');
103
        } elseif (isset($data['error'])) {
104
            throw new TokenResponseException(
105
                'Error in retrieving token: "' . $data['error'] . '"'
106
            );
107
        }
108
109
        $token = new StdOAuth1Token();
110
111
        $token->setRequestToken($data['oauth_token']);
112
        $token->setRequestTokenSecret($data['oauth_token_secret']);
113
        $token->setAccessToken($data['oauth_token']);
114
        $token->setAccessTokenSecret($data['oauth_token_secret']);
115
116
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
117
        unset($data['oauth_token'], $data['oauth_token_secret']);
118
        $token->setExtraParams($data);
119
120
        return $token;
121
    }
122
}
123