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

UserDeleteCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
ccs 4
cts 4
cp 1
rs 9.9666
cc 1
nc 1
nop 1
crap 1
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