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

TotalItemsHandler::matches()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 3
nop 0
crap 3
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 12
    public function matches(): bool
22
    {
23 12
        return $this->config->items > 0
24 12
            && $this->config->pages === null
25 12
            && $this->config->perPageQuery === null;
26
    }
27
28
    /**
29
     * Handle the APIs configuration
30
     *
31
     * @return Traversable
32
     */
33 3
    public function handle(): Traversable
34
    {
35 3
        $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 3
        $pages = $perPage > 0 ? (int) ceil($this->config->items / $perPage) : 0;
37
38 3
        yield from $this->handleByTotalPages($pages);
39 3
    }
40
}
41