Completed
Push — master ( 4c45e0...1a7b7a )
by Дмитрий
02:22
created

MailRu   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 85.42%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 48
dl 0
loc 127
rs 10
c 1
b 0
f 0
ccs 41
cts 48
cp 0.8542

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthorizeUri() 0 3 1
A makeSecureSignature() 0 11 2
A getBaseUri() 0 3 1
A getRequestTokenUri() 0 3 1
A parseToken() 0 15 3
A getIdentity() 0 49 5
A getName() 0 3 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\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
    const NAME = 'mail-ru';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 3
    public function getBaseUri()
24
    {
25 3
        return 'https://appsmail.ru/platform/api';
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function getAuthorizeUri()
32
    {
33 2
        return 'https://connect.mail.ru/oauth/authorize';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 2
    public function getRequestTokenUri()
40
    {
41 2
        return 'https://connect.mail.ru/oauth/token';
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 3
    public function getName()
48
    {
49 3
        return self::NAME;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 3
    public function parseToken($body)
56
    {
57 3
        if (empty($body)) {
58
            throw new InvalidAccessToken('Provider response with empty body');
59
        }
60
61 3
        $result = json_decode($body, true);
62 3
        if ($result) {
63 1
            $token = new AccessToken($result);
64 1
            $token->setUid($result['x_mailru_vid']);
65
66 1
            return $token;
67
        }
68
69 2
        throw new InvalidAccessToken('Provider response with not valid JSON');
70
    }
71
72
    /**
73
     * Copy/pasted from MailRU examples :)
74
     *
75
     * @param array $requestParameters
76
     * @return string
77
     */
78 2
    protected function makeSecureSignature(array $requestParameters)
79
    {
80 2
        ksort($requestParameters);
81
82 2
        $params = '';
83
84 2
        foreach ($requestParameters as $key => $value) {
85 2
            $params .= "$key=$value";
86
        }
87
88 2
        return md5($params . $this->consumer->getSecret());
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 2
    public function getIdentity(AccessTokenInterface $accessToken)
95
    {
96
        $parameters = [
97 2
            'client_id' => $this->consumer->getKey(),
98 2
            'format' => 'json',
99 2
            'method' => 'users.getInfo',
100 2
            'secure' => 1,
101 2
            'session_key' => $accessToken->getToken()
102
        ];
103
104 2
        $parameters['sig'] = $this->makeSecureSignature($parameters);
105
106 2
        $response = $this->httpClient->request(
107 2
            $this->getBaseUri(),
108 2
            $parameters
109
        );
110
111 2
        if (!$response->isSuccess()) {
112 1
            throw new InvalidResponse(
113 1
                'API response with error code',
114 1
                $response
115
            );
116
        }
117
118 1
        $result = $response->json();
119 1
        if (!$result) {
120 1
            throw new InvalidResponse(
121 1
                'API response is not a valid JSON object',
122 1
                $response
123
            );
124
        }
125
126
        $hydrator = new ObjectMap(
127
            [
128
                'uid' => 'id',
129
                'first_name' => 'firstname',
130
                'last_name' => 'lastname',
131
                'nick' => 'username',
132
                'pic_big' => 'pictureURL'
133
            ]
134
        );
135
136
        $user = $hydrator->hydrate(new User(), $result[0]);
137
138
        if ($user->sex) {
139
            $user->sex = $user->sex === 1 ? 'female' : 'male';
0 ignored issues
show
introduced by
The condition $user->sex === 1 is always false.
Loading history...
140
        }
141
142
        return $user;
143
    }
144
}
145