Completed
Branch master (bda8d5)
by Дмитрий
02:19
created

Steein::getIdentity()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 46
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.8984

Importance

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