ServerRequestQueryParamsFactory   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 55.88%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 124
ccs 19
cts 34
cp 0.5588
rs 10
c 0
b 0
f 0
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A mapRequest() 0 20 5
A getLimit() 0 7 2
A getDirection() 0 7 2
A getPage() 0 7 2
A build() 0 7 2
A setQueryParamMapping() 0 5 1
A getSortBy() 0 7 2
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Copyright (c) Phauthentic (https://github.com/Phauthentic)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Phauthentic (https://github.com/Phauthentic)
11
 * @link          https://github.com/Phauthentic
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
namespace Phauthentic\Pagination\ParamsFactory;
15
16
use Phauthentic\Pagination\PaginationParamsInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
19
/**
20
 * Extracts the pagination information from a server requests query string
21
 */
22
class ServerRequestQueryParamsFactory extends AbstractFactory
23
{
24
25
    /**
26
     * Parameter map
27
     *
28
     * @var array
29
     */
30
    protected $map = [
31
        'limit' => 'limit',
32
        'page' => 'page',
33
        'direction' => 'direction',
34
        'sortBy' => 'sort'
35
    ];
36
37
    /**
38
     * Gets the page number value from the server request params
39
     *
40
     * @param array $params Server request query params
41
     * @param string $name of the query param
42
     * @return int
43
     */
44
    public function getPage(array $params, string $name = 'page'): int
45
    {
46
        if (isset($params[$name])) {
47
            return (int)$params[$name];
48
        }
49
50
        return 1;
51
    }
52
53
    /**
54
     * Gets the limit value from the server request params
55
     *
56
     * @param array $params Server request query params
57
     * @param string $name of the query param
58
     * @return int
59
     */
60
    public function getLimit(array $params, string $name = 'limit'): int
61
    {
62
        if (isset($params[$name])) {
63
            return (int)$params['limit'];
64
        }
65
66
        return 20;
67
    }
68
69
    /**
70
     * Gets the sort value from the server request params
71
     *
72
     * @param array $params Server request query params
73
     * @param string $name of the query param
74
     * @return string|null
75
     */
76 2
    public function getSortBy(array $params, string $name = 'sort'): ?string
77
    {
78 2
        if (!empty($params[$name])) {
79 2
            return (string)$params[$name];
80
        }
81
82
        return null;
83
    }
84
85
    /**
86
     * Gets the direction value from the server request params
87
     *
88
     * @param array $params Server request query params
89
     * @param string $name of the query param
90
     * @return string Must be desc `desc`or `asc`
91
     */
92 2
    public function getDirection(array $params, string $name = 'direction'): string
93
    {
94 2
        if (isset($params[$name])) {
95 2
            return (string)$params[$name];
96
        }
97
98
        return 'desc';
99
    }
100
101
    /**
102
     * Sets the query param mapping
103
     *
104
     * @param array $map Query param map
105
     * @return \Phauthentic\Pagination\PaginationParamsFactoryInterface
0 ignored issues
show
Bug introduced by
The type Phauthentic\Pagination\P...nParamsFactoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
106
     */
107
    public function setQueryParamMapping(array $map = []): PaginationParamsFactoryInterface
108
    {
109
        $this->map = array_merge($this->map, $map);
110
111
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Phauthentic\Pagination\P...questQueryParamsFactory which is incompatible with the documented return type Phauthentic\Pagination\P...nParamsFactoryInterface.
Loading history...
112
    }
113
114 3
    protected function mapRequest(ServerRequestInterface $request)
115
    {
116 3
        $queryParams = $request->getQueryParams();
117
118 3
        $params = new static::$paginationParamsClass();
119
120 3
        foreach ($this->map as $setter => $value) {
121 3
            $setterMethod = 'set' . $setter;
122 3
            $getterMethod = 'get' . $setter;
123
124 3
            if (!is_string($value) && is_callable($value)) {
125
                $value = $value($request);
126
            }
127
128 3
            if (isset($queryParams[$value])) {
129 3
                $params->{$setterMethod}($this->{$getterMethod}($queryParams, $value));
130
            }
131
        }
132
133 3
        return $params;
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139 3
    public function build($data = null): PaginationParamsInterface
140
    {
141 3
        if (!$data instanceof ServerRequestInterface) {
142
            return new static::$paginationParamsClass();
143
        }
144
145 3
        return $this->mapRequest($data);
146
    }
147
}
148