src/Command/User/EditUserGroupCommand.php
<?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 Override;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
/**
* @package App\Command\User
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
#[AsCommand(
name: self::NAME,
description: 'Command to edit existing user group',
)]
class EditUserGroupCommand extends Command
{
use SymfonyStyleTrait;
final public const string NAME = 'user:edit-group';
public function __construct(
private readonly UserGroupResource $userGroupResource,
private readonly UserHelper $userHelper,
) {
parent::__construct();
}
/**
* @noinspection PhpMissingParentCallCommonInspection
*
* @throws Throwable
*/
#[Override]
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';
}
}