Passed
Push — master ( c14464...f0ebd0 )
by Mikołaj
04:04
created

PaginationDataHandler::resolveLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
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
    /**
18
     * {@inheritdoc}
19
     */
20
    public function retrieveData(array $requestData): array
21
    {
22
        $data = [];
23
24
        $this->resolvePage($requestData, $data);
25
        $this->resolveLimit($requestData, $data);
26
27
        return $data;
28
    }
29
30
    /**
31
     * @param array $requestData
32
     * @param array $data
33
     */
34
    private function resolvePage(array $requestData, array &$data): void
35
    {
36
        $page = 1;
37
38
        if (isset($requestData[self::PAGE_INDEX])) {
39
            $page = (int) $requestData[self::PAGE_INDEX];
40
        }
41
42
        $data[self::PAGE_INDEX] = $page;
43
    }
44
45
    /**
46
     * @param array $requestData
47
     * @param array $data
48
     */
49
    private function resolveLimit(array $requestData, array &$data): void
50
    {
51
        $limit = self::DEFAULT_LIMIT;
52
53
        if (isset($requestData[self::LIMIT_INDEX])) {
54
            $limit = (int) $requestData[self::LIMIT_INDEX];
55
        }
56
57
        $data[self::LIMIT_INDEX] = $limit;
58
    }
59
}
60