Odnoklassniki   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenParams() 0 8 1
A requestToken() 0 10 1
A dispatchUserInfo() 0 13 1
A getTokenUri() 0 3 1
A getOauthUri() 0 3 1
A getUserInfoUri() 0 8 1
A getDesiredFields() 0 3 1
1
<?php
2
namespace Mezon\SocialNetwork\Auth;
3
4
use Mezon\SocialNetwork\BaseAuth;
5
6
/**
7
 * Class Odnoklassniki
8
 *
9
 * @package BaseAuth
10
 * @subpackage Odnoklassniki
11
 * @author Dodonov A.A.
12
 * @version v.1.0 (2019/08/17)
13
 * @copyright Copyright (c) 2019, aeon.org
14
 */
15
16
/**
17
 * Class provides integration with OK
18
 *
19
 * @author Dodonov A.A.
20
 */
21
class Odnoklassniki extends BaseAuth
22
{
23
24
    /**
25
     * Method returns URL wich generates tokens
26
     *
27
     * @return string URL
28
     */
29
    public function getOauthUri(): string
30
    {
31
        return 'https://connect.ok.ru/oauth/authorize?scope=VALUABLE_ACCESS;PHOTO_CONTENT&';
32
    }
33
34
    /**
35
     *
36
     * {@inheritdoc}
37
     * @see \Mezon\SocialNetwork\BaseAuth::getUserInfoUri()
38
     */
39
    public function getUserInfoUri(string $token = ''): string
40
    {
41
        $signature = md5(
42
            'application_key=' . $this->settings['client_public'] . 'fields=' . $this->getDesiredFields() .
43
            'format=jsonmethod=users.getCurrentUser' . md5($token . $this->settings['client_secret']));
44
45
        return 'http://api.odnoklassniki.ru/fb.do?application_key=' . $this->settings['client_public'] .
46
            '&format=json&method=users.getCurrentUser&sig=' . $signature . '&';
47
    }
48
49
    /**
50
     * Method returns
51
     *
52
     * @return string URL
53
     */
54
    public function getTokenUri(): string
55
    {
56
        return 'http://api.odnoklassniki.ru/oauth/token.do?grant_type=authorization_code&';
57
    }
58
59
    /**
60
     * Method returns a list of desired fields
61
     *
62
     * @return string Comma separated of the desired fields
63
     */
64
    public function getDesiredFields(): string
65
    {
66
        return 'UID,LOCALE,FIRST_NAME,LAST_NAME,EMAIL,PIC190X190,PIC640X480';
67
    }
68
69
    /**
70
     * Method dispatches user info
71
     *
72
     * @param array $userInfo
73
     *            user info got from social network
74
     * @return array dispatched user info. Must be as array with keys id, first_name, last_name, email, picture
75
     */
76
    public function dispatchUserInfo(array $userInfo): array
77
    {
78
        $userInfo['email'] = $userInfo['email'] ?? '';
79
80
        $return = [
81
            'id' => $userInfo['uid'],
82
            'first_name' => $userInfo['first_name'],
83
            'last_name' => $userInfo['last_name'],
84
            'picture' => $userInfo['pic190x190'],
85
            'email' => $userInfo['email']
86
        ];
87
88
        return $return;
89
    }
90
91
    /**
92
     * Method returns params for getting token
93
     *
94
     * @param string $code
95
     *            - Access code
96
     * @return array Params
97
     */
98
    public function getTokenParams(string $code): array
99
    {
100
        return [
101
            'client_id' => $this->settings['client_id'],
102
            'redirect_uri' => $this->settings['redirect_uri'],
103
            'client_secret' => $this->settings['client_secret'],
104
            'grant_type' => 'authorization_code',
105
            'code' => $code
106
        ];
107
    }
108
109
    /**
110
     * Method requests token from server
111
     *
112
     * @param array $params
113
     *            - Request params
114
     * @return array Token data
115
     * @codeCoverageIgnore
116
     */
117
    public function requestToken(array $params): array
118
    {
119
        // TODO create CurlWrapperMock in InfraStructureLayer
120
        $result = \Mezon\CustomClient\CurlWrapper::sendRequest(
121
            'http://api.odnoklassniki.ru/oauth/token.do',
122
            [],
123
            'POST',
124
            $params);
125
126
        return json_decode($result[0], true);
0 ignored issues
show
Bug introduced by
It seems like $result[0] can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

126
        return json_decode(/** @scrutinizer ignore-type */ $result[0], true);
Loading history...
127
    }
128
}
129