|
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\Exception\ValidatorException; |
|
11
|
|
|
use Symfony\Component\Validator\Validator\RecursiveValidator; |
|
12
|
|
|
|
|
13
|
|
|
class FacebookUserProvider |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var RecursiveValidator |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $validator; |
|
19
|
|
|
|
|
20
|
|
|
protected $serializer; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(RecursiveValidator $validator, Serializer $serializer) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->validator = $validator; |
|
25
|
|
|
$this->serializer = $serializer; |
|
26
|
|
|
} |
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $accessToken |
|
29
|
|
|
* |
|
30
|
|
|
* @return |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getUser($accessToken) |
|
33
|
|
|
{ |
|
34
|
|
|
try { |
|
35
|
|
|
$client = new Client(); |
|
36
|
|
|
$result = $client->get( |
|
37
|
|
|
'https://graph.facebook.com/v2.8/me', |
|
38
|
|
|
['query' => [ |
|
39
|
|
|
'access_token' => $accessToken, |
|
40
|
|
|
'fields' => 'id, email, first_name, last_name', ], |
|
41
|
|
|
] |
|
42
|
|
|
); |
|
43
|
|
|
// todo: Add validation facebook response |
|
|
|
|
|
|
44
|
|
|
// $userFacebook = json_decode($result->getBody(), true); |
|
45
|
|
|
$userFacebook = $this->serializer->deserialize( |
|
46
|
|
|
$result->getBody(), |
|
|
|
|
|
|
47
|
|
|
FacebookResponse::class, |
|
48
|
|
|
'json' |
|
49
|
|
|
); |
|
50
|
|
|
$errors = $this->validator->validate($userFacebook); |
|
51
|
|
|
|
|
52
|
|
|
if (count($errors) > 0) { |
|
53
|
|
|
$errorsString = (string) $errors; |
|
54
|
|
|
|
|
55
|
|
|
throw new ValidatorException(sprintf('There are errors "%s"', $errorsString)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $userFacebook; |
|
59
|
|
|
} catch (TransferException $e) { |
|
60
|
|
|
throw new HttpException(400, 'Social login error'); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.