UniqueValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * This file is part of the FOSUserBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DoL\LdapBundle\Validator;
13
14
use DoL\LdapBundle\Ldap\LdapManagerInterface;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
use Symfony\Component\Validator\Constraint;
17
use Symfony\Component\Validator\ConstraintValidator;
18
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19
20
/**
21
 * UniqueValidator.
22
 *
23
 * @author DarwinOnLine
24
 * @author Maks3w
25
 *
26
 * @see https://github.com/DarwinOnLine/DoLLdapBundle
27
 */
28
class UniqueValidator extends ConstraintValidator
29
{
30
    /**
31
     * @var LdapManagerInterface
32
     */
33
    protected $ldapManager;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param LdapManagerInterface $ldapManager
39
     */
40 4
    public function __construct(LdapManagerInterface $ldapManager)
41
    {
42 4
        $this->ldapManager = $ldapManager;
43 4
    }
44
45
    /**
46
     * Checks if the passed value is valid.
47
     *
48
     * @param UserInterface $value      The value that should be validated
49
     * @param Constraint    $constraint The constraint for the validation
50
     *
51
     * @throws UnexpectedTypeException if $value is not instance of \Symfony\Component\Security\Core\User\UserInterface
52
     * @throws UnexpectedTypeException if $constraint is not instance of \DoL\LdapBundle\Validator\Unique
53
     */
54 4
    public function validate($value, Constraint $constraint)
55
    {
56 4
        if (!$constraint instanceof Unique) {
57 1
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Unique');
58
        }
59
60 3
        if (!$value instanceof UserInterface) {
0 ignored issues
show
introduced by
$value is always a sub-type of Symfony\Component\Security\Core\User\UserInterface.
Loading history...
61 1
            throw new UnexpectedTypeException($value, 'Symfony\Component\Security\Core\User\UserInterface');
62
        }
63
64 2
        $user = $this->ldapManager->findUserByUsername($value->getUsername());
65
66 2
        if ($user) {
67 1
            $this->context->addViolation($constraint->message);
68 1
        }
69 2
    }
70
}
71