Completed
Push — master ( 8b6d81...76c85e )
by Дмитрий
02:22
created

Steein::getIdentity()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 47
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8.6557

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 47
ccs 18
cts 31
cp 0.5806
rs 8.5125
c 1
b 0
f 0
cc 6
eloc 23
nc 7
nop 1
crap 8.6557
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 Steein extends \SocialConnect\OAuth2\AbstractProvider
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 3
    public function getBaseUri()
23
    {
24 3
        return 'https://www.steein.ru/';
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 2
    public function getAuthorizeUri()
31
    {
32 2
        return 'https://www.steein.ru/oauth/authorize';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2
    public function getRequestTokenUri()
39
    {
40 2
        return 'https://www.steein.ru/oauth/token';
41
    }
42
43
    /**
44
     * @return string
45
     */
46 3
    public function getName()
47
    {
48 3
        return 'steein';
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getScopeInline()
55
    {
56
        return implode(' ', $this->scope);
57
    }
58
59
    /**
60
     * @param $body
61
     * @return AccessToken
62
     * @throws InvalidAccessToken
63
     */
64 3
    public function parseToken($body)
65
    {
66 3
        $response = json_decode($body, true);
67 3
        if ($response) {
68 1
            return new AccessToken($response);
69
        }
70
71 2
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    public function getIdentity(AccessTokenInterface $accessToken)
78
    {
79 2
        $response = $this->httpClient->request(
80 2
            $this->getBaseUri() . 'api/v2.0/users/show',
81 2
            [],
82 2
            Client::GET,
83
            [
84 2
                'Authorization' => 'Bearer ' . $accessToken->getToken()
85 2
            ]
86 2
        );
87
88 2
        if (!$response->isSuccess()) {
89 1
            throw new InvalidResponse(
90 1
                'API response with error code',
91
                $response
92 1
            );
93
        }
94
95 1
        $result = $response->json();
96 1
        if (!$result) {
97 1
            throw new InvalidResponse(
98 1
                'API response is not a valid JSON object',
99 1
                $response->getBody()
100 1
            );
101
        }
102
103
        $hydrator = new ObjectMap(
104
            [
105
                'displayName' => 'fullname',
106
            ]
107
        );
108
109
        /** @var User $user */
110
        $user = $hydrator->hydrate(new User(), $result);
111
112
        if ($result->name) {
113
            if ($result->name->first_name) {
114
                $user->firstname = $result->name->first_name;
115
            }
116
117
            if ($result->name->last_name) {
118
                $user->lastname = $result->name->last_name;
119
            }
120
        }
121
122
        return $user;
123
    }
124
}
125