FacebookUserProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A loadUserByUsername() 0 16 3
A refreshUser() 0 10 2
A supportsClass() 0 4 1
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger can also be of type object<Psr\Log\NullLogger>. However, the property $logger is declared as type object<Psr\Log\LoggerInterface>. Maybe add an additional type check?

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 $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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