RegistrationCodeManager::complete()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 2
b 0
f 0
nc 3
nop 3
dl 0
loc 23
ccs 0
cts 13
cp 0
crap 12
rs 9.7333
1
<?php
2
3
namespace App\Security\Registration;
4
5
use App\Entity\RegistrationCode;
6
use App\Entity\User;
7
use App\Repository\RegistrationCodeRepositoryInterface;
8
use App\Repository\UserRepositoryInterface;
9
use App\Repository\UserTypeRepositoryInterface;
10
use App\Security\EmailConfirmation\ConfirmationManager;
11
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
12
13
class RegistrationCodeManager {
14
15
    public function __construct(private RegistrationCodeRepositoryInterface $codeRepository, private UserRepositoryInterface $userRepository, private UserPasswordHasherInterface $passwordHasher, private UserTypeRepositoryInterface $typeRepository, private ConfirmationManager $confirmationManager)
16
    {
17
    }
18
19
    public function getTemplateUser(): User {
20
        $type = $this->typeRepository->findOneByAlias('parent');
21
        $user = new User();
22
        $user->setType($type);
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type null; however, parameter $userType of App\Entity\User::setType() does only seem to accept App\Entity\UserType, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
        $user->setType(/** @scrutinizer ignore-type */ $type);
Loading history...
23
24
        return $user;
25
    }
26
27
    public function isRedeemed(RegistrationCode $code): bool {
28
        return $code->getRedeemingUser() !== null;
29
    }
30
31
    /**
32
     * @throws CodeAlreadyRedeemedException
33
     */
34
    public function complete(RegistrationCode $code, User $user, string $password): void {
35
        if($this->isRedeemed($code)) {
36
            throw new CodeAlreadyRedeemedException();
37
        }
38
39
        $type = $this->typeRepository->findOneByAlias('parent');
40
        $user
41
            ->setType($type)
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type null; however, parameter $userType of App\Entity\User::setType() does only seem to accept App\Entity\UserType, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            ->setType(/** @scrutinizer ignore-type */ $type)
Loading history...
42
            ->setIsEmailConfirmationPending($user->getEmail() !== null)
43
            ->setPassword($this->passwordHasher->hashPassword($user, $password));
44
45
        $user->addLinkedStudent($code->getStudent());
0 ignored issues
show
Bug introduced by
It seems like $code->getStudent() can also be of type null; however, parameter $user of App\Entity\User::addLinkedStudent() does only seem to accept App\Entity\User, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $user->addLinkedStudent(/** @scrutinizer ignore-type */ $code->getStudent());
Loading history...
46
        $code->setRedeemingUser($user);
47
48
        $this->userRepository->persist($user);
49
        $this->codeRepository->persist($code);
50
51
        if($user->getEmail() !== null) {
52
            $this->confirmationManager->newConfirmation($user, $user->getEmail());
53
            $user->setIsEmailConfirmationPending(true);
54
            $user->setEmail(null);
55
56
            $this->userRepository->persist($user);
57
        }
58
    }
59
}