Passed
Pull Request — master (#17)
by
unknown
02:00
created

OffsetDataReader::withOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\DataReader;
4
5
use Countable;
6
use Cycle\ORM\Select;
7
use InvalidArgumentException;
8
use Spiral\Database\Query\QueryInterface;
9
use Spiral\Pagination\PaginableInterface;
10
use Yiisoft\Data\Reader\CountableDataInterface;
11
use Yiisoft\Data\Reader\DataReaderInterface;
12
use Yiisoft\Data\Reader\OffsetableDataInterface;
13
use Yiisoft\Yii\Cycle\DataReader\Cache\CachedCount;
14
use Yiisoft\Yii\Cycle\DataReader\Cache\CachedCollection;
15
16
final class OffsetDataReader implements DataReaderInterface, OffsetableDataInterface, CountableDataInterface
17
{
18
    /** @var QueryInterface|Select */
19
    private $query;
20
    private ?int $limit = null;
21
    private ?int $offset = null;
22
    private CachedCount $countCache;
23
    private CachedCollection $itemsCache;
24
25
    /**
26
     * @param Select|QueryInterface $query
27
     */
28
    public function __construct($query)
29
    {
30
        if (!$query instanceof Countable) {
31
            throw new InvalidArgumentException(sprintf('Query should implement %s interface', Countable::class));
32
        }
33
        if (!$query instanceof PaginableInterface) {
0 ignored issues
show
introduced by
$query is always a sub-type of Spiral\Pagination\PaginableInterface.
Loading history...
34
            throw new InvalidArgumentException(
35
                sprintf('Query should implement %s interface', PaginableInterface::class)
36
            );
37
        }
38
        $this->query = clone $query;
39
        $this->countCache = new CachedCount($this->query);
40
        $this->itemsCache = new CachedCollection();
41
    }
42
    public function withLimit(int $limit): self
43
    {
44
        $clone = clone $this;
45
        $clone->setLimit($limit);
46
        return $clone;
47
    }
48
    public function withOffset(int $offset): self
49
    {
50
        $clone = clone $this;
51
        $clone->setOffset($offset);
52
        return $clone;
53
    }
54
    public function count(): int
55
    {
56
        return $this->countCache->getCount();
57
    }
58
    public function read(): iterable
59
    {
60
        if ($this->itemsCache->getCollection() !== null) {
61
            return $this->itemsCache->getCollection();
62
        }
63
        $newQuery = clone $this->query;
64
        if ($this->offset !== null) {
65
            $newQuery->offset($this->offset);
0 ignored issues
show
Bug introduced by
The method offset() does not exist on Spiral\Database\Query\QueryInterface. It seems like you code against a sub-type of Spiral\Database\Query\QueryInterface such as Spiral\Database\Query\SelectQuery. ( Ignorable by Annotation )

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

65
            $newQuery->/** @scrutinizer ignore-call */ 
66
                       offset($this->offset);
Loading history...
66
        }
67
        if ($this->limit !== null) {
68
            $newQuery->limit($this->limit);
0 ignored issues
show
Bug introduced by
The method limit() does not exist on Spiral\Database\Query\QueryInterface. It seems like you code against a sub-type of Spiral\Database\Query\QueryInterface such as Spiral\Database\Query\SelectQuery. ( Ignorable by Annotation )

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

68
            $newQuery->/** @scrutinizer ignore-call */ 
69
                       limit($this->limit);
Loading history...
69
        }
70
        $this->itemsCache->setCollection($newQuery->fetchAll());
0 ignored issues
show
Bug introduced by
The method fetchAll() does not exist on Spiral\Database\Query\QueryInterface. It seems like you code against a sub-type of Spiral\Database\Query\QueryInterface such as Spiral\Database\Query\SelectQuery. ( Ignorable by Annotation )

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

70
        $this->itemsCache->setCollection($newQuery->/** @scrutinizer ignore-call */ fetchAll());
Loading history...
71
        return $this->itemsCache->getCollection();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->itemsCache->getCollection() returns the type null which is incompatible with the type-hinted return iterable.
Loading history...
Bug introduced by
Are you sure the usage of $this->itemsCache->getCollection() targeting Yiisoft\Yii\Cycle\DataRe...ection::getCollection() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
72
    }
73
74
    private function setLimit(?int $limit): void
75
    {
76
        if ($this->limit !== $limit) {
77
            $this->limit = $limit;
78
            $this->itemsCache = new CachedCollection();
79
        }
80
    }
81
    private function setOffset(?int $offset): void
82
    {
83
        if ($this->offset !== $offset) {
84
            $this->offset = $offset;
85
            $this->itemsCache = new CachedCollection();
86
        }
87
    }
88
}
89