Passed
Push — discord-provider ( 0e4f76 )
by AD
03:05
created

Reddit::makeAccessTokenRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 12
ccs 0
cts 11
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
declare(strict_types=1);
7
8
namespace SocialConnect\OAuth2\Provider;
9
10
use Psr\Http\Message\RequestInterface;
11
use SocialConnect\Common\ArrayHydrator;
12
use SocialConnect\Provider\AccessTokenInterface;
13
use SocialConnect\Common\Entity\User;
14
15
class Reddit extends \SocialConnect\OAuth2\AbstractProvider
16
{
17
    const NAME = 'reddit';
18
19
    public function getBaseUri()
20
    {
21
        return 'https://oauth.reddit.com/api/v1/';
22
    }
23
24
    public function getAuthorizeUri()
25
    {
26
        return 'https://ssl.reddit.com/api/v1/authorize';
27
    }
28
29
    public function getRequestTokenUri()
30
    {
31
        return 'https://ssl.reddit.com/api/v1/access_token';
32
    }
33
34
    public function getName()
35
    {
36
        return self::NAME;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    protected function makeAccessTokenRequest(string $code): RequestInterface
43
    {
44
        $parameters = [
45
            'code' => $code,
46
            'grant_type' => 'authorization_code',
47
            'redirect_uri' => $this->getRedirectUrl()
48
        ];
49
50
        return $this->httpStack->createRequest($this->requestHttpMethod, $this->getRequestTokenUri())
51
            ->withHeader('Content-Type', 'application/x-www-form-urlencoded')
52
            ->withHeader('Authorization', 'Basic ' . base64_encode($this->consumer->getKey() . ':' . $this->consumer->getSecret()))
53
            ->withBody($this->httpStack->createStream(http_build_query($parameters)))
54
        ;
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function prepareRequest(string $method, string $uri, array &$headers, array &$query, AccessTokenInterface $accessToken = null): void
61
    {
62
        if ($accessToken) {
63
            $headers['Authorization'] = "Bearer {$accessToken->getToken()}";
64
        }
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getIdentity(AccessTokenInterface $accessToken)
71
    {
72
        $response = $this->request('GET', 'me.json', [], $accessToken);
73
74
        $hydrator = new ArrayHydrator([]);
75
76
        return $hydrator->hydrate(new User(), $response);
77
    }
78
}
79