Passed
Branch feature/first-release (57b0a8)
by Andrea Marco
13:40
created

TotalItemsHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 8
c 1
b 0
f 0
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A matches() 0 5 3
A handle() 0 6 2
1
<?php
2
3
namespace Cerbero\LazyJsonPages\Handlers;
4
5
use Cerbero\LazyJsonPages\Concerns\HandlesTotalPages;
6
use Traversable;
7
8
/**
9
 * The total items handler.
10
 *
11
 */
12
class TotalItemsHandler extends AbstractHandler
13
{
14
    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\TotalItemsHandler: $chunk, $source, $request, $path, $firstPage, $concurrency, $pageName, $timeout
Loading history...
15
16
    /**
17
     * Determine whether the handler can handle the APIs configuration
18
     *
19
     * @return bool
20
     */
21
    public function matches(): bool
22
    {
23
        return $this->config->items > 0
24
            && $this->config->pages === null
25
            && $this->config->perPageQuery === null;
26
    }
27
28
    /**
29
     * Handle the APIs configuration
30
     *
31
     * @return Traversable
32
     */
33
    public function handle(): Traversable
34
    {
35
        $perPage = $this->config->perPage ?? count($this->config->source->json($this->config->path));
0 ignored issues
show
Bug introduced by
It seems like $this->config->source->json($this->config->path) can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

35
        $perPage = $this->config->perPage ?? count(/** @scrutinizer ignore-type */ $this->config->source->json($this->config->path));
Loading history...
36
        $pages = $perPage > 0 ? (int) ceil($this->config->items / $perPage) : 0;
37
38
        yield from $this->handleByTotalPages($pages);
39
    }
40
}
41