Completed
Push — master ( 483eda...cb59ac )
by Дмитрий
03:04
created

MailRu   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 10
dl 0
loc 128
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseUri() 0 4 1
A getAuthorizeUri() 0 4 1
A getRequestTokenUri() 0 4 1
A getName() 0 4 1
A parseToken() 0 20 4
A makeSecureSignature() 0 12 2
B getIdentity() 0 47 5
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Auth\Provider;
8
9
use SocialConnect\Auth\Provider\Exception\InvalidAccessToken;
10
use SocialConnect\Auth\Provider\Exception\InvalidResponse;
11
use SocialConnect\OAuth2\AccessToken;
12
use SocialConnect\Common\Entity\User;
13
use SocialConnect\Common\Hydrator\ObjectMap;
14
15
class MailRu extends \SocialConnect\OAuth2\AbstractProvider
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function getBaseUri()
21
    {
22
        return 'http://www.appsmail.ru/platform/api';
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getAuthorizeUri()
29
    {
30
        return 'https://connect.mail.ru/oauth/authorize';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getRequestTokenUri()
37
    {
38
        return 'https://connect.mail.ru/oauth/token';
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getName()
45
    {
46
        return 'mail-ru';
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function parseToken($body)
53
    {
54
        if (empty($body)) {
55
            throw new InvalidAccessToken('Provider response with empty body');
56
        }
57
58
        $result = json_decode($body);
59
        if ($result) {
60
            if (isset($result->access_token)) {
61
                $token = new AccessToken($result->access_token);
62
                $token->setUid($result->x_mailru_vid);
63
64
                return $token;
65
            }
66
67
            throw new InvalidAccessToken('Provider API returned without access_token field inside JSON');
68
        }
69
70
        throw new InvalidAccessToken('Provider response with not valid JSON');
71
    }
72
73
    /**
74
     * Copy/pasted from MailRU examples :)
75
     *
76
     * @param array $requestParameters
77
     * @return string
78
     */
79
    protected function makeSecureSignature(array $requestParameters)
80
    {
81
        ksort($requestParameters);
82
83
        $params = '';
84
85
        foreach ($requestParameters as $key => $value) {
86
            $params .= "$key=$value";
87
        }
88
89
        return md5($params . $this->consumer->getSecret());
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getIdentity(AccessToken $accessToken)
96
    {
97
        $parameters = [
98
            'client_id' => $this->consumer->getKey(),
99
            'format' => 'json',
100
            'method' => 'users.getInfo',
101
            'secure' => 1,
102
            'session_key' => $accessToken->getToken()
103
        ];
104
105
        $parameters['sig'] = $this->makeSecureSignature($parameters);
106
107
        $response = $this->service->getHttpClient()->request(
108
            $this->getBaseUri(),
109
            $parameters
110
        );
111
112
        if (!$response->isSuccess()) {
113
            throw new InvalidResponse(
114
                'API response with error code',
115
                $response
116
            );
117
        }
118
119
        $result = $response->json();
120
        if (!$result) {
121
            throw new InvalidResponse(
122
                'API response is not a valid JSON object',
123
                $response->getBody()
124
            );
125
        }
126
127
        $hydrator = new ObjectMap(array(
128
            'uid' => 'id',
129
            'first_name' => 'firstname',
130
            'last_name' => 'lastname',
131
            'nick' => 'username'
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