UserProviderLoaderTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 45
ccs 0
cts 28
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadUserByUsername() 0 16 3
A refreshUser() 0 11 2
A supportsClass() 0 4 1
1
<?php
2
3
namespace Majora\Framework\Loader\Bridge\Security;
4
5
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
6
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
9
/**
10
 * Trait which provides a bridge between majora loader and symfony security
11
 * user providers
12
 *
13
 * @property string entityClass
14
 * @property \ReflectionClass entityReflection
15
 */
16
trait UserProviderLoaderTrait
17
{
18
    /**
19
     * @see UserProviderInterface::loadUserByUsername()
20
     */
21
    public function loadUserByUsername($username)
22
    {
23
        $queryParameters = array('email' => $username);
24
        if ($this->entityReflection->implementsInterface('Majora\Framework\Model\EnablableInterface')) {
25
            $queryParameters['enabled'] = true;
26
        }
27
28
        if (!$person = $this->retrieveOne($queryParameters)) {
0 ignored issues
show
Bug introduced by
It seems like retrieveOne() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
            throw new UsernameNotFoundException(sprintf(
30
                'User "%s" is not an active person.',
31
                $username
32
            ));
33
        }
34
35
        return $person;
36
    }
37
38
    /**
39
     * @see UserProviderInterface::refreshUser()
40
     */
41
    public function refreshUser(UserInterface $user)
42
    {
43
        if (!$this->supportsClass(get_class($user))) {
44
            throw new UnsupportedUserException(sprintf(
45
                'Instances of "%s" are not supported.',
46
                get_class($user)
47
            ));
48
        }
49
50
        return $this->loadUserByUsername($user->getUsername());
51
    }
52
53
    /**
54
     * @see UserProviderInterface::supportsClass()
55
     */
56
    public function supportsClass($class)
57
    {
58
        return $class == $this->entityClass;
59
    }
60
}
61