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

Etsy::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
namespace OAuth\OAuth1\Service;
4
5
use OAuth\OAuth1\Signature\SignatureInterface;
6
use OAuth\OAuth1\Token\StdOAuth1Token;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Consumer\CredentialsInterface;
10
use OAuth\Common\Http\Uri\UriInterface;
11
use OAuth\Common\Storage\TokenStorageInterface;
12
use OAuth\Common\Http\Client\ClientInterface;
13
14
class Etsy extends AbstractService
15
{
16
17
    protected $scopes = array();
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function init()
23
    {
24
        if (null === $this->baseApiUri) {
25
            $this->baseApiUri = new Uri('https://openapi.etsy.com/v2/');
26
        }
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getRequestTokenEndpoint()
33
    {
34
        $uri = new Uri($this->baseApiUri . 'oauth/request_token');
35
        $scopes = $this->getScopes();
36
37
        if (count($scopes)) {
38
            $uri->setQuery('scope=' . implode('%20', $scopes));
39
        }
40
41
        return $uri;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getAuthorizationEndpoint()
48
    {
49
        return new Uri($this->baseApiUri);
0 ignored issues
show
Bug introduced by
It seems like $this->baseApiUri can also be of type object<OAuth\Common\Http\Uri\UriInterface>; however, OAuth\Common\Http\Uri\Uri::__construct() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getAccessTokenEndpoint()
56
    {
57
        return new Uri($this->baseApiUri . 'oauth/access_token');
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function parseRequestTokenResponse($responseBody)
64
    {
65
        parse_str($responseBody, $data);
66
67
        if (null === $data || !is_array($data)) {
68
            throw new TokenResponseException('Unable to parse response.');
69
        } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
70
            throw new TokenResponseException('Error in retrieving token.');
71
        }
72
73
        return $this->parseAccessTokenResponse($responseBody);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected function parseAccessTokenResponse($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['error'])) {
86
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
87
        }
88
89
        $token = new StdOAuth1Token();
90
91
        $token->setRequestToken($data['oauth_token']);
92
        $token->setRequestTokenSecret($data['oauth_token_secret']);
93
        $token->setAccessToken($data['oauth_token']);
94
        $token->setAccessTokenSecret($data['oauth_token_secret']);
95
96
        $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
97
        unset($data['oauth_token'], $data['oauth_token_secret']);
98
        $token->setExtraParams($data);
99
100
        return $token;
101
    }
102
103
    /**
104
     * Set the scopes for permissions
105
     * @see https://www.etsy.com/developers/documentation/getting_started/oauth#section_permission_scopes
106
     * @param array $scopes
107
     *
108
     * @return $this
109
     */
110
    public function setScopes(array $scopes)
111
    {
112
        if (!is_array($scopes)) {
113
            $scopes = array();
114
        }
115
116
        $this->scopes = $scopes;
117
        return $this;
118
    }
119
120
    /**
121
     * Return the defined scopes
122
     * @return array
123
     */
124
    public function getScopes()
125
    {
126
        return $this->scopes;
127
    }
128
}
129