|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Otobank\Bundle\FacebookBundle\Security\User; |
|
4
|
|
|
|
|
5
|
|
|
use Facebook\Exceptions\FacebookResponseException; |
|
6
|
|
|
use Facebook\Exceptions\FacebookSDKException; |
|
7
|
|
|
use Facebook\Facebook; |
|
8
|
|
|
use Otobank\Bundle\FacebookBundle\Security\User\FacebookUser; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Psr\Log\NullLogger; |
|
11
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
|
12
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
|
13
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
15
|
|
|
|
|
16
|
|
|
class FacebookUserProvider implements UserProviderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var Facebook */ |
|
19
|
|
|
private $facebook; |
|
20
|
|
|
|
|
21
|
|
|
/** @var LoggerInterface */ |
|
22
|
|
|
private $logger; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Constructor |
|
26
|
|
|
* |
|
27
|
|
|
* @param Facebook $facebook |
|
28
|
|
|
* @param LoggerInterface $logger |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Facebook $facebook, LoggerInterface $logger = null) |
|
31
|
|
|
{ |
|
32
|
|
|
if (is_null($logger)) { |
|
33
|
|
|
$logger = new NullLogger(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->facebook = $facebook; |
|
37
|
|
|
$this->logger = $logger; |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function loadUserByUsername($username) |
|
44
|
|
|
{ |
|
45
|
|
|
try { |
|
46
|
|
|
$response = $this->facebook->get('/' . $username); |
|
47
|
|
|
|
|
48
|
|
|
return $response->getGraphNode(FacebookUser::class); |
|
49
|
|
|
} catch (FacebookResponseException $e) { |
|
50
|
|
|
$this->logger->info(sprintf('FacebookResponseException: %s', $e->getMessage())); |
|
51
|
|
|
} catch (FacebookSDKException $e) { |
|
52
|
|
|
$this->logger->warning(sprintf('FacebookSDKException: %s', $e->getMessage())); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
throw new UsernameNotFoundException( |
|
56
|
|
|
sprintf('Username "%s" does not exist.', $username) |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function refreshUser(UserInterface $user) |
|
64
|
|
|
{ |
|
65
|
|
|
if (!$user instanceof FacebookUser) { |
|
66
|
|
|
throw new UnsupportedUserException( |
|
67
|
|
|
sprintf('Instances of "%s" are not supported.', get_class($user)) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->loadUserByUsername($user->getUsername()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritDoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function supportsClass($class) |
|
78
|
|
|
{ |
|
79
|
|
|
return $class === FacebookUser::class; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.