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

Reddit   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 11

Test Coverage

Coverage 95.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 2
cbo 11
dl 0
loc 94
ccs 44
cts 46
cp 0.9565
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseUri() 0 4 1
A getAuthorizeUri() 0 4 1
A getRequestTokenUri() 0 4 1
A getName() 0 4 1
A makeAccessTokenRequest() 0 18 1
A parseToken() 0 9 2
B getIdentity() 0 30 3
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