Completed
Pull Request — master (#506)
by Dragonqos
02:52
created

Pinterest::getAuthorizationMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
16
/**
17
 * Pinterest service.
18
 *
19
 * @author  Pedro Amorim <[email protected]>
20
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
21
 * @link    https://developers.pinterest.com/docs/api/overview/
22
 */
23
class Pinterest extends AbstractService
24
{
25
    /**
26
     * Defined scopes - More scopes are listed here:
27
     * https://developers.pinterest.com/docs/api/overview/
28
     */
29
    const SCOPE_READ_PUBLIC         = 'read_public';            // read a user’s Pins, boards and likes
30
    const SCOPE_WRITE_PUBLIC        = 'write_public';           // write Pins, boards, likes
31
    const SCOPE_READ_RELATIONSHIPS  = 'read_relationships';     // read a user’s follows (boards, users, interests)
32
    const SCOPE_WRITE_RELATIONSHIPS = 'write_relationships';    // follow boards, users and interests
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function init()
38
    {
39
        $this->stateParameterInAuthUrl = true;
40
41
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
42
            $this->baseApiUri = new Uri('https://api.pinterest.com/');
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getAuthorizationEndpoint()
50
    {
51
        return new Uri('https://api.pinterest.com/oauth/');
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getAccessTokenEndpoint()
58
    {
59
        return new Uri('https://api.pinterest.com/v1/oauth/token');
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function getAuthorizationMethod()
66
    {
67
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function parseAccessTokenResponse($responseBody)
74
    {
75
        $data = json_decode($responseBody, true);
76
77
        if (null === $data || !is_array($data)) {
78
            throw new TokenResponseException('Unable to parse response.');
79
        } elseif (isset($data['error'])) {
80
            throw new TokenResponseException(
81
                'Error in retrieving token: "' . $data['error'] . '"'
82
            );
83
        }
84
85
        $token = new StdOAuth2Token();
86
        $token->setAccessToken($data['access_token']);
87
88
        if (isset($data['expires_in'])) {
89
            $token->setLifeTime($data['expires_in']);
90
            unset($data['expires_in']);
91
        }
92
        // I hope one day Pinterest add a refresh token :)
93
        if (isset($data['refresh_token'])) {
94
            $token->setRefreshToken($data['refresh_token']);
95
            unset($data['refresh_token']);
96
        }
97
98
        unset($data['access_token']);
99
100
        $token->setExtraParams($data);
101
102
        return $token;
103
    }
104
}
105