1<?php
2
3namespace App\Repository;
4
5/**
6 * Repository class for fetching database status data presented under /admin
7 */
8class DatabaseStatusRepository
9{
10    /**
11     * Database handler.
12     * @var \PDO
13     */
14    private $dbh;
15
16    /**
17     * Class constructor.
18     */
19    public function __construct(\PDO $dbh)
20    {
21        $this->dbh = $dbh;
22    }
23
24    public function getMysqlVersion(): string
25    {
26        return $this->dbh->query('SELECT version() mysql_version')->fetchColumn(0);
27    }
28
29    /**
30     * @return string[]
31     */
32    public function findAllTables(): array
33    {
34        return $this->dbh->query('SHOW TABLES')->fetchAll(\PDO::FETCH_COLUMN);
35    }
36
37    /**
38     * @return array<int,string>
39     */
40    public function getNumberOfRowsInTables(): array
41    {
42        $numberOfRowsPerTable = [];
43
44        foreach ($this->findAllTables() as $tableName) {
45            $sql = sprintf('SELECT COUNT(*) FROM `%s`', $tableName);
46
47            $numberOfRowsPerTable[$tableName] = $this->dbh->query($sql)->fetchColumn(0);
48        }
49
50        return $numberOfRowsPerTable;
51    }
52
53    /**
54     * @return array<int,array<string,mixed>>
55     */
56    public function getStatusOfTables(): array
57    {
58        return $this->dbh->query('SHOW TABLE STATUS')->fetchAll();
59    }
60}
61