UserFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 54
ccs 0
cts 30
cp 0
rs 10
c 1
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B create() 0 35 6
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\Factory\User;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
18
use Silverback\ApiComponentsBundle\Helper\Timestamped\TimestampedDataPersister;
19
use Silverback\ApiComponentsBundle\Repository\User\UserRepositoryInterface;
20
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
21
use Symfony\Component\Validator\Validator\ValidatorInterface;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
class UserFactory
27
{
28
    private EntityManagerInterface $entityManager;
29
    private ValidatorInterface $validator;
30
    private UserRepositoryInterface $userRepository;
31
    private TimestampedDataPersister $timestampedDataPersister;
32
    private UserPasswordHasherInterface $passwordHasher;
33
    private string $userClass;
34
35
    public function __construct(EntityManagerInterface $entityManager, ValidatorInterface $validator, UserRepositoryInterface $userRepository, TimestampedDataPersister $timestampedDataPersister, UserPasswordHasherInterface $passwordHasher, string $userClass)
36
    {
37
        $this->entityManager = $entityManager;
38
        $this->validator = $validator;
39
        $this->userRepository = $userRepository;
40
        $this->timestampedDataPersister = $timestampedDataPersister;
41
        $this->passwordHasher = $passwordHasher;
42
        $this->userClass = $userClass;
43
    }
44
45
    public function create(string $username, string $password, ?string $email = null, bool $inactive = false, bool $superAdmin = false, bool $admin = false, bool $overwrite = false): void
46
    {
47
        if (!$email) {
48
            $email = $username;
49
        }
50
51
        /** @var AbstractUser|null $user */
52
        $user = $overwrite ? $this->userRepository->loadUserByIdentifier($username) : null;
53
54
        if (!$user) {
55
            $user = new $this->userClass();
56
        }
57
58
        $encodedPassword = $this->passwordHasher->hashPassword($user, $password);
59
60
        $roles = ['ROLE_USER'];
61
        if ($superAdmin) {
62
            $roles = ['ROLE_SUPER_ADMIN'];
63
        } elseif ($admin) {
64
            $roles = ['ROLE_ADMIN'];
65
        }
66
67
        $user
68
            ->setUsername($username)
69
            ->setPassword($encodedPassword)
70
            ->setEmailAddress($email)
71
            ->setEnabled(!$inactive)
72
            ->setEmailAddressVerified(true)
73
            ->setRoles($roles);
74
75
        $this->timestampedDataPersister->persistTimestampedFields($user, true);
76
        $this->validator->validate($user);
77
78
        $this->entityManager->persist($user);
79
        $this->entityManager->flush();
80
    }
81
}
82