Completed
Push — master ( 4ffaca...459278 )
by Дмитрий
01:55
created

MailRu::getIdentity()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.3183

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 20
cts 32
cp 0.625
rs 8.6315
c 0
b 0
f 0
cc 5
eloc 30
nc 5
nop 1
crap 6.3183
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\Provider\AccessTokenInterface;
10
use SocialConnect\Provider\Exception\InvalidAccessToken;
11
use SocialConnect\Provider\Exception\InvalidResponse;
12
use SocialConnect\OAuth2\AccessToken;
13
use SocialConnect\Common\Entity\User;
14
use SocialConnect\Common\Hydrator\ObjectMap;
15
16
class MailRu extends \SocialConnect\OAuth2\AbstractProvider
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 3
    public function getBaseUri()
22
    {
23 3
        return 'http://www.appsmail.ru/platform/api';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function getAuthorizeUri()
30
    {
31 2
        return 'https://connect.mail.ru/oauth/authorize';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function getRequestTokenUri()
38
    {
39 2
        return 'https://connect.mail.ru/oauth/token';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function getName()
46
    {
47 3
        return 'mail-ru';
48
    }
49
50
    /**
51
     * {@inheritdoc}
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
            $token = new AccessToken($result);
62 1
            $token->setUid($result['x_mailru_vid']);
63
64 1
            return $token;
65
        }
66
67 2
        throw new InvalidAccessToken('Provider response with not valid JSON');
68
    }
69
70
    /**
71
     * Copy/pasted from MailRU examples :)
72
     *
73
     * @param array $requestParameters
74
     * @return string
75
     */
76 2
    protected function makeSecureSignature(array $requestParameters)
77
    {
78 2
        ksort($requestParameters);
79
80 2
        $params = '';
81
82 2
        foreach ($requestParameters as $key => $value) {
83 2
            $params .= "$key=$value";
84 2
        }
85
86 2
        return md5($params . $this->consumer->getSecret());
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 2
    public function getIdentity(AccessTokenInterface $accessToken)
93
    {
94
        $parameters = [
95 2
            'client_id' => $this->consumer->getKey(),
96 2
            'format' => 'json',
97 2
            'method' => 'users.getInfo',
98 2
            'secure' => 1,
99 2
            'session_key' => $accessToken->getToken()
100 2
        ];
101
102 2
        $parameters['sig'] = $this->makeSecureSignature($parameters);
103
104 2
        $response = $this->httpClient->request(
105 2
            $this->getBaseUri(),
106
            $parameters
107 2
        );
108
109 2
        if (!$response->isSuccess()) {
110 1
            throw new InvalidResponse(
111 1
                'API response with error code',
112
                $response
113 1
            );
114
        }
115
116 1
        $result = $response->json();
117 1
        if (!$result) {
118 1
            throw new InvalidResponse(
119 1
                'API response is not a valid JSON object',
120
                $response
121 1
            );
122
        }
123
124
        $hydrator = new ObjectMap(
125
            [
126
                'uid' => 'id',
127
                'first_name' => 'firstname',
128
                'last_name' => 'lastname',
129
                'nick' => 'username',
130
                'pic_big' => 'pictureURL'
131
            ]
132
        );
133
134
        $user = $hydrator->hydrate(new User(), $result[0]);
135
136
        if ($user->sex) {
137
            $user->sex = $user->sex === 1 ? 'female' : 'male';
138
        }
139
140
        return $user;
141
    }
142
}
143