Completed
Push — master ( 1425f5...557488 )
by Дмитрий
05:01 queued 02:42
created

MailRu   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 113
ccs 43
cts 43
cp 1
rs 10
wmc 13

8 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 getName() 0 3 1
A parseToken() 0 15 3
A getIdentity() 0 20 2
A prepareRequest() 0 9 2
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
declare(strict_types=1);
7
8
namespace SocialConnect\OAuth2\Provider;
9
10
use SocialConnect\Common\ArrayHydrator;
11
use SocialConnect\Provider\AccessTokenInterface;
12
use SocialConnect\Provider\Exception\InvalidAccessToken;
13
use SocialConnect\OAuth2\AccessToken;
14
use SocialConnect\Common\Entity\User;
15
16
class MailRu extends \SocialConnect\OAuth2\AbstractProvider
17
{
18
    const NAME = 'mail-ru';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 4
    public function getBaseUri()
24
    {
25 4
        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 4
    public function parseToken(string $body)
56
    {
57 4
        if (empty($body)) {
58 1
            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->setUserId((string) $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 3
    protected function makeSecureSignature(array $requestParameters)
79
    {
80 3
        ksort($requestParameters);
81
82 3
        $params = '';
83
84 3
        foreach ($requestParameters as $key => $value) {
85 3
            $params .= "$key=$value";
86
        }
87
88 3
        return md5($params . $this->consumer->getSecret());
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94 3
    public function prepareRequest(string $method, string $uri, array &$headers, array &$query, AccessTokenInterface $accessToken = null): void
95
    {
96 3
        $query['client_id'] = $this->consumer->getKey();
97 3
        $query['format'] = 'json';
98 3
        $query['secure'] = 1;
99
100 3
        if ($accessToken) {
101 3
            $query['session_key'] = $accessToken->getToken();
102 3
            $query['sig'] = $this->makeSecureSignature($query);
103
        }
104 3
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 3
    public function getIdentity(AccessTokenInterface $accessToken)
110
    {
111
        $query = [
112 3
            'method' => 'users.getInfo',
113
        ];
114
115 3
        $response = $this->request('GET', '', $query, $accessToken);
116
117 1
        $hydrator = new ArrayHydrator([
118 1
            'uid' => 'id',
119 1
            'first_name' => 'firstname',
120 1
            'last_name' => 'lastname',
121 1
            'nick' => 'username',
122 1
            'pic_big' => 'pictureURL',
123
            'sex' => static function ($value, User $user) {
124 1
                $user->setSex($value === 1 ? User::SEX_FEMALE : User::SEX_MALE);
125 1
            }
126
        ]);
127
128 1
        return $hydrator->hydrate(new User(), $response[0]);
129
    }
130
}
131