PasswordService::processUserPassword()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App\Service;
12
13
use App\Entity\User;
14
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
15
16
class PasswordService
17
{
18
    /**
19
     * @var int
20
     */
21
    private const SALT_LENGTH = 16;
22
23
    private EncoderFactoryInterface $encoderFactory;
24
25
    public function __construct(EncoderFactoryInterface $encoderFactory)
26
    {
27
        $this->encoderFactory = $encoderFactory;
28
    }
29
30
    public function processUserPassword(User $user): void
31
    {
32
        if (null !== $user->getPlainPassword()) {
33
            $encoder = $this->encoderFactory->getEncoder($user);
34
            $user->setPassword(
35
                $encoder->encodePassword(
36
                    $user->getPlainPassword(),
37
                    substr(sha1(\random_bytes(50)), 0, self::SALT_LENGTH)
38
                )
39
            );
40
        }
41
    }
42
}
43