BaseRepository::getResultByPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 12
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repository;
6
7
abstract class BaseRepository
8
{
9
    public function __construct(protected \PDO $database)
10
    {
11 56
    }
12
13 56
    protected function getDb(): \PDO
14 56
    {
15
        return $this->database;
16 39
    }
17
18 39
    protected function getResultsWithPagination(
19
        string $query,
20
        int $page,
21 4
        int $perPage,
22
        array $params,
23
        int $total
24
    ): array {
25
        return [
26
            'pagination' => [
27
                'totalRows' => $total,
28
                'totalPages' => ceil($total / $perPage),
29
                'currentPage' => $page,
30 4
                'perPage' => $perPage,
31 4
            ],
32 4
            'data' => $this->getResultByPage($query, $page, $perPage, $params),
33 4
        ];
34
    }
35 4
36
    /**
37
     * @param array<string> $params
38
     * @return array<float|int|string>
39 4
     */
40
    protected function getResultByPage(
41
        string $query,
42
        int $page,
43
        int $perPage,
44
        array $params
45 4
    ): array {
46 4
        $offset = ($page - 1) * $perPage;
47 4
        $query .= " LIMIT ${perPage} OFFSET ${offset}";
48 4
        $statement = $this->database->prepare($query);
49
        $statement->execute($params);
50 4
51
        return (array) $statement->fetchAll();
52
    }
53
}
54