Passed
Push — master ( 3ec415...9f2f47 )
by Daniel
16:25
created

UserFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 51
ccs 25
cts 26
cp 0.9615
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 32 5
A __construct() 0 8 1
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\UserRepository;
20
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
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 UserRepository $userRepository;
31
    private TimestampedDataPersister $timestampedDataPersister;
32
    private UserPasswordEncoderInterface $passwordEncoder;
33
    private string $userClass;
34
35 5
    public function __construct(EntityManagerInterface $entityManager, ValidatorInterface $validator, UserRepository $userRepository, TimestampedDataPersister $timestampedDataPersister, UserPasswordEncoderInterface $passwordEncoder, string $userClass)
36
    {
37 5
        $this->entityManager = $entityManager;
38 5
        $this->validator = $validator;
39 5
        $this->userRepository = $userRepository;
40 5
        $this->timestampedDataPersister = $timestampedDataPersister;
41 5
        $this->passwordEncoder = $passwordEncoder;
42 5
        $this->userClass = $userClass;
43 5
    }
44
45 2
    public function create(string $username, string $password, string $email = null, bool $inactive = false, bool $superAdmin = false, bool $overwrite = false): void
46
    {
47 2
        if (!$email) {
48
            $email = $username;
49
        }
50
51
        /** @var AbstractUser|null $user */
52 2
        $user = $overwrite ? $this->userRepository->loadUserByUsername($username) : null;
53
54 2
        if (!$user) {
55 1
            $user = new $this->userClass();
56
        }
57
58 2
        $encodedPassword = $this->passwordEncoder->encodePassword($user, $password);
59
60
        $user
61 2
            ->setUsername($username)
62 2
            ->setPassword($encodedPassword)
63 2
            ->setEmailAddress($email)
64 2
            ->setEnabled(!$inactive)
65 2
            ->setEmailAddressVerified(true)
66 2
            ->setRoles(
67
                [
68 2
                    $superAdmin ? 'ROLE_SUPER_ADMIN' : 'ROLE_USER',
69
                ]
70
            );
71
72 2
        $this->timestampedDataPersister->persistTimestampedFields($user, true);
73 2
        $this->validator->validate($user);
74
75 2
        $this->entityManager->persist($user);
76 2
        $this->entityManager->flush();
77 2
    }
78
}
79