Completed
Push — master ( fd4934...d699bb )
by Kento
02:03
created

Mastodon   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 130
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getBaseAuthorizationUrl() 0 3 1
A getAuthorizationUrl() 0 3 1
A getBaseAccessTokenUrl() 0 3 1
A fetchResourceOwnerDetails() 0 3 1
A getInstanceUrl() 0 3 1
A createResourceOwner() 0 3 1
A getResourceOwnerDetailsUrl() 0 3 1
A getDefaultScopes() 0 3 1
A checkResponse() 0 7 3
1
<?php
2
3
namespace Lrf141\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
7
use League\OAuth2\Client\Token\AccessToken;
8
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
9
use Psr\Http\Message\ResponseInterface;
10
11
class Mastodon extends AbstractProvider
12
{
13
14
    use BearerAuthorizationTrait;
15
16
    /**
17
     * Mastodon Instance URL
18
     * ex) https://mstdn.jp
19
     * @var string
20
     */
21
    protected $instance;
22
23
24
    /**
25
     * Mastodon constructor.
26
     * @param array $options
27
     * @param array $collaborators
28
     */
29 18
    public function __construct(array $options = [], array $collaborators = [])
30
    {
31 18
        parent::__construct($options, $collaborators);
32
33 18
        if (isset($options['instance'])) {
34 18
            $this->instance = $options['instance'];
35
        }
36 18
    }
37
38
39
    /**
40
     * Get authorization url to begin OAuth flow
41
     * @return string
42
     */
43 6
    public function getBaseAuthorizationUrl()
44
    {
45 6
        return $this->instance.'/oauth/authorize';
46
    }
47
48
    /**
49
     * Get access token url to retrieve token
50
     * @param array $params
51
     * @return string
52
     */
53 8
    public function getBaseAccessTokenUrl(array $params)
54
    {
55 8
        return $this->instance.'/oauth/token';
56
    }
57
58
59
    /**
60
     * Get provider url to fetch user details
61
     * @param AccessToken $token
62
     * @return string
63
     */
64 2
    public function getResourceOwnerDetailsUrl(AccessToken $token)
65
    {
66 2
        return $this->instance . '/api/v1/accounts/verify_credentials';
67
    }
68
69
    /**
70
     * Get the default scopes used by this provider
71
     * @return array
72
     */
73 2
    protected function getDefaultScopes()
74
    {
75 2
        return ['scope' => 'read'];
76
    }
77
78
    /**
79
     * Check a provider response for errors
80
     * @throws IdentityProviderException
81
     * @param ResponseInterface $response
82
     * @param array|string $data
83
     * @return void
84
     */
85 6
    protected function checkResponse(ResponseInterface $response, $data)
86
    {
87 6
        if (isset($data['error'])) {
88 2
            throw  new IdentityProviderException(
89 2
                $data['error'] ?: $response->getReasonPhrase(),
90 2
                $response->getStatusCode(),
91 2
                $response
0 ignored issues
show
Bug introduced by
$response of type Psr\Http\Message\ResponseInterface is incompatible with the type string|array expected by parameter $response of League\OAuth2\Client\Pro...xception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
                /** @scrutinizer ignore-type */ $response
Loading history...
92
            );
93
        }
94 4
    }
95
96
97
    /**
98
     * Generate a user object from a successful user details request.
99
     *
100
     * @param array $response
101
     * @param AccessToken $token
102
     * @return \League\OAuth2\Client\Provider\ResourceOwnerInterface|MastodonResourceOwner
103
     */
104 2
    protected function createResourceOwner(array $response, AccessToken $token)
105
    {
106 2
        return new MastodonResourceOwner($response);
107
    }
108
109
110
    /**
111
     * Requests resource owner details.
112
     *
113
     * @param AccessToken $token
114
     * @return mixed
115
     */
116 2
    protected function fetchResourceOwnerDetails(AccessToken $token)
117
    {
118 2
        return parent::fetchResourceOwnerDetails($token);
119
    }
120
121
122
    /**
123
     * Builds the authorization URL.
124
     *
125
     * @param array $options
126
     * @return string
127
     */
128 6
    public function getAuthorizationUrl(array $options = [])
129
    {
130 6
        return parent::getAuthorizationUrl($options);
131
    }
132
133
134
    /**
135
     * get mastodon instance url
136
     * @return string
137
     */
138 4
    public function getInstanceUrl() : string
139
    {
140 4
        return $this->instance;
141
    }
142
}
143