|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Services; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Model\FacebookResponse; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use GuzzleHttp\Exception\TransferException; |
|
8
|
|
|
use JMS\Serializer\Serializer; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
10
|
|
|
use Symfony\Component\Validator\Validator\RecursiveValidator; |
|
11
|
|
|
|
|
12
|
|
|
class FacebookUserProvider |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Serializer |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $serializer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var RecursiveValidator |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $validator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Serializer $serializer |
|
26
|
|
|
* @param RecursiveValidator $validator |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Serializer $serializer, RecursiveValidator $validator) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->serializer = $serializer; |
|
31
|
|
|
$this->validator = $validator; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $accessToken |
|
36
|
|
|
* |
|
37
|
|
|
* @return FacebookResponse |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getUser($accessToken) |
|
40
|
|
|
{ |
|
41
|
|
|
try { |
|
42
|
|
|
$client = new Client(); |
|
43
|
|
|
$result = $client->get( |
|
44
|
|
|
'https://graph.facebook.com/v2.8/me', |
|
45
|
|
|
['query' => [ |
|
46
|
|
|
'access_token' => $accessToken, |
|
47
|
|
|
'fields' => 'id, email, first_name, last_name', ], |
|
48
|
|
|
] |
|
49
|
|
|
); |
|
50
|
|
|
$userFacebook = $this->serializer->deserialize( |
|
51
|
|
|
$result->getBody(), |
|
|
|
|
|
|
52
|
|
|
FacebookResponse::class, |
|
53
|
|
|
'json' |
|
54
|
|
|
); |
|
55
|
|
|
$errors = $this->validator->validate($userFacebook); |
|
56
|
|
|
|
|
57
|
|
|
if (count($errors) > 0) { |
|
58
|
|
|
throw new HttpException(400, 'Social response validation error'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $userFacebook; |
|
|
|
|
|
|
62
|
|
|
} catch (TransferException $e) { |
|
63
|
|
|
throw new HttpException(400, 'Social login error'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: