LdapUserProvider::loadUserByUsername()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 23
ccs 17
cts 17
cp 1
crap 2
rs 9.7998
1
<?php
2
3
namespace DoL\LdapBundle\Security\User;
4
5
use DoL\LdapBundle\Ldap\LdapManagerInterface;
6
use Psr\Log\LoggerInterface;
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
/**
13
 * Provides users from Ldap.
14
 */
15
class LdapUserProvider implements UserProviderInterface
16
{
17
    /** @var LdapManagerInterface */
18
    protected $ldapManager;
19
20
    /** @var null|LoggerInterface */
21
    protected $logger;
22
23 3
    public function __construct(LdapManagerInterface $ldapManager, LoggerInterface $logger = null)
24
    {
25 3
        $this->ldapManager = $ldapManager;
26 3
        $this->logger = $logger;
27 3
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 3
    public function loadUserByUsername($username)
33
    {
34 3
        $user = $this->ldapManager->findUserByUsername($username);
35
36 3
        if (empty($user)) {
37 1
            $this->logInfo('User {username} {result} on LDAP', [
38 1
                'action' => 'loadUserByUsername',
39 1
                'username' => $username,
40 1
                'result' => 'not found',
41 1
            ]);
42 1
            $ex = new UsernameNotFoundException(sprintf('User "%s" not found', $username));
43 1
            $ex->setUsername($username);
44
45 1
            throw $ex;
46
        }
47
48 2
        $this->logInfo('User {username} {result} on LDAP', [
49 2
            'action' => 'loadUserByUsername',
50 2
            'username' => $username,
51 2
            'result' => 'found',
52 2
        ]);
53
54 2
        return $user;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    public function refreshUser(UserInterface $user)
61
    {
62 1
        if (!$this->supportsClass(get_class($user))) {
63
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
64
        }
65
66 1
        return $this->loadUserByUsername($user->getUsername());
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 1
    public function supportsClass($class)
73
    {
74 1
        return true;
75
    }
76
77
    /**
78
     * Log a message into the logger if this exists.
79
     *
80
     * @param string $message
81
     * @param array  $context
82
     */
83 3
    private function logInfo($message, array $context = [])
84
    {
85 3
        if (!$this->logger) {
86
            return;
87
        }
88
89 3
        $this->logger->info($message, $context);
90 3
    }
91
}
92