PasswordUpdater::hashPassword()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

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