ObjectUserEmailProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 4
dl 56
loc 56
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A loadUserByUsername() 15 15 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\BaseUserRepositoryCollection;
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 BaseUserRepositoryCollection */
15
    private $userRepositoryCollection;
16
17
    public function __construct(BaseUserRepositoryCollection $userRepositoryCollection)
18
    {
19
        $this->userRepositoryCollection = $userRepositoryCollection;
20
    }
21
22
    /**
23
     * @param string $emailAddress
24
     *
25
     * @return User
26
     *
27
     * @throws UsernameNotFoundException
28
     */
29
    public function loadUserByUsername($emailAddress): User
30
    {
31
        foreach ($this->userRepositoryCollection->all() as $repository) {
32
            $user = $repository->findByEmailAddress($emailAddress);
33
34
            if ($user instanceof User) {
35
                return $user;
36
            }
37
        }
38
39
        // Since we are using the email as username we keep this exception since it is a part of symfony
40
        throw new UsernameNotFoundException(
41
            sprintf('Email "%s" does not exist.', $emailAddress)
42
        );
43
    }
44
45
    /**
46
     * @param UserInterface $user
47
     *
48
     * @return User
49
     *
50
     * @throws UnsupportedUserException
51
     */
52
    public function refreshUser(UserInterface $user): User
53
    {
54
        if (!$this->supportsClass(get_class($user))) {
55
            throw new UnsupportedUserException(
56
                sprintf('Instances of "%s" are not supported.', get_class($user))
57
            );
58
        }
59
60
        return $this->loadUserByUsername($user->getEmail());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Security\Core\User\UserInterface as the method getEmail() does only exist in the following implementations of said interface: SumoCoders\FrameworkMult...rBundle\Entity\BaseUser.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
61
    }
62
63
    public function supportsClass($class): bool
64
    {
65
        return $this->userRepositoryCollection->supportsClass($class);
66
    }
67
}
68