UserProviderLoaderTrait::loadUserByUsername()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 14
cp 0
rs 9.4285
cc 3
eloc 9
nc 4
nop 1
crap 12
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