PaginationDataHandler::retrieveData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler;
12
13
final class PaginationDataHandler implements PaginationDataHandlerInterface
14
{
15
    /** @var int */
16
    private $defaultLimit;
17
18
    public function __construct(int $defaultLimit)
19
    {
20
        $this->defaultLimit = $defaultLimit;
21
    }
22
23
    public function retrieveData(array $requestData): array
24
    {
25
        $data = [];
26
27
        $this->resolvePage($requestData, $data);
28
        $this->resolveLimit($requestData, $data);
29
30
        return $data;
31
    }
32
33
    private function resolvePage(array $requestData, array &$data): void
34
    {
35
        $page = 1;
36
37
        if (isset($requestData[self::PAGE_INDEX])) {
38
            $page = (int) $requestData[self::PAGE_INDEX];
39
        }
40
41
        $data[self::PAGE_INDEX] = $page;
42
    }
43
44
    private function resolveLimit(array $requestData, array &$data): void
45
    {
46
        $limit = $this->defaultLimit;
47
48
        if (isset($requestData[self::LIMIT_INDEX])) {
49
            $limit = (int) $requestData[self::LIMIT_INDEX];
50
        }
51
52
        $data[self::LIMIT_INDEX] = $limit;
53
    }
54
}
55