Completed
Pull Request — master (#485)
by
unknown
04:42
created

Pinterest::parseAccessTokenResponse()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 31
rs 8.439
c 1
b 0
f 1
cc 6
eloc 18
nc 6
nop 1
1
<?php
2
/**
3
 * Pinterest service.
4
 *
5
 * @author  Pedro Amorim <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
7
 * @link    https://developers.pinterest.com/docs/api/overview/
8
 */
9
10
namespace OAuth\OAuth2\Service;
11
12
use OAuth\OAuth2\Token\StdOAuth2Token;
13
use OAuth\Common\Http\Exception\TokenResponseException;
14
use OAuth\Common\Http\Uri\Uri;
15
use OAuth\Common\Consumer\CredentialsInterface;
16
use OAuth\Common\Http\Client\ClientInterface;
17
use OAuth\Common\Storage\TokenStorageInterface;
18
use OAuth\Common\Http\Uri\UriInterface;
19
20
/**
21
 * Pinterest service.
22
 *
23
 * @author  Pedro Amorim <[email protected]>
24
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
25
 * @link    https://developers.pinterest.com/docs/api/overview/
26
 */
27
class Pinterest extends AbstractService
28
{
29
    /**
30
     * Defined scopes - More scopes are listed here:
31
     * https://developers.pinterest.com/docs/api/overview/
32
     */
33
    const SCOPE_READ_PUBLIC         = 'read_public';            // read a user’s Pins, boards and likes
34
    const SCOPE_WRITE_PUBLIC        = 'write_public';           // write Pins, boards, likes
35
    const SCOPE_READ_RELATIONSHIPS  = 'read_relationships';     // read a user’s follows (boards, users, interests)
36
    const SCOPE_WRITE_RELATIONSHIPS = 'write_relationships';    // follow boards, users and interests
37
38
    public function __construct(
39
        CredentialsInterface $credentials,
40
        ClientInterface $httpClient,
41
        TokenStorageInterface $storage,
42
        $scopes = array(),
43
        UriInterface $baseApiUri = null
44
    ) {
45
        parent::__construct(
46
            $credentials,
47
            $httpClient,
48
            $storage,
49
            $scopes,
50
            $baseApiUri,
51
            true
52
        );
53
54
        if (null === $baseApiUri) {
55
            $this->baseApiUri = new Uri('https://api.pinterest.com/');
56
        }
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getAuthorizationEndpoint()
63
    {
64
        return new Uri('https://api.pinterest.com/oauth/');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getAccessTokenEndpoint()
71
    {
72
        return new Uri('https://api.pinterest.com/v1/oauth/token');
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function getAuthorizationMethod()
79
    {
80
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function parseAccessTokenResponse($responseBody)
87
    {
88
        $data = json_decode($responseBody, true);
89
90
        if (null === $data || !is_array($data)) {
91
            throw new TokenResponseException('Unable to parse response.');
92
        } elseif (isset($data['error'])) {
93
            throw new TokenResponseException(
94
                'Error in retrieving token: "' . $data['error'] . '"'
95
            );
96
        }
97
98
        $token = new StdOAuth2Token();
99
        $token->setAccessToken($data['access_token']);
100
101
        if (isset($data['expires_in'])) {
102
            $token->setLifeTime($data['expires_in']);
103
            unset($data['expires_in']);
104
        }
105
        // I hope one day Pinterest add a refresh token :)
106
        if (isset($data['refresh_token'])) {
107
            $token->setRefreshToken($data['refresh_token']);
108
            unset($data['refresh_token']);
109
        }
110
111
        unset($data['access_token']);
112
113
        $token->setExtraParams($data);
114
115
        return $token;
116
    }
117
}
118