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

Odnoklassniki   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 10
dl 0
loc 121
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 17 4
A makeSecureSignature() 0 16 3
B getIdentity() 0 39 3
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
/**
16
 * @link https://apiok.ru/dev
17
 */
18
class Odnoklassniki extends \SocialConnect\OAuth2\AbstractProvider
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function getBaseUri()
24
    {
25
        return 'https://api.ok.ru/api/';
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getAuthorizeUri()
32
    {
33
        return 'http://connect.ok.ru/oauth/authorize';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getRequestTokenUri()
40
    {
41
        return 'http://api.odnoklassniki.ru/oauth/token.do';
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getName()
48
    {
49
        return 'odnoklassniki';
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function parseToken($body)
56
    {
57
        if (empty($body)) {
58
            throw new InvalidAccessToken('Provider response with empty body');
59
        }
60
61
        $result = json_decode($body);
62
        if ($result) {
63
            if (isset($result->access_token)) {
64
                return new AccessToken($result->access_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
     * @link https://apiok.ru/dev/methods/
75
     *
76
     * @param array $requestParameters
77
     * @return string
78
     */
79
    protected function makeSecureSignature(array $requestParameters, AccessToken $accessToken)
80
    {
81
        ksort($requestParameters);
82
83
        $params = '';
84
85
        foreach ($requestParameters as $key => $value) {
86
            if ($key === 'access_token') {
87
                continue;
88
            }
89
90
            $params .= "$key=$value";
91
        }
92
93
        return strtolower(md5($params . md5($accessToken->getToken() . $this->consumer->getSecret())));
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getIdentity(AccessToken $accessToken)
100
    {
101
        $parameters = [
102
            'application_key' => $this->consumer->getPublic(),
103
            'access_token' => $accessToken->getToken(),
104
            'format' => 'json'
105
        ];
106
107
        $parameters['sig'] = $this->makeSecureSignature($parameters, $accessToken);
108
109
        $response = $this->service->getHttpClient()->request(
110
            $this->getBaseUri() . 'users/getCurrentUser',
111
            $parameters
112
        );
113
114
        if (!$response->isSuccess()) {
115
            throw new InvalidResponse(
116
                'API response with error code',
117
                $response
118
            );
119
        }
120
121
        $result = $response->json();
122
        if (!$result) {
123
            throw new InvalidResponse(
124
                'API response is not a valid JSON object',
125
                $response->getBody()
126
            );
127
        }
128
129
        $hydrator = new ObjectMap(array(
130
            'uid' => 'id',
131
            'first_name' => 'firstname',
132
            'last_name' => 'lastname',
133
            'name' => 'fullname'
134
        ));
135
136
        return $hydrator->hydrate(new User(), $result);
137
    }
138
}
139