/
EditUserGroupCommand.php
91 lines (78 loc) · 2.52 KB
/
EditUserGroupCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types = 1);
/**
* /src/Command/User/EditUserGroupCommand.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
namespace App\Command\User;
use App\Command\Traits\SymfonyStyleTrait;
use App\DTO\UserGroup\UserGroupPatch as UserGroupDto;
use App\Entity\UserGroup as UserGroupEntity;
use App\Form\Type\Console\UserGroupType;
use App\Resource\UserGroupResource;
use Matthias\SymfonyConsoleForm\Console\Helper\FormHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
/**
* Class EditUserGroupCommand
*
* @package App\Command\User
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
class EditUserGroupCommand extends Command
{
use SymfonyStyleTrait;
public function __construct(
private UserGroupResource $userGroupResource,
private UserHelper $userHelper,
) {
parent::__construct('user:edit-group');
$this->setDescription('Command to edit existing user group');
}
/**
* @noinspection PhpMissingParentCallCommonInspection
*
* @throws Throwable
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = $this->getSymfonyStyle($input, $output);
$userGroup = $this->userHelper->getUserGroup($io, 'Which user group you want to edit?');
$message = $userGroup instanceof UserGroupEntity ? $this->updateUserGroup($input, $output, $userGroup) : null;
if ($input->isInteractive()) {
$io->success($message ?? 'Nothing changed - have a nice day');
}
return 0;
}
/**
* Method to update specified user group entity via specified form.
*
* @throws Throwable
*/
protected function updateUserGroup(
InputInterface $input,
OutputInterface $output,
UserGroupEntity $userGroup,
): string {
// Load entity to DTO
$dtoLoaded = new UserGroupDto();
$dtoLoaded->load($userGroup);
/** @var FormHelper $helper */
$helper = $this->getHelper('form');
/** @var UserGroupDto $dtoEdit */
$dtoEdit = $helper->interactUsingForm(
UserGroupType::class,
$input,
$output,
[
'data' => $dtoLoaded,
]
);
// Patch user group
$this->userGroupResource->patch($userGroup->getId(), $dtoEdit);
return 'User group updated - have a nice day';
}
}