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

TotalItemsHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 8
c 1
b 0
f 0
dl 0
loc 27
ccs 8
cts 8
cp 1
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 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