Completed
Push — master ( 407fac...dcdeb9 )
by Дмитрий
02:25
created

Reddit::parseToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth2\Provider;
8
9
use SocialConnect\Common\Http\Client\Client;
10
use SocialConnect\OAuth2\AccessToken;
11
use SocialConnect\Provider\AccessTokenInterface;
12
use SocialConnect\Provider\Exception\InvalidAccessToken;
13
use SocialConnect\Provider\Exception\InvalidResponse;
14
use SocialConnect\Common\Entity\User;
15
use SocialConnect\Common\Hydrator\ObjectMap;
16
17
class Reddit extends \SocialConnect\OAuth2\AbstractProvider
18
{
19 3
    public function getBaseUri()
20
    {
21 3
        return 'https://oauth.reddit.com/api/v1/';
22
    }
23
24 2
    public function getAuthorizeUri()
25
    {
26 2
        return 'https://ssl.reddit.com/api/v1/authorize';
27
    }
28
29 2
    public function getRequestTokenUri()
30
    {
31 2
        return 'https://ssl.reddit.com/api/v1/access_token';
32
    }
33
34 3
    public function getName()
35
    {
36 3
        return 'reddit';
37
    }
38
39
    /**
40
     * @param string $code
41
     * @return \SocialConnect\Common\Http\Request
42
     */
43 1
    protected function makeAccessTokenRequest($code)
44
    {
45
        $parameters = [
46 1
            'code' => $code,
47 1
            'grant_type' => 'authorization_code',
48 1
            'redirect_uri' => $this->getRedirectUrl()
49 1
        ];
50
51 1
        return new \SocialConnect\Common\Http\Request(
52 1
            $this->getRequestTokenUri(),
53 1
            $parameters,
54 1
            $this->requestHttpMethod,
55
            [
56 1
                'Content-Type' => 'application/x-www-form-urlencoded',
57 1
                'Authorization' => 'Basic ' . base64_encode($this->consumer->getKey() . ':' . $this->consumer->getSecret())
58 1
            ]
59 1
        );
60
    }
61
62
    /**
63
     * @param $body
64
     * @return AccessToken
65
     * @throws InvalidAccessToken
66
     */
67 3
    public function parseToken($body)
68
    {
69 3
        $response = json_decode($body, true);
70 3
        if ($response) {
71 1
            return new AccessToken($response);
72
        }
73
74 2
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 2
    public function getIdentity(AccessTokenInterface $accessToken)
81
    {
82 2
        $response = $this->httpClient->request(
83 2
            $this->getBaseUri() . 'me.json',
84 2
            [],
85 2
            Client::GET,
86
            [
87 2
                'Authorization' => 'Bearer ' . $accessToken->getToken()
88 2
            ]
89 2
        );
90
91 2
        if (!$response->isSuccess()) {
92 1
            throw new InvalidResponse(
93 1
                'API response with error code',
94
                $response
95 1
            );
96
        }
97
98 1
        $result = $response->json();
99 1
        if (!$result) {
100 1
            throw new InvalidResponse(
101 1
                'API response is not a valid JSON object',
102 1
                $response->getBody()
103 1
            );
104
        }
105
106
        $hydrator = new ObjectMap([]);
107
108
        return $hydrator->hydrate(new User(), $result);
109
    }
110
}
111