Completed
Push — master ( 2798f7...d9c10c )
by
unknown
06:53 queued 04:03
created

ObjectUserEmailProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 5
dl 44
loc 44
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A loadUserByUsername() 14 14 3
A refreshUser() 10 10 2
A supportsClass() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Security;
4
5
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User;
6
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection;
7
use Symfony\Component\Security\Core\User\UserProviderInterface;
8
use Symfony\Component\Security\Core\User\UserInterface;
9
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
10
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
11
12 View Code Duplication
class ObjectUserEmailProvider implements UserProviderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    /** @var UserRepositoryCollection */
15
    private $userRepositoryCollection;
16
17
    /**
18
     * @param UserRepositoryCollection $userRepositoryCollection
19
     */
20
    public function __construct(UserRepositoryCollection $userRepositoryCollection)
21
    {
22
        $this->userRepositoryCollection = $userRepositoryCollection;
23
    }
24
25
    public function loadUserByUsername($username)
26
    {
27
        foreach ($this->userRepositoryCollection->all() as $repository) {
28
            $user = $repository->findByEmailAddress($username);
29
30
            if ($user instanceof User) {
31
                return $user;
32
            }
33
        }
34
35
        throw new UsernameNotFoundException(
36
            sprintf('Username "%s" does not exist.', $username)
37
        );
38
    }
39
40
    public function refreshUser(UserInterface $user)
41
    {
42
        if (!$this->supportsClass(get_class($user))) {
43
            throw new UnsupportedUserException(
44
                sprintf('Instances of "%s" are not supported.', get_class($user))
45
            );
46
        }
47
48
        return $this->loadUserByUsername($user->getUsername());
49
    }
50
51
    public function supportsClass($class)
52
    {
53
        return $this->userRepositoryCollection->supportsClass($class);
54
    }
55
}
56