Passed
Push — master ( 3ec415...9f2f47 )
by Daniel
16:25
created

UserPasswordValidator::validate()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
nc 5
nop 2
dl 0
loc 23
ccs 0
cts 13
cp 0
crap 56
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
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
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Validator\Constraints;
15
16
use Silverback\ApiComponentsBundle\Repository\User\UserRepository;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
19
use Symfony\Component\Security\Core\User\UserInterface;
20
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
21
use Symfony\Component\Validator\Constraint;
22
use Symfony\Component\Validator\ConstraintValidator;
23
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
24
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
25
26
/**
27
 * @author Daniel West <[email protected]>
28
 */
29
class UserPasswordValidator extends ConstraintValidator
30
{
31
    private TokenStorageInterface $tokenStorage;
32
    private EncoderFactoryInterface $encoderFactory;
33
    private UserRepository $userRepository;
34
35
    public function __construct(TokenStorageInterface $tokenStorage, EncoderFactoryInterface $encoderFactory, UserRepository $userRepository)
36
    {
37
        $this->tokenStorage = $tokenStorage;
38
        $this->encoderFactory = $encoderFactory;
39
        $this->userRepository = $userRepository;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function validate($password, Constraint $constraint): void
46
    {
47
        if (!$constraint instanceof UserPassword) {
48
            throw new UnexpectedTypeException($constraint, UserPassword::class);
49
        }
50
51
        if (null === $password || '' === $password) {
52
            $this->context->addViolation($constraint->message);
53
54
            return;
55
        }
56
57
        $databaselessUser = $this->tokenStorage->getToken()->getUser();
58
        $user = $this->userRepository->find($databaselessUser->getId());
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Symfony\Component\Security\Core\User\UserInterface. It seems like you code against a sub-type of Symfony\Component\Security\Core\User\UserInterface such as Silverback\ApiComponents...ntity\User\AbstractUser or Silverback\ApiComponents...tity\User\UserInterface or Silverback\ApiComponents...ntity\User\AbstractUser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $user = $this->userRepository->find($databaselessUser->/** @scrutinizer ignore-call */ getId());
Loading history...
Bug introduced by
The method getId() does not exist on Stringable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $user = $this->userRepository->find($databaselessUser->/** @scrutinizer ignore-call */ getId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
60
        if (!$user instanceof UserInterface) {
61
            throw new ConstraintDefinitionException('The User object must implement the UserInterface interface.');
62
        }
63
64
        $encoder = $this->encoderFactory->getEncoder($user);
65
66
        if (null === $user->getPassword() || !$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $user->getSalt() targeting Silverback\ApiComponents...AbstractUser::getSalt() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
67
            $this->context->addViolation($constraint->message);
68
        }
69
    }
70
}
71