Passed
Push — main ( ad42a0...8a363b )
by Daniel
05:42
created

UserDeleteCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.9
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Cli;
6
7
use Ahc\Cli\Input\Command;
8
use Ahc\Cli\IO\Interactor;
9
use Psr\Container\ContainerInterface;
10
use Throwable;
11
use Uxmp\Core\Component\User\UserDeleterInterface;
12
use Uxmp\Core\Orm\Repository\UserRepositoryInterface;
13
14
final class UserDeleteCommand extends Command
15
{
16 3
    public function __construct(
17
        private readonly ContainerInterface $dic
18
    ) {
19 3
        parent::__construct(
20
            'user:del',
21
            'Deletes a user'
22
        );
23
24
        $this
25 3
            ->argument(
26
                '<userId>',
27
                'User Id'
28
            )
29 3
            ->usage(
30
                '<bold>  $0 ud 666</end> <comment></end> ## Deletes the user with the id `666`<eol/>'
31
            );
32
    }
33
34 3
    public function execute(?string $userId): void
35
    {
36
        /** @var Interactor|null $io */
37 3
        $io = $this->app()?->io();
38 3
        $userId = (int) $userId;
39
40 3
        $user = $this->dic->get(UserRepositoryInterface::class)->find($userId);
41
42 3
        if ($user === null) {
43 1
            $io?->error(sprintf('User with id `%d` not found', $userId), true);
44 1
            return;
45
        }
46
47
        try {
48 2
            $this->dic->get(UserDeleterInterface::class)->delete($user);
49
50 1
            $io?->ok('The user has been deleted', true);
51 1
        } catch (Throwable) {
52 1
            $io?->error('Error deleting user (see logs)', true);
53
        }
54
    }
55
}
56