Passed
Push — master ( 342193...f80fe6 )
by Andrea Marco
03:14 queued 11s
created

ItemsPerPageHandler::countItems()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 10
cc 4
nc 4
nop 0
crap 4
1
<?php
2
3
namespace Cerbero\LazyJsonPages\Handlers;
4
5
use Cerbero\LazyJsonPages\Concerns\HandlesTotalPages;
6
use GuzzleHttp\Psr7\Uri;
7
use Traversable;
8
9
/**
10
 * The items per page handler.
11
 *
12
 */
13
class ItemsPerPageHandler extends AbstractHandler
14
{
15
    use HandlesTotalPages;
0 ignored issues
show
introduced by
The trait Cerbero\LazyJsonPages\Concerns\HandlesTotalPages requires some properties which are not provided by Cerbero\LazyJsonPages\Handlers\ItemsPerPageHandler: $chunk, $source, $request, $path, $firstPage, $concurrency, $pageName, $timeout
Loading history...
16
17
    /**
18
     * Determine whether the handler can handle the APIs configuration
19
     *
20
     * @return bool
21
     */
22 9
    public function matches(): bool
23
    {
24 9
        return $this->config->perPageQuery
25 9
            && $this->config->nextPageKey === null
26 9
            && ($this->config->items || $this->config->pages || $this->config->lastPage);
27
    }
28
29
    /**
30
     * Handle the APIs configuration
31
     *
32
     * @return Traversable
33
     */
34 4
    public function handle(): Traversable
35
    {
36 4
        $originalUri = $this->config->source->request->getUri();
37 4
        $pages = (int) ceil($this->countItems() / $this->config->perPageOverride);
38 4
        $uri = Uri::withQueryValue($originalUri, $this->config->perPageQuery, $this->config->perPageOverride);
39
40 4
        yield from $this->handleByTotalPages($pages, $uri);
41 4
    }
42
43
    /**
44
     * Retrieve the total number of items
45
     *
46
     * @return int
47
     */
48 4
    protected function countItems(): int
49
    {
50 4
        if ($this->config->items) {
51 1
            return $this->config->items;
52 3
        } elseif ($this->config->pages) {
53 1
            return $this->config->pages * $this->config->perPage;
54
        }
55
56 2
        $pages = $this->config->firstPage == 0 ? $this->config->lastPage + 1 : $this->config->lastPage;
57
58 2
        return $pages * $this->config->perPage;
59
    }
60
}
61