BaseRepository   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 45
ccs 17
cts 17
cp 1
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getResultByPage() 0 12 1
A getDb() 0 3 1
A getResultsWithPagination() 0 15 1
A __construct() 0 2 1
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