Completed
Push — master ( a178a2...762452 )
by Дмитрий
03:58
created

Px500   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 84
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseUri() 0 4 1
A getAuthorizeUri() 0 4 1
A getRequestTokenUri() 0 4 1
A getRequestTokenAccessUri() 0 4 1
A getName() 0 4 1
B getIdentity() 0 37 5
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth1\Provider;
8
9
use SocialConnect\Provider\AccessTokenInterface;
10
use SocialConnect\Provider\Exception\InvalidResponse;
11
use SocialConnect\OAuth1\AbstractProvider;
12
use SocialConnect\Common\Entity\User;
13
use SocialConnect\Common\Hydrator\ObjectMap;
14
15
class Px500 extends AbstractProvider
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function getBaseUri()
21
    {
22
        return 'https://api.500px.com/v1/';
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getAuthorizeUri()
29
    {
30
        return 'https://api.500px.com/v1/oauth/authorize';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getRequestTokenUri()
37
    {
38
        return 'https://api.500px.com/v1/oauth/request_token';
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getRequestTokenAccessUri()
45
    {
46
        return 'https://api.500px.com/v1/oauth/access_token';
47
    }
48
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getName()
54
    {
55
        return 'px500';
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getIdentity(AccessTokenInterface $accessToken)
62
    {
63
        $response = $this->httpClient->request(
64
            $this->getBaseUri() . 'users'
65
        );
66
67
        if (!$response->isSuccess()) {
68
            throw new InvalidResponse(
69
                'API response with error code',
70
                $response
71
            );
72
        }
73
74
        $result = $response->json();
75
        if (!$result) {
76
            throw new InvalidResponse(
77
                'API response is not a valid JSON object',
78
                $response->getBody()
79
            );
80
        }
81
82
        if (!isset($result->user) || !$result->user) {
83
            throw new InvalidResponse(
84
                'API response without user inside JSON',
85
                $response->getBody()
86
            );
87
        }
88
89
        $hydrator = new ObjectMap(
90
            [
91
                'id' => 'id',
92
                'name' => 'name',
93
            ]
94
        );
95
96
        return $hydrator->hydrate(new User(), $result->user);
97
    }
98
}
99