Passed
Pull Request — master (#17)
by
unknown
01:40
created

SelectDataReader::withSort()   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 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\DataReader;
6
7
use Countable;
8
use Cycle\ORM\Select;
9
use InvalidArgumentException;
10
use Spiral\Database\Query\SelectQuery;
11
use Spiral\Pagination\PaginableInterface;
12
use Yiisoft\Data\Reader\CountableDataInterface;
13
use Yiisoft\Data\Reader\DataReaderInterface;
14
use Yiisoft\Data\Reader\OffsetableDataInterface;
15
use Yiisoft\Data\Reader\Sort;
16
use Yiisoft\Data\Reader\SortableDataInterface;
17
use Yiisoft\Yii\Cycle\DataReader\Cache\CachedCount;
18
use Yiisoft\Yii\Cycle\DataReader\Cache\CachedCollection;
19
20
final class SelectDataReader implements
21
    DataReaderInterface,
22
    OffsetableDataInterface,
23
    CountableDataInterface,
24
    SortableDataInterface
25
{
26
    /** @var Select|SelectQuery */
27
    private $query;
28
    private ?int $limit = null;
29
    private ?int $offset = null;
30
    private ?Sort $sorting = null;
31
    private CachedCount $countCache;
32
    private CachedCollection $itemsCache;
33
34
    /**
35
     * @param Select|SelectQuery $query
36
     */
37
    public function __construct($query)
38
    {
39
        if (!$query instanceof Countable) {
0 ignored issues
show
introduced by
$query is always a sub-type of Countable.
Loading history...
40
            throw new InvalidArgumentException(sprintf('Query should implement %s interface', Countable::class));
41
        }
42
        if (!$query instanceof PaginableInterface) {
0 ignored issues
show
introduced by
$query is always a sub-type of Spiral\Pagination\PaginableInterface.
Loading history...
43
            throw new InvalidArgumentException(
44
                sprintf('Query should implement %s interface', PaginableInterface::class)
45
            );
46
        }
47
        $this->query = clone $query;
48
        $this->countCache = new CachedCount($this->query);
49
        $this->itemsCache = new CachedCollection();
50
    }
51
52
    public function getSort(): ?Sort
53
    {
54
        return $this->sorting;
55
    }
56
57
    public function withLimit(int $limit): self
58
    {
59
        $clone = clone $this;
60
        $clone->setLimit($limit);
61
        return $clone;
62
    }
63
64
    public function withOffset(int $offset): self
65
    {
66
        $clone = clone $this;
67
        $clone->setOffset($offset);
68
        return $clone;
69
    }
70
71
    public function withSort(?Sort $sorting): self
72
    {
73
        $clone = clone $this;
74
        $clone->setSort($sorting);
75
        return $clone;
76
    }
77
78
    public function count(): int
79
    {
80
        return $this->countCache->getCount();
81
    }
82
83
    public function read(): iterable
84
    {
85
        if ($this->itemsCache->getCollection() !== null) {
86
            return $this->itemsCache->getCollection();
87
        }
88
        $newQuery = clone $this->query;
89
        if ($this->offset !== null) {
90
            $newQuery->offset($this->offset);
91
        }
92
        if ($this->sorting !== null) {
93
            $newQuery->orderBy($this->sorting->getOrder());
94
        }
95
        if ($this->limit !== null) {
96
            $newQuery->limit($this->limit);
97
        }
98
        $this->itemsCache->setCollection($newQuery->fetchAll());
99
        return $this->itemsCache->getCollection();
0 ignored issues
show
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...
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...
100
    }
101
102
    private function setSort(?Sort $sorting): void
103
    {
104
        if ($this->sorting !== $sorting) {
105
            $this->sorting = $sorting;
106
            $this->itemsCache = new CachedCollection();
107
        }
108
    }
109
110
    private function setLimit(?int $limit): void
111
    {
112
        if ($this->limit !== $limit) {
113
            $this->limit = $limit;
114
            $this->itemsCache = new CachedCollection();
115
        }
116
    }
117
118
    private function setOffset(?int $offset): void
119
    {
120
        if ($this->offset !== $offset) {
121
            $this->offset = $offset;
122
            $this->itemsCache = new CachedCollection();
123
        }
124
    }
125
}
126