Passed
Push — master ( 664e38...4397d3 )
by Daniel
27:55 queued 20:15
created

UserFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 6
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
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\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 $admin = 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 2
        $roles = ['ROLE_USER'];
61 2
        if ($superAdmin) {
62 1
            $roles = ['ROLE_SUPER_ADMIN'];
63 1
        } elseif ($admin) {
64
            $roles = ['ROLE_ADMIN'];
65
        }
66
67
        $user
68 2
            ->setUsername($username)
69 2
            ->setPassword($encodedPassword)
70 2
            ->setEmailAddress($email)
71 2
            ->setEnabled(!$inactive)
72 2
            ->setEmailAddressVerified(true)
73 2
            ->setRoles($roles);
74
75 2
        $this->timestampedDataPersister->persistTimestampedFields($user, true);
76 2
        $this->validator->validate($user);
77
78 2
        $this->entityManager->persist($user);
79 2
        $this->entityManager->flush();
80 2
    }
81
}
82