Lines 100.00% 24 / 24
Methods 100.00% 2 / 2
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getList 100.00% 23 / 23 100.00% 1 / 1 7
26final readonly class RecentUsers
27{
28    public function __construct(
29        private Configuration $configuration,
30    ) {
31    }
32
33    /**
34     * Returns recent users for the admin dashboard.
35     *
36     * @return array<int, array<string, mixed>>
37     */
38    public function getList(int $limit = 5): array
39    {
40        $users = [];
41        $databaseDriver = $this->configuration->getDb();
42
43        $query = sprintf(
44            'SELECT fu.user_id, fu.login, fu.member_since, fud.display_name FROM %sfaquser fu LEFT JOIN %sfaquserdata fud '
45            . 'ON (fud.user_id = fu.user_id) WHERE fu.user_id <> -1 ORDER BY fu.member_since DESC',
46            Database::getTablePrefix(),
47            Database::getTablePrefix(),
48        );
49
50        $result = $databaseDriver->query($query, 0, $limit);
51        if ($result) {
52            while (true) {
53                $row = $databaseDriver->fetchArray($result);
54                if ($row === false || $row === null || $row === []) {
55                    break;
56                }
57
58                $users[] = [
59                    'id' => (int) ($row['user_id'] ?? 0),
60                    'login' => (string) ($row['login'] ?? ''),
61                    'display_name' => (string) ($row['display_name'] ?? ''),
62                    'member_since_iso' => ($row['member_since'] ?? '') === ''
63                        ? ''
64                        : Date::createIsoDate((string) $row['member_since']),
65                ];
66            }
67        }
68
69        return $users;
70    }
71}