Completed
Pull Request — master (#5)
by Valentyn
07:26
created

UserProvider::supportsClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
3
namespace App\Security;
4
5
use App\Repository\UserRepository;
6
use App\Entity\User;
7
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
8
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
use Symfony\Component\Security\Core\User\UserProviderInterface;
11
12
class UserProvider implements UserProviderInterface
13
{
14
    protected $userRepository;
15
16
    public function __construct(UserRepository $userRepository)
17
    {
18
        $this->userRepository = $userRepository;
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function loadUserByUsername($username)
25
    {
26
        $user = $this->findUser($username);
27
28
        if (!$user) {
29
            throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
30
        }
31
32
        return $user;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function refreshUser(UserInterface $user)
39
    {
40
        if (!$this->supportsClass(get_class($user))) {
41
            throw new UnsupportedUserException(sprintf('Expected an instance of %s, but got "%s".', User::class, get_class($user)));
42
        }
43
44
        /**
45
         * @var $user User
46
         */
47
48
        if (null === $reloadedUser = $this->userRepository->findBy(['id' => $user->getId()])) {
49
            throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId()));
50
        }
51
52
        return $reloadedUser;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $reloadedUser; (App\Entity\User[]) is incompatible with the return type declared by the interface Symfony\Component\Securi...rInterface::refreshUser of type Symfony\Component\Security\Core\User\UserInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function supportsClass($class)
59
    {
60
        $userClass = User::class;
61
        return $userClass === $class || is_subclass_of($class, $userClass);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $userClass can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
62
    }
63
64
    /**
65
     * Finds a user by username.
66
     *
67
     * This method is meant to be an extension point for child classes.
68
     *
69
     * @param string $username
70
     *
71
     * @return User|null
72
     */
73
    protected function findUser($username)
74
    {
75
        return $this->userRepository->loadUserByUsername($username);
76
    }
77
}