CreateApiKeyCommand::checkUserGroups()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 3
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Command/ApiKey/CreateApiKeyCommand.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Command\ApiKey;
10
11
use App\Command\HelperConfigure;
12
use App\Command\Traits\ApiKeyUserManagementHelperTrait;
13
use App\Command\Traits\SymfonyStyleTrait;
14
use App\DTO\ApiKey\ApiKeyCreate as ApiKey;
15
use App\Form\Type\Console\ApiKeyType;
16
use App\Repository\RoleRepository;
17
use App\Resource\ApiKeyResource;
18
use App\Resource\UserGroupResource;
19
use App\Security\RolesService;
20
use Matthias\SymfonyConsoleForm\Console\Helper\FormHelper;
21
use Symfony\Component\Console\Attribute\AsCommand;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26
use Throwable;
27
28
/**
29
 * Class CreateApiKeyCommand
30
 *
31
 * @package App\Command\ApiKey
32
 * @author TLe, Tarmo Leppänen <[email protected]>
33
 */
34
#[AsCommand(
35
    name: self::NAME,
36
    description: 'Command to create new API key',
37
)]
38
class CreateApiKeyCommand extends Command
39
{
40
    use ApiKeyUserManagementHelperTrait;
41
    use SymfonyStyleTrait;
42
43
    final public const NAME = 'api-key:create';
44
45
    /**
46
     * @var array<int, array<string, string>>
47
     */
48
    private static array $commandParameters = [
49
        [
50
            'name' => 'description',
51
            'description' => 'Description',
52
        ],
53
    ];
54
55
    public function __construct(
56
        private readonly ApiKeyHelper $apiKeyHelper,
57
        private readonly ApiKeyResource $apiKeyResource,
58
        private readonly UserGroupResource $userGroupResource,
59
        private readonly RolesService $rolesService,
60
        private readonly RoleRepository $roleRepository,
61
    ) {
62
        parent::__construct();
63
    }
64
65
    public function getRolesService(): RolesService
66
    {
67
        return $this->rolesService;
68
    }
69
70
    protected function configure(): void
71
    {
72
        parent::configure();
73
74
        HelperConfigure::configure($this, self::$commandParameters);
75
    }
76
77
    /**
78
     * @noinspection PhpMissingParentCallCommonInspection
79
     *
80
     * @throws Throwable
81
     */
82
    protected function execute(InputInterface $input, OutputInterface $output): int
83
    {
84
        $io = $this->getSymfonyStyle($input, $output);
85
86
        // Check that user group(s) exists
87
        $this->checkUserGroups($io, $output, $input->isInteractive());
88
89
        /** @var FormHelper $helper */
90
        $helper = $this->getHelper('form');
91
92
        /** @var ApiKey $dto */
93
        $dto = $helper->interactUsingForm(ApiKeyType::class, $input, $output);
94
95
        // Create new API key
96
        $apiKey = $this->apiKeyResource->create($dto);
97
98
        if ($input->isInteractive()) {
99
            $io->success($this->apiKeyHelper->getApiKeyMessage('API key created - have a nice day', $apiKey));
100
        }
101
102
        return 0;
103
    }
104
105
    /**
106
     * Method to check if database contains user groups, if non exists method will run 'user:create-group' command
107
     * to create those automatically according to '$this->roles->getRoles()' output. Basically this will automatically
108
     * create user groups for each role that is defined to application.
109
     *
110
     * Also note that if groups are not found method will reset application 'role' table content, so that we can be
111
     * sure that we can create all groups correctly.
112
     *
113
     * @throws Throwable
114
     */
115
    private function checkUserGroups(SymfonyStyle $io, OutputInterface $output, bool $interactive): void
116
    {
117
        if ($this->userGroupResource->count() !== 0) {
118
            return;
119
        }
120
121
        if ($interactive) {
122
            $io->block(['User groups are not yet created, creating those now...']);
123
        }
124
125
        // Reset roles
126
        $this->roleRepository->reset();
127
128
        // Create user groups for each roles
129
        $this->createUserGroups($output);
130
    }
131
}
132