Issues (2500)

app/Cli/Commands/UserList.php (2 issues)

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Cli\Commands;
21
22
use Fisharebest\Webtrees\Contracts\UserInterface;
0 ignored issues
show
The type Fisharebest\Webtrees\Contracts\UserInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\Registry;
24
use Fisharebest\Webtrees\Services\UserService;
25
use Fisharebest\Webtrees\User;
26
use Symfony\Component\Console\Helper\Table;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
use Symfony\Component\Console\Style\SymfonyStyle;
31
32
use function addcslashes;
33
use function array_map;
34
use function implode;
35
36
final class UserList extends AbstractCommand
37
{
38
    public function __construct(private readonly UserService $user_service)
39
    {
40
        parent::__construct();
41
    }
42
43
    protected function configure(): void
44
    {
45
        $this
46
            ->setName(name: 'user-list')
47
            ->setDescription(description: 'List users')
48
            ->addOption(
49
                name: 'format',
50
                shortcut: 'f',
51
                mode: InputOption::VALUE_REQUIRED,
52
                description: 'Output format (table, json, csv)',
53
                default: 'table',
54
            );
55
    }
56
57
    protected function execute(InputInterface $input, OutputInterface $output): int
58
    {
59
        $format = $this->stringOption(input: $input, name: 'format');
60
61
        $io = new SymfonyStyle(input: $input, output: $output);
62
63
        $users = $this->user_service->all()->sort(callback: fn ($a, $b) => $a->id() <=> $b->id());
64
65
        $headers = ['ID', 'Username', 'Real Name', 'Email', 'Admin', 'Approved', 'Verified', 'Language', 'Timezone', 'Contact', 'Registered', 'Last login'];
66
67
        $rows = $users->map(callback: fn (User $user): array => [
68
            'id'         => $user->id(),
69
            'username'   => $user->userName(),
70
            'real_name'  => $user->realName(),
71
            'email'      => $user->email(),
72
            'admin'      => $user->getPreference(setting_name: UserInterface::PREF_IS_ADMINISTRATOR) ? 'yes' : 'no',
73
            'approved'   => $user->getPreference(setting_name: UserInterface::PREF_IS_ACCOUNT_APPROVED) ? 'yes' : 'no',
74
            'verified'   => $user->getPreference(setting_name: UserInterface::PREF_IS_EMAIL_VERIFIED) ? 'yes' : 'no',
75
            'language'   => $user->getPreference(setting_name: UserInterface::PREF_LANGUAGE),
76
            'timezone'   => $user->getPreference(setting_name: UserInterface::PREF_TIME_ZONE),
77
            'contact'    => $user->getPreference(setting_name: UserInterface::PREF_CONTACT_METHOD),
78
            'registered' => $this->formatTimestamp(timestamp: (int) $user->getPreference(setting_name: UserInterface::PREF_TIMESTAMP_REGISTERED)),
79
            'last_login' => $this->formatTimestamp(timestamp: (int) $user->getPreference(setting_name: UserInterface::PREF_TIMESTAMP_ACTIVE)),
80
        ])
81
        ->values()
82
        ->all();
83
84
        switch ($format) {
85
            case 'table':
86
                $table = new Table(output: $output);
87
                $table->setHeaders(headers: $headers);
88
                $table->setRows(rows: $rows);
89
                $table->render();
90
                break;
91
92
            case 'csv':
93
                $output->writeln(messages: $this->quoteCsvRow(columns: $headers));
94
95
                foreach ($rows as $row) {
96
                    $output->writeln(messages: $this->quoteCsvRow(columns: $row));
97
                }
98
                break;
99
100
            case 'json':
101
                $output->writeln(messages: json_encode(value: $rows, flags: JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
102
                break;
103
104
            default:
105
                $io->error(message: 'Invalid format: ‘' . $format . '’');
106
107
                return self::FAILURE;
108
        }
109
110
        return self::SUCCESS;
111
    }
112
113
    private function formatTimestamp(int $timestamp): string
114
    {
115
        if ($timestamp === 0) {
116
            return '';
117
        }
118
119
        return Registry::timestampFactory()->make(timestamp: $timestamp)->format(format: 'Y-m-d H:i:s');
120
    }
121
122
    /**
123
     * @param array<string|int> $columns
124
     */
125
    private function quoteCsvRow(array $columns): string
126
    {
127
        $columns = array_map(callback: $this->quoteCsvValue(...), array: $columns);
128
129
        return implode(separator: ',', array: $columns);
130
    }
131
132
    private function quoteCsvValue(string|int $value): string
0 ignored issues
show
The method quoteCsvValue() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
133
    {
134
        return '"' . addcslashes(string: (string) $value, characters: '"') . '"';
135
    }
136
}
137