Etsy::getRequestTokenEndpoint()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace OAuth\OAuth1\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth1\Signature\SignatureInterface;
12
use OAuth\OAuth1\Token\StdOAuth1Token;
13
14
class Etsy extends AbstractService
15
{
16
    protected $scopes = [];
17
18
    public function __construct(
19
        CredentialsInterface $credentials,
20
        ClientInterface $httpClient,
21
        TokenStorageInterface $storage,
22
        SignatureInterface $signature,
23
        ?UriInterface $baseApiUri = null
24
    ) {
25
        parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
26
27
        if (null === $baseApiUri) {
28
            $this->baseApiUri = new Uri('https://openapi.etsy.com/v2/');
29
        }
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getRequestTokenEndpoint()
36
    {
37
        $uri = new Uri($this->baseApiUri . 'oauth/request_token');
38
        $scopes = $this->getScopes();
39
40
        if (count($scopes)) {
41
            $uri->setQuery('scope=' . implode('%20', $scopes));
42
        }
43
44
        return $uri;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getAuthorizationEndpoint()
51
    {
52
        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...
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getAccessTokenEndpoint()
59
    {
60
        return new Uri($this->baseApiUri . 'oauth/access_token');
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function parseRequestTokenResponse($responseBody)
67
    {
68
        parse_str($responseBody, $data);
69
70
        if (null === $data || !is_array($data)) {
71
            throw new TokenResponseException('Unable to parse response.');
72
        } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') {
73
            throw new TokenResponseException('Error in retrieving token.');
74
        }
75
76
        return $this->parseAccessTokenResponse($responseBody);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    protected function parseAccessTokenResponse($responseBody)
83
    {
84
        parse_str($responseBody, $data);
85
86
        if (null === $data || !is_array($data)) {
87
            throw new TokenResponseException('Unable to parse response.');
88
        } elseif (isset($data['error'])) {
89
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
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
    /**
107
     * Set the scopes for permissions.
108
     *
109
     * @see https://www.etsy.com/developers/documentation/getting_started/oauth#section_permission_scopes
110
     *
111
     * @return $this
112
     */
113
    public function setScopes(array $scopes)
114
    {
115
        if (!is_array($scopes)) {
116
            $scopes = [];
117
        }
118
119
        $this->scopes = $scopes;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Return the defined scopes.
126
     *
127
     * @return array
128
     */
129
    public function getScopes()
130
    {
131
        return $this->scopes;
132
    }
133
}
134