Passed
Pull Request — master (#1181)
by Tarmo
06:39 queued 03:09
created

ListUserGroupsCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Command/User/ListUserGroupsCommand.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\UserGroupResource;
15
use Closure;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Throwable;
20
use function array_map;
21
use function implode;
22
use function sprintf;
23
24
/**
25
 * Class ListUserGroupsCommand
26
 *
27
 * @package App\Command\User
28
 * @author TLe, Tarmo Leppänen <[email protected]>
29
 */
30
class ListUserGroupsCommand extends Command
31
{
32
    use SymfonyStyleTrait;
33
34
    public function __construct(
35
        private UserGroupResource $userGroupResource,
36
    ) {
37
        parent::__construct('user:list-groups');
38
39
        $this->setDescription('Console command to list user groups');
40
    }
41
42
    /**
43
     * @noinspection PhpMissingParentCallCommonInspection
44
     *
45
     * @throws Throwable
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output): int
48
    {
49
        $io = $this->getSymfonyStyle($input, $output);
50
51
        $headers = [
52
            'Id',
53
            'Name',
54
            'Role',
55
            'Users',
56
        ];
57
58
        $io->title('Current user groups');
59
        $io->table($headers, $this->getRows());
60
61
        return 0;
62
    }
63
64
    /**
65
     * Getter method for formatted user group rows for console table.
66
     *
67
     * @return array<int, string>
68
     *
69
     * @throws Throwable
70
     */
71
    private function getRows(): array
72
    {
73
        return array_map($this->getFormatterUserGroup(), $this->userGroupResource->find(orderBy: ['name' => 'ASC']));
74
    }
75
76
    /**
77
     * Getter method for user group formatter closure. This closure will
78
     * format single UserGroup entity for console table.
79
     */
80
    private function getFormatterUserGroup(): Closure
81
    {
82
        $userFormatter = static fn (User $user): string => sprintf(
83
            '%s %s <%s>',
84
            $user->getFirstName(),
85
            $user->getLastName(),
86
            $user->getEmail(),
87
        );
88
89
        return static fn (UserGroup $userGroup): array => [
90
            $userGroup->getId(),
91
            $userGroup->getName(),
92
            $userGroup->getRole()->getId(),
93
            implode(",\n", $userGroup->getUsers()->map($userFormatter)->toArray()),
94
        ];
95
    }
96
}
97