Issues (115)

src/Services/PaginationService.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace VGirol\JsonApi\Services;
4
5
use Illuminate\Support\Collection;
6
use VGirol\JsonApiConstant\Members;
7
8
class PaginationService extends AbstractService
9
{
10
    /**
11
     * Undocumented variable
12
     *
13
     * @var integer
14
     */
15
    protected $totalItem;
16
17
    /**
18
     * Undocumented variable
19
     *
20
     * @var Collection
21
     */
22
    protected $options;
23
24
    protected function getConfigKey(): string
25
    {
26
        return 'pagination';
27
    }
28
29
    protected function parseParameters($request)
30
    {
31
        return Collection::make($request->query(config('json-api-paginate.pagination_parameter'), null));
32
    }
33
34
    public function queryIsValid(): bool
35
    {
36
        return true;
37
    }
38
39
    public function parseRequest($request = null, $force = false)
40
    {
41
        parent::parseRequest($request, $force);
42
43
        return $this->fillOptions();
44
    }
45
46
    /**
47
     * Undocumented function
48
     *
49
     * @return static
50
     */
51
    protected function fillOptions()
52
    {
53
        $number_parameter = config('json-api-paginate.number_parameter');
54
        $size_parameter = config('json-api-paginate.size_parameter');
55
56
        $page = $this->value($number_parameter) ?: 1;
57
        $itemPerPage = $this->value($size_parameter) ?: config('json-api-paginate.max_results');
58
        $pageCount = intdiv($this->totalItem, $itemPerPage);
59
        if ($this->totalItem % $itemPerPage != 0) {
60
            $pageCount++;
61
        }
62
        if ($pageCount == 0) {
63
            $pageCount = 1;
64
        }
65
66
        $this->options = Collection::make([
67
            'number_parameter' => $number_parameter,
68
            'size_parameter' => $size_parameter,
69
            'total_items' => $this->totalItem,
70
            'item_per_page' => $itemPerPage,
71
            'page_count' => $pageCount,
72
            'page' => $page
73
        ]);
74
75
        return $this;
76
    }
77
78
    /**
79
     * Undocumented function
80
     *
81
     * @param integer $total
82
     *
83
     * @return void
84
     */
85
    public function setTotalItem(int $total)
86
    {
87
        $this->totalItem = $total;
88
    }
89
90
    /**
91
     * Undocumented function
92
     *
93
     * @return array
94
     */
95
    public function getPaginationMeta(): array
96
    {
97
        return $this->options->only(['total_items', 'item_per_page', 'page_count', 'page'])->toArray();
98
    }
99
100
    /**
101
     * Undocumented function
102
     *
103
     * @return array
104
     */
105
    public function getPaginationLinks(): array
106
    {
107
        $route = request()->route();
0 ignored issues
show
The method route() does not exist on Illuminate\Http\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        $route = request()->/** @scrutinizer ignore-call */ route();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        $selfRouteName = $route->getName();
109
        $page_parameter = config('json-api-paginate.pagination_parameter');
110
111
        // Set document "links" member of json response
112
        $links = [
113
            Members::LINK_PAGINATION_FIRST => [
114
                'route' => $selfRouteName,
115
                'query' => [
116
                    "{$page_parameter}[{$this->options['number_parameter']}]" => 1,
117
                    "{$page_parameter}[{$this->options['size_parameter']}]" => $this->options['item_per_page']
118
                ]
119
            ],
120
            Members::LINK_PAGINATION_LAST => [
121
                'route' => $selfRouteName,
122
                'query' => [
123
                    "{$page_parameter}[{$this->options['number_parameter']}]" => $this->options['page_count'],
124
                    "{$page_parameter}[{$this->options['size_parameter']}]" => $this->options['item_per_page']
125
                ]
126
            ],
127
            Members::LINK_PAGINATION_PREV => null,
128
            Members::LINK_PAGINATION_NEXT => null
129
        ];
130
        if ($this->options['total_items'] > $this->options['item_per_page']) {
131
            if ($this->options['page'] > 1) {
132
                $links[Members::LINK_PAGINATION_PREV] = [
133
                    'route' => $selfRouteName,
134
                    'query' => [
135
                        "{$page_parameter}[{$this->options['number_parameter']}]" => $this->options['page'] - 1,
136
                        "{$page_parameter}[{$this->options['size_parameter']}]" => $this->options['item_per_page']
137
                    ]
138
                ];
139
            }
140
            if ($this->options['page'] < $this->options['page_count']) {
141
                $links[Members::LINK_PAGINATION_NEXT] = [
142
                    'route' => $selfRouteName,
143
                    'query' => [
144
                        "{$page_parameter}[{$this->options['number_parameter']}]" => $this->options['page'] + 1,
145
                        "{$page_parameter}[{$this->options['size_parameter']}]" => $this->options['item_per_page']
146
                    ]
147
                ];
148
            }
149
        }
150
151
        foreach ($links as $name => $params) {
152
            if (is_null($params)) {
153
                $url = null;
154
            } else {
155
                $url = route(
156
                    $params['route'],
157
                    array_merge(
158
                        jsonapiFields()->getQueryParameter(),
159
                        jsonapiFilter()->getQueryParameter(),
160
                        jsonapiInclude()->getQueryParameter(),
161
                        jsonapiSort()->getQueryParameter(),
162
                        $params['query'],
163
                        $route->parameters
164
                    )
165
                );
166
            }
167
            $links[$name] = $url;
168
        }
169
170
        return $links;
171
    }
172
}
173