Passed
Pull Request — master (#159)
by
unknown
08:56
created

PaginationDataHandler::retrieveData()   A

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 has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler;
14
15
final class PaginationDataHandler implements PaginationDataHandlerInterface
16
{
17
    /** @var int */
18
    private $defaultLimit;
19
20
    public function __construct(int $defaultLimit)
21
    {
22
        $this->defaultLimit = $defaultLimit;
23
    }
24
25
    public function retrieveData(array $requestData): array
26
    {
27
        $data = [];
28
29
        $this->resolvePage($requestData, $data);
30
        $this->resolveLimit($requestData, $data);
31
32
        return $data;
33
    }
34
35
    private function resolvePage(array $requestData, array &$data): void
36
    {
37
        $page = 1;
38
39
        if (isset($requestData[self::PAGE_INDEX])) {
40
            $page = (int) $requestData[self::PAGE_INDEX];
41
        }
42
43
        $data[self::PAGE_INDEX] = $page;
44
    }
45
46
    private function resolveLimit(array $requestData, array &$data): void
47
    {
48
        $limit = $this->defaultLimit;
49
50
        if (isset($requestData[self::LIMIT_INDEX])) {
51
            $limit = (int) $requestData[self::LIMIT_INDEX];
52
        }
53
54
        $data[self::LIMIT_INDEX] = $limit;
55
    }
56
}
57