Completed
Push — master ( 43ea89...18283e )
by Дмитрий
01:44
created

WordPress::getRequestTokenUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\Provider\AccessTokenInterface;
11
use SocialConnect\Provider\Exception\InvalidAccessToken;
12
use SocialConnect\Provider\Exception\InvalidResponse;
13
use SocialConnect\OAuth2\AccessToken;
14
use SocialConnect\Common\Entity\User;
15
use SocialConnect\Common\Hydrator\ObjectMap;
16
17
class WordPress extends \SocialConnect\OAuth2\AbstractProvider
18
{
19
    const NAME = 'wordpress';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 3
    public function getBaseUri()
25
    {
26 3
        return 'https://public-api.wordpress.com/rest/v1/';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2
    public function getAuthorizeUri()
33
    {
34 2
        return 'https://public-api.wordpress.com/oauth2/authorize';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 2
    public function getRequestTokenUri()
41
    {
42 2
        return 'https://public-api.wordpress.com/oauth2/token';
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 3
    public function getName()
49
    {
50 3
        return self::NAME;
51
    }
52
53 3
    public function parseToken($body)
54
    {
55 3
        if (empty($body)) {
56
            throw new InvalidAccessToken('Provider response with empty body');
57
        }
58
59 3
        $result = json_decode($body, true);
60 3
        if ($result) {
61 1
            return new AccessToken($result);
62
        }
63
64 2
        throw new InvalidAccessToken('Server response with not valid/empty JSON');
65
    }
66
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 2
    public function getIdentity(AccessTokenInterface $accessToken)
72
    {
73 2
        $response = $this->httpClient->request(
74 2
            $this->getBaseUri() . 'me/',
75 2
            [],
76 2
            Client::GET,
77
            [
78 2
                'Authorization' => "Bearer {$accessToken->getToken()}"
79
            ]
80
        );
81
82 2
        if (!$response->isSuccess()) {
83 1
            throw new InvalidResponse(
84 1
                'API response with error code',
85 1
                $response
86
            );
87
        }
88
89 1
        $result = $response->json();
90 1
        if (!$result) {
91 1
            throw new InvalidResponse(
92 1
                'API response is not a valid JSON object',
93 1
                $response
94
            );
95
        }
96
97
        $hydrator = new ObjectMap(
98
            [
99
                'ID' => 'id',
100
                'avatar_URL' => 'pictureURL',
101
            ]
102
        );
103
104
        return $hydrator->hydrate(new User(), $result);
105
    }
106
}
107