EditUserCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 11 3
A updateUser() 0 23 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Command/User/EditUserCommand.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\DTO\User\UserUpdate as UserDto;
13
use App\Entity\User as UserEntity;
14
use App\Form\Type\Console\UserType;
15
use App\Resource\UserResource;
16
use Matthias\SymfonyConsoleForm\Console\Helper\FormHelper;
17
use Symfony\Component\Console\Attribute\AsCommand;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Throwable;
22
23
/**
24
 * Class EditUserCommand
25
 *
26
 * @package App\Command\User
27
 * @author TLe, Tarmo Leppänen <[email protected]>
28
 */
29
#[AsCommand(
30
    name: self::NAME,
31
    description: 'Command to edit existing user',
32
)]
33
class EditUserCommand extends Command
34
{
35
    use SymfonyStyleTrait;
36
37
    final public const NAME = 'user:edit';
38
39
    public function __construct(
40
        private readonly UserResource $userResource,
41
        private readonly UserHelper $userHelper,
42
    ) {
43
        parent::__construct();
44
    }
45
46
    /**
47
     * @noinspection PhpMissingParentCallCommonInspection
48
     *
49
     * @throws Throwable
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output): int
52
    {
53
        $io = $this->getSymfonyStyle($input, $output);
54
        $user = $this->userHelper->getUser($io, 'Which user you want to edit?');
55
        $message = $user instanceof UserEntity ? $this->updateUser($input, $output, $user) : null;
56
57
        if ($input->isInteractive()) {
58
            $io->success($message ?? 'Nothing changed - have a nice day');
59
        }
60
61
        return 0;
62
    }
63
64
    /**
65
     * Method to update specified user entity via specified form.
66
     *
67
     * @throws Throwable
68
     */
69
    private function updateUser(InputInterface $input, OutputInterface $output, UserEntity $user): string
70
    {
71
        // Load entity to DTO
72
        $dtoLoaded = new UserDto();
73
        $dtoLoaded->load($user);
74
75
        /** @var FormHelper $helper */
76
        $helper = $this->getHelper('form');
77
78
        /** @var UserDto $dtoEdit */
79
        $dtoEdit = $helper->interactUsingForm(
80
            UserType::class,
81
            $input,
82
            $output,
83
            [
84
                'data' => $dtoLoaded,
85
            ]
86
        );
87
88
        // Patch user
89
        $this->userResource->patch($user->getId(), $dtoEdit);
90
91
        return 'User updated - have a nice day';
92
    }
93
}
94