Passed
Push — heiglandreas-patch-1 ( c6f183...8b9b12 )
by Andreas
03:32
created

MailRu::getIdentity()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 2
rs 9.8666
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);
0 ignored issues
show
Deprecated Code introduced by
The function SocialConnect\Common\Entity\User::setSex() has been deprecated: Use setGender instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

124
                /** @scrutinizer ignore-deprecated */ $user->setSex($value === 1 ? User::SEX_FEMALE : User::SEX_MALE);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The constant SocialConnect\Common\Entity\User::SEX_MALE has been deprecated: Use GENDER_MALE instead! ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

124
                $user->setSex($value === 1 ? User::SEX_FEMALE : /** @scrutinizer ignore-deprecated */ User::SEX_MALE);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The constant SocialConnect\Common\Entity\User::SEX_FEMALE has been deprecated: Use GENDER_FEMALE instead! ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

124
                $user->setSex($value === 1 ? /** @scrutinizer ignore-deprecated */ User::SEX_FEMALE : User::SEX_MALE);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
125 1
            }
126
        ]);
127
128 1
        return $hydrator->hydrate(new User(), $response[0]);
129
    }
130
}
131