Test Setup Failed
Pull Request — master (#1)
by Florian
10:46
created

ServerRequestQueryParamsFactory::getLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
     * Parameter map
26
     *
27
     * @var array
28
     */
29
    protected $map = [
30
        'limit' => 'limit',
31
        'page' => 'page',
32
        'direction' => 'direction',
33
        'sortBy' => 'sort'
34
    ];
35
36
    /**
37
     * Gets the page number value from the server request params
38
     *
39
     * @param array $params Server request query params
40
     * @param string name of the query param
0 ignored issues
show
Bug introduced by
The type Phauthentic\Pagination\ParamsFactory\name 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...
41
     * @return int
42
     */
43
    public function getPage(array $params, string $name = 'page'): int
44
    {
45
        if (isset($params[$name])) {
46
            return (int)$params[$name];
47
        }
48
49
        return 1;
50
    }
51
52
    /**
53
     * Gets the limit value from the server request params
54
     *
55
     * @param array $params Server request query params
56
     * @param string name of the query param
57
     * @return int
58
     */
59
    public function getLimit(array $params, string $name = 'limit'): int
60
    {
61
        if (isset($params[$name])) {
62
            return (int)$params['limit'];
63
        }
64
65
        return 20;
66
    }
67
68
    /**
69
     * Gets the sort value from the server request params
70
     *
71
     * @param array $params Server request query params
72
     * @param string name of the query param
73
     * @return string|null
74
     */
75
    public function getSortBy(array $params, string $name = 'sort'): ?string
76
    {
77
        if (!empty($params[$name])) {
78
            return (string)$params[$name];
79
        }
80
81
        return null;
82
    }
83
84
    /**
85
     * Gets the direction value from the server request params
86
     *
87
     * @param array $params Server request query params
88
     * @param string name of the query param
89
     * @return string Must be desc `desc`or `asc`
90
     */
91
    public function getDirection(array $params, string $name = 'direction'): string
92
    {
93
        if (isset($params[$name])) {
94
            return (string)$params[$name];
95
        }
96
97
        return 'desc';
98
    }
99
100
    /**
101
     * Sets the query param mapping
102
     *
103
     * @param array $map Query param map
104
     * @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...
105
     */
106
    public function setQueryParamMapping(array $map = []): PaginationParamsFactoryInterface
107
    {
108
        $this->map = array_merge($this->map, $map);
109
110
        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...
111
    }
112
113
    protected function mapRequest(ServerRequestInterface $request)
114
    {
115
        $queryParams = $request->getQueryParams();
116
117
        $params = new static::$paginationParamsClass();
118
119
        foreach ($this->map as $setter => $value) {
120
            $setterMethod = 'set' . $setter;
121
            $getterMethod = 'get' . $setter;
122
123
            if (!is_string($value) && is_callable($value)) {
124
                $value = $value($request);
125
            }
126
127
            if (isset($queryParams[$value])) {
128
                $params->{$setterMethod}($this->{$getterMethod}($queryParams, $value));
129
            }
130
        }
131
132
        return $params;
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    public function build($data): PaginationParamsInterface
139
    {
140
        if (!$data instanceof ServerRequestInterface) {
141
           return new static::$paginationParamsClass();
142
        }
143
144
        return $this->mapRequest($data);
145
    }
146
}
147