TwitterOAuth2   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 30
ccs 0
cts 8
cp 0
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initUserAttributes() 0 3 1
A getName() 0 3 1
A applyAccessTokenToRequest() 0 3 1
A getTitle() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\AuthClient\Client;
6
7
use Psr\Http\Message\RequestInterface;
8
use Yiisoft\Yii\AuthClient\OAuth2;
9
use Yiisoft\Yii\AuthClient\OAuthToken;
10
11
/**
12
 * TwitterOAuth2 allows authentication via Twitter OAuth 2.
13
 *
14
 * Note, that at the time these docs are written, Twitter does not provide full support for OAuth 2 protocol.
15
 * It is supported only for [application-only authentication](https://dev.twitter.com/oauth/application-only) workflow.
16
 * Thus only {@see authenticateClient()} method of this class has a practical usage.
17
 *
18
 * Any authentication attempt on behalf of the end-user will fail for this client. You should use {@see Twitter} class for
19
 * this workflow.
20
 *
21
 * @see Twitter
22
 * @link https://dev.twitter.com/
23
 */
24
final class TwitterOAuth2 extends OAuth2
25
{
26
    protected string $authUrl = 'https://api.twitter.com/oauth2/authenticate';
27
    protected string $tokenUrl = 'https://api.twitter.com/oauth2/token';
28
    protected string $endpoint = 'https://api.twitter.com/1.1';
29
30
    public function applyAccessTokenToRequest(RequestInterface $request, OAuthToken $accessToken): RequestInterface
31
    {
32
        return $request->withHeader('Authorization', 'Bearer ' . $accessToken->getToken());
33
    }
34
35
    /**
36
     * @return string service name.
37
     */
38
    public function getName(): string
39
    {
40
        return 'twitter';
41
    }
42
43
    /**
44
     * @return string service title.
45
     */
46
    public function getTitle(): string
47
    {
48
        return 'Twitter';
49
    }
50
51
    protected function initUserAttributes(): array
52
    {
53
        return $this->api('account/verify_credentials.json', 'GET');
54
    }
55
}
56