Passed
Pull Request — master (#67)
by Daniel
06:16
created

UserChangesProcessor::encodePassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 6
rs 10
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\Helper\User;
15
16
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
17
use Silverback\ApiComponentsBundle\Security\TokenGenerator;
18
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
class UserChangesProcessor
24
{
25
    private UserPasswordEncoderInterface $passwordEncoder;
26
    private bool $initialEmailVerifiedState;
27
    private bool $verifyEmailOnRegister;
28
    private bool $verifyEmailOnChange;
29
30
    public function __construct(UserPasswordEncoderInterface $passwordEncoder, bool $initialEmailVerifiedState, bool $verifyEmailOnRegister, bool $verifyEmailOnChange)
31
    {
32
        $this->passwordEncoder = $passwordEncoder;
33
        $this->initialEmailVerifiedState = $initialEmailVerifiedState;
34
        $this->verifyEmailOnRegister = $verifyEmailOnRegister;
35
        $this->verifyEmailOnChange = $verifyEmailOnChange;
36
    }
37
38
    public function processChanges(AbstractUser $user, ?AbstractUser $previousUser): void
39
    {
40
        $this->encodePassword($user);
41
        $user->setEmailAddressVerified($this->initialEmailVerifiedState);
42
43
        if (!$previousUser && !$this->initialEmailVerifiedState) {
44
            $user->setNewEmailAddress($user->getEmailAddress());
45
            if ($this->verifyEmailOnRegister) {
46
                $user->setNewEmailVerificationToken(TokenGenerator::generateToken());
47
            }
48
        }
49
50
        if ($previousUser && $previousUser->getNewEmailAddress() !== ($newEmail = $user->getNewEmailAddress())) {
51
            if ($this->verifyEmailOnChange) {
52
                $user->setNewEmailVerificationToken(TokenGenerator::generateToken());
53
            } else {
54
                $user->setEmailAddress($newEmail);
55
                $user->setNewEmailAddress(null);
56
            }
57
        }
58
    }
59
60
    private function encodePassword(AbstractUser $entity): bool
61
    {
62
        if (!$entity->getPlainPassword()) {
63
            return false;
64
        }
65
        $encoded = $this->passwordEncoder->encodePassword($entity, $entity->getPlainPassword());
66
        $entity->setPassword($encoded);
67
        $entity->eraseCredentials();
68
69
        return true;
70
    }
71
}
72