Completed
Push — master ( ad6910...d77fda )
by Tarmo
18s queued 12s
created

CreateRolesCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 26
rs 9.7998
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Command/User/CreateRolesCommand.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Command\User;
10
11
use App\Command\Traits\SymfonyStyleTrait;
12
use App\Entity\Role;
13
use App\Repository\RoleRepository;
14
use App\Security\RolesService;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Throwable;
20
use function array_map;
21
use function array_sum;
22
use function sprintf;
23
24
/**
25
 * Class CreateRolesCommand
26
 *
27
 * @package App\Command\User
28
 * @author TLe, Tarmo Leppänen <[email protected]>
29
 */
30
class CreateRolesCommand extends Command
31
{
32
    use SymfonyStyleTrait;
33
34
    public function __construct(
35
        private EntityManagerInterface $entityManager,
36
        private RoleRepository $roleRepository,
37
        private RolesService $rolesService,
38
    ) {
39
        parent::__construct('user:create-roles');
40
41
        $this->setDescription('Console command to create roles to database');
42
    }
43
44
    /**
45
     * @noinspection PhpMissingParentCallCommonInspection
46
     *
47
     * @throws Throwable
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output): int
50
    {
51
        $io = $this->getSymfonyStyle($input, $output);
52
53
        $created = array_sum(
54
            array_map(
55
                fn (string $role): int => $this->createRole($role),
56
                $this->rolesService->getRoles(),
57
            ),
58
        );
59
60
        $this->entityManager->flush();
61
62
        $removed = $this->clearRoles($this->rolesService->getRoles());
63
64
        if ($input->isInteractive()) {
65
            $message = sprintf(
66
                'Created total of %d role(s) and removed %d role(s) - have a nice day',
67
                $created,
68
                $removed,
69
            );
70
71
            $io->success($message);
72
        }
73
74
        return 0;
75
    }
76
77
    /**
78
     * Method to check if specified role exists on database and if not create
79
     * and persist it to database.
80
     *
81
     * @throws Throwable
82
     */
83
    private function createRole(string $role): int
84
    {
85
        $output = 0;
86
87
        if ($this->roleRepository->find($role) === null) {
88
            $entity = new Role($role);
89
90
            $this->entityManager->persist($entity);
91
92
            $output = 1;
93
        }
94
95
        return $output;
96
    }
97
98
    /**
99
     * Method to clean existing roles from database that does not really
100
     * exists.
101
     *
102
     * @param array<int, string> $roles
103
     */
104
    private function clearRoles(array $roles): int
105
    {
106
        return (int)$this->roleRepository->createQueryBuilder('role')
107
            ->delete()
108
            ->where('role.id NOT IN(:roles)')
109
            ->setParameter(':roles', $roles)
110
            ->getQuery()
111
            ->execute();
112
    }
113
}
114