Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

FiveHundredPx::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
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
 * @link    https://developers.500px.com/
8
 */
9
10
namespace OAuth\OAuth1\Service;
11
12
use OAuth\OAuth1\Token\StdOAuth1Token;
13
use OAuth\Common\Http\Exception\TokenResponseException;
14
use OAuth\Common\Http\Uri\Uri;
15
16
/**
17
 * 500px service.
18
 *
19
 * @author  Pedro Amorim <[email protected]>
20
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
21
 * @link    https://developers.500px.com/
22
 */
23
class FiveHundredPx extends AbstractService
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function init()
29
    {
30
        if (null === $this->baseApiUri) {
31
            $this->baseApiUri = new Uri('https://api.500px.com/v1/');
32
        }
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function getRequestTokenEndpoint()
39
    {
40
        return new Uri('https://api.500px.com/v1/oauth/request_token');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getAuthorizationEndpoint()
47
    {
48
        return new Uri('https://api.500px.com/v1/oauth/authorize');
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getAccessTokenEndpoint()
55
    {
56
        return new Uri('https://api.500px.com/v1/oauth/access_token');
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function parseRequestTokenResponse($responseBody)
63
    {
64
        parse_str($responseBody, $data);
65
66
        if (null === $data || !is_array($data)) {
67
            throw new TokenResponseException('Unable to parse response.');
68
        } elseif (!isset($data['oauth_callback_confirmed'])
69
            || $data['oauth_callback_confirmed'] !== 'true'
70
        ) {
71
            throw new TokenResponseException('Error in retrieving token.');
72
        }
73
74
        return $this->parseAccessTokenResponse($responseBody);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    protected function parseAccessTokenResponse($responseBody)
81
    {
82
        parse_str($responseBody, $data);
83
84
        if (null === $data || !is_array($data)) {
85
            throw new TokenResponseException('Unable to parse response.');
86
        } elseif (isset($data['error'])) {
87
            throw new TokenResponseException(
88
                'Error in retrieving token: "' . $data['error'] . '"'
89
            );
90
        }
91
92
        $token = new StdOAuth1Token();
93
94
        $token->setRequestToken($data['oauth_token']);
95
        $token->setRequestTokenSecret($data['oauth_token_secret']);
96
        $token->setAccessToken($data['oauth_token']);
97
        $token->setAccessTokenSecret($data['oauth_token_secret']);
98
99
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
100
        unset($data['oauth_token'], $data['oauth_token_secret']);
101
        $token->setExtraParams($data);
102
103
        return $token;
104
    }
105
}
106