Completed
Push — master ( 4f8692...b64605 )
by US
15s queued 11s
created

PaginationDataHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 40
rs 10
c 1
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolvePage() 0 9 2
A retrieveData() 0 8 1
A resolveLimit() 0 9 2
A __construct() 0 3 1
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