ListUsersCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 17 1
A getRows() 0 6 1
A __construct() 0 5 1
A getFormatterUser() 0 15 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Command/User/ListUsersCommand.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\Entity\User;
13
use App\Entity\UserGroup;
14
use App\Resource\UserResource;
15
use App\Security\RolesService;
16
use Closure;
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
use function array_map;
23
use function implode;
24
use function sprintf;
25
26
/**
27
 * Class ListUsersCommand
28
 *
29
 * @package App\Command\User
30
 * @author TLe, Tarmo Leppänen <[email protected]>
31
 */
32
#[AsCommand(
33
    name: self::NAME,
34
    description: 'Console command to list users',
35
)]
36
class ListUsersCommand extends Command
37
{
38
    use SymfonyStyleTrait;
39
40
    final public const NAME = 'user:list';
41
42
    public function __construct(
43
        private readonly UserResource $userResource,
44
        private readonly RolesService $roles,
45
    ) {
46
        parent::__construct();
47
    }
48
49
    /**
50
     * @noinspection PhpMissingParentCallCommonInspection
51
     *
52
     * @throws Throwable
53
     */
54
    protected function execute(InputInterface $input, OutputInterface $output): int
55
    {
56
        $io = $this->getSymfonyStyle($input, $output);
57
58
        $headers = [
59
            'Id',
60
            'Username',
61
            'Email',
62
            'Full name',
63
            'Roles (inherited)',
64
            'Groups',
65
        ];
66
67
        $io->title('Current users');
68
        $io->table($headers, $this->getRows());
69
70
        return 0;
71
    }
72
73
    /**
74
     * Getter method for formatted user rows for console table.
75
     *
76
     * @return array<int, string>
77
     *
78
     * @throws Throwable
79
     */
80
    private function getRows(): array
81
    {
82
        return array_map(
83
            $this->getFormatterUser(),
84
            $this->userResource->find(orderBy: [
85
                'username' => 'ASC',
86
            ])
87
        );
88
    }
89
90
    /**
91
     * Getter method for user formatter closure. This closure will format
92
     * single User entity for console table.
93
     */
94
    private function getFormatterUser(): Closure
95
    {
96
        $userGroupFormatter = static fn (UserGroup $userGroup): string => sprintf(
97
            '%s (%s)',
98
            $userGroup->getName(),
99
            $userGroup->getRole()->getId(),
100
        );
101
102
        return fn (User $user): array => [
103
            $user->getId(),
104
            $user->getUsername(),
105
            $user->getEmail(),
106
            $user->getFirstName() . ' ' . $user->getLastName(),
107
            implode(",\n", $this->roles->getInheritedRoles($user->getRoles())),
108
            implode(",\n", $user->getUserGroups()->map($userGroupFormatter)->toArray()),
109
        ];
110
    }
111
}
112