PasswordUpdater   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A hashPassword() 0 16 4
1
<?php
2
3
/*
4
 * This file is part of the DoyoUserBundle project.
5
 *
6
 * (c) Anthonius Munthi <[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 Doyo\UserBundle\Util;
15
16
use Doyo\UserBundle\Model\UserInterface;
17
use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder;
18
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
19
20
class PasswordUpdater implements PasswordUpdaterInterface
21
{
22
    /**
23
     * @var EncoderFactoryInterface
24
     */
25
    private $encoderFactory;
26
27
    /**
28
     * PasswordUpdater constructor.
29
     */
30 18
    public function __construct(EncoderFactoryInterface $encoderFactory)
31
    {
32 18
        $this->encoderFactory = $encoderFactory;
33
    }
34
35 14
    public function hashPassword(UserInterface $user)
36
    {
37 14
        $plainPassword = $user->getPlainPassword();
38 14
        if (null === $plainPassword || 0 === \strlen($plainPassword)) {
39 12
            return;
40
        }
41 13
        $encoder = $this->encoderFactory->getEncoder($user);
42 13
        if ($encoder instanceof BCryptPasswordEncoder) {
43 12
            $user->setSalt(null);
44
        } else {
45 1
            $salt = rtrim(str_replace('+', '.', base64_encode(random_bytes(32))), '=');
46 1
            $user->setSalt($salt);
47
        }
48 13
        $hashedPassword = $encoder->encodePassword($plainPassword, $user->getSalt());
49 13
        $user->setPassword($hashedPassword);
50 13
        $user->eraseCredentials();
51
    }
52
}
53