Completed
Push — master ( 6b3ae4...255f8e )
by Jeff
02:46
created

PasswordService::processUserPassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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
    private const SALT_LENGTH = 16;
19
20
    private $encoderFactory;
21
22
    public function __construct(EncoderFactoryInterface $encoderFactory)
23
    {
24
        $this->encoderFactory = $encoderFactory;
25
    }
26
27
    public function processUserPassword(User $user): void
28
    {
29
        if (null !== $user->getPlainPassword()) {
30
            $encoder = $this->encoderFactory->getEncoder($user);
31
            $user->setPassword(
32
                $encoder->encodePassword(
33
                    $user->getPlainPassword(),
34
                    substr(sha1(\random_bytes(50)), 0, self::SALT_LENGTH)
35
                )
36
            );
37
        }
38
    }
39
}
40