Etsy   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 37
dl 0
loc 118
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getScopes() 0 3 1
A setScopes() 0 9 2
A __construct() 0 11 2
A getAccessTokenEndpoint() 0 3 1
A parseAccessTokenResponse() 0 22 4
A getRequestTokenEndpoint() 0 10 2
A parseRequestTokenResponse() 0 11 5
A getAuthorizationEndpoint() 0 3 1
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);
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)) {
0 ignored issues
show
introduced by
The condition is_array($scopes) is always true.
Loading history...
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