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\Common\Entity\User; |
13
|
|
|
|
14
|
|
|
class Vk extends \SocialConnect\OAuth2\AbstractProvider |
15
|
|
|
{ |
16
|
|
|
const NAME = 'vk'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
protected $requestHttpMethod = 'GET'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Vk returns email inside AccessToken |
25
|
|
|
* |
26
|
|
|
* @var string|null |
27
|
|
|
*/ |
28
|
|
|
protected $email; |
29
|
|
|
|
30
|
4 |
|
public function getBaseUri() |
31
|
|
|
{ |
32
|
4 |
|
return 'https://api.vk.com/'; |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
public function getAuthorizeUri() |
36
|
|
|
{ |
37
|
2 |
|
return 'https://oauth.vk.com/authorize'; |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
public function getRequestTokenUri() |
41
|
|
|
{ |
42
|
2 |
|
return 'https://oauth.vk.com/access_token'; |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
public function getName() |
46
|
|
|
{ |
47
|
3 |
|
return self::NAME; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
3 |
|
public function prepareRequest(string $method, string $uri, array &$headers, array &$query, AccessTokenInterface $accessToken = null): void |
54
|
|
|
{ |
55
|
3 |
|
if ($accessToken) { |
56
|
3 |
|
$query['access_token'] = $accessToken->getToken(); |
57
|
|
|
} |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
3 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
64
|
|
|
{ |
65
|
|
|
$query = [ |
66
|
3 |
|
'v' => '5.100' |
67
|
|
|
]; |
68
|
|
|
|
69
|
3 |
|
$fields = $this->getArrayOption('identity.fields', []); |
70
|
3 |
|
if ($fields) { |
71
|
|
|
$query['fields'] = implode(',', $fields); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
$response = $this->request('GET', 'method/users.get', $query, $accessToken); |
75
|
|
|
|
76
|
1 |
|
$hydrator = new ArrayHydrator([ |
77
|
1 |
|
'id' => 'id', |
78
|
1 |
|
'first_name' => 'firstname', |
79
|
1 |
|
'last_name' => 'lastname', |
80
|
1 |
|
'email' => 'email', |
81
|
|
|
'bdate' => static function ($value, User $user) { |
82
|
|
|
$user->setBirthday( |
83
|
|
|
new \DateTime($value) |
84
|
|
|
); |
85
|
1 |
|
}, |
86
|
|
|
'sex' => static function ($value, User $user) { |
87
|
1 |
|
$user->setSex($value === 1 ? User::SEX_FEMALE : User::SEX_MALE); |
|
|
|
|
88
|
1 |
|
}, |
89
|
1 |
|
'screen_name' => 'username', |
90
|
1 |
|
'photo_max_orig' => 'pictureURL', |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
/** @var User $user */ |
94
|
1 |
|
$user = $hydrator->hydrate(new User(), $response['response'][0]); |
95
|
|
|
|
96
|
1 |
|
$user->email = $this->email; |
97
|
1 |
|
$user->emailVerified = true; |
98
|
|
|
|
99
|
1 |
|
return $user; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.