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

Odnoklassniki::makeSecureSignature()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
rs 10
c 0
b 0
f 0
ccs 8
cts 8
cp 1
cc 3
nc 3
nop 2
crap 3
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\Consumer;
13
use SocialConnect\Common\Entity\User;
14
15
/**
16
 * @link https://apiok.ru/dev
17
 */
18
class Odnoklassniki extends \SocialConnect\OAuth2\AbstractProvider
19
{
20
    const NAME = 'odnoklassniki';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 4
    public function getBaseUri()
26
    {
27 4
        return 'https://api.ok.ru/api/';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 2
    public function getAuthorizeUri()
34
    {
35 2
        return 'http://connect.ok.ru/oauth/authorize';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 2
    public function getRequestTokenUri()
42
    {
43 2
        return 'http://api.odnoklassniki.ru/oauth/token.do';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 3
    public function getName()
50
    {
51 3
        return self::NAME;
52
    }
53
54
    /**
55
     * @link https://apiok.ru/dev/methods/
56
     *
57
     * @param array $requestParameters
58
     * @param AccessTokenInterface $accessToken
59
     * @return string
60
     */
61 3
    protected function makeSecureSignature(array $requestParameters, AccessTokenInterface $accessToken)
62
    {
63 3
        ksort($requestParameters);
64
65 3
        $params = '';
66
67 3
        foreach ($requestParameters as $key => $value) {
68 3
            if ($key === 'access_token') {
69 3
                continue;
70
            }
71
72 3
            $params .= "$key=$value";
73
        }
74
75 3
        return strtolower(md5($params . md5($accessToken->getToken() . $this->consumer->getSecret())));
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 3
    public function getIdentity(AccessTokenInterface $accessToken)
82
    {
83
        $parameters = [
84 3
            'application_key' => $this->consumer->getPublic(),
85 3
            'access_token' => $accessToken->getToken(),
86 3
            'format' => 'json'
87
        ];
88
89 3
        $parameters['sig'] = $this->makeSecureSignature($parameters, $accessToken);
90
91 3
        $response = $this->request('GET', 'users/getCurrentUser', [], $accessToken);
92
93 1
        $hydrator = new ArrayHydrator([
94 1
            'uid' => 'id',
95
            'first_name' => 'firstname',
96
            'last_name' => 'lastname',
97
            'name' => 'fullname',
98
            'pic_3' => 'pictureURL'
99
        ]);
100
101 1
        return $hydrator->hydrate(new User(), $response);
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107 15
    protected function createConsumer(array $parameters): Consumer
108
    {
109 15
        $consumer = parent::createConsumer($parameters);
110 15
        $consumer->setPublic(
111 15
            $this->getRequiredStringParameter('applicationPublic', $parameters)
112
        );
113
114 15
        return $consumer;
115
    }
116
}
117