1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace lroman242\LaravelCassandra; |
4
|
|
|
|
5
|
|
|
use \Cassandra\Rows; |
6
|
|
|
use lroman242\LaravelCassandra\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Collection as BaseCollection; |
8
|
|
|
|
9
|
|
|
class Collection extends BaseCollection |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Cassandra rows instance |
13
|
|
|
* |
14
|
|
|
* @var \Cassandra\Rows |
15
|
|
|
*/ |
16
|
|
|
private $rows; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Set Cassandra rows instance related to the |
20
|
|
|
* collection items. |
21
|
|
|
* |
22
|
|
|
* Required for fetching next pages |
23
|
|
|
* |
24
|
|
|
* @param Rows $rows |
25
|
|
|
* |
26
|
|
|
* @return $this |
27
|
|
|
*/ |
28
|
56 |
|
public function setRowsInstance(Rows $rows) |
29
|
|
|
{ |
30
|
56 |
|
$this->rows = $rows; |
31
|
|
|
|
32
|
56 |
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Next page token |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
3 |
|
public function getNextPageToken() |
41
|
|
|
{ |
42
|
3 |
|
if ($this->rows === null) { |
43
|
1 |
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
return $this->rows->pagingStateToken(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Last page indicator |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
6 |
|
public function isLastPage() |
54
|
|
|
{ |
55
|
6 |
|
if ($this->rows === null) { |
56
|
2 |
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
return $this->rows->isLastPage(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get next page |
64
|
|
|
* |
65
|
|
|
* @return Collection |
66
|
|
|
*/ |
67
|
5 |
|
public function nextPage() |
68
|
|
|
{ |
69
|
5 |
|
if ($this->rows !== null && !$this->isLastPage()) { |
70
|
|
|
/** @var Model $instance */ |
71
|
3 |
|
$model = $this->first(); |
72
|
|
|
|
73
|
3 |
|
$nextPageRows = $nextPageItems = $this->rows->nextPage(); |
|
|
|
|
74
|
3 |
|
$nextPageCollection = $model->newCassandraCollection($nextPageRows); |
75
|
|
|
|
76
|
3 |
|
return $nextPageCollection; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
return new self; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get rows instance |
84
|
|
|
* |
85
|
|
|
* @return \Cassandra\Rows |
86
|
|
|
*/ |
87
|
50 |
|
public function getRows() |
88
|
|
|
{ |
89
|
50 |
|
return $this->rows; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Update current collection with results from |
94
|
|
|
* the next page |
95
|
|
|
* |
96
|
|
|
* @return Collection |
97
|
|
|
*/ |
98
|
2 |
|
public function appendNextPage() |
99
|
|
|
{ |
100
|
2 |
|
$nextPage = $this->nextPage(); |
101
|
|
|
|
102
|
2 |
|
if (!$nextPage->isEmpty()) { |
103
|
1 |
|
$this->items = array_merge($this->items, $nextPage->toArray()); |
104
|
1 |
|
$this->rows = $nextPage->getRows(); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Merge the collection with the given items. |
112
|
|
|
* |
113
|
|
|
* @param \ArrayAccess|array $items |
114
|
|
|
* @return static |
115
|
|
|
*/ |
116
|
2 |
|
public function merge($items) |
117
|
|
|
{ |
118
|
2 |
|
$dictionary = $this->getDictionary(); |
119
|
|
|
|
120
|
2 |
|
foreach ($items as $item) { |
121
|
2 |
|
$dictionary[(string) $item->getKey()] = $item; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
return new static(array_values($dictionary)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Reload a fresh model instance from the database for all the entities. |
129
|
|
|
* |
130
|
|
|
* @param array|string $with |
131
|
|
|
* @return static |
132
|
|
|
*/ |
133
|
4 |
|
public function fresh($with = []) |
134
|
|
|
{ |
135
|
4 |
|
if ($this->isEmpty()) { |
136
|
1 |
|
return new static([]); |
137
|
|
|
} |
138
|
|
|
|
139
|
3 |
|
$model = $this->first(); |
140
|
|
|
|
141
|
3 |
|
$freshModels = $model->newQueryWithoutScopes() |
142
|
3 |
|
->whereIn($model->getKeyName(), $this->modelKeys()) |
143
|
3 |
|
->get() |
144
|
3 |
|
->getDictionary(); |
145
|
|
|
|
146
|
3 |
|
return $this->map(function ($model) use ($freshModels) { |
147
|
3 |
|
if ($model->exists && isset($freshModels[(string) $model->getKey()])) { |
148
|
2 |
|
return $freshModels[(string) $model->getKey()]; |
149
|
|
|
} else { |
150
|
2 |
|
return null; |
151
|
|
|
} |
152
|
3 |
|
}); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Diff the collection with the given items. |
157
|
|
|
* |
158
|
|
|
* @param \ArrayAccess|array $items |
159
|
|
|
* @return static |
160
|
|
|
*/ |
161
|
1 |
View Code Duplication |
public function diff($items) |
|
|
|
|
162
|
|
|
{ |
163
|
1 |
|
$diff = new static; |
164
|
|
|
|
165
|
1 |
|
$dictionary = $this->getDictionary($items); |
166
|
|
|
|
167
|
1 |
|
foreach ($this->items as $item) { |
168
|
1 |
|
if (!isset($dictionary[(string) $item->getKey()])) { |
169
|
1 |
|
$diff->add($item); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
return $diff; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Intersect the collection with the given items. |
178
|
|
|
* |
179
|
|
|
* @param \ArrayAccess|array $items |
180
|
|
|
* @return static |
181
|
|
|
*/ |
182
|
1 |
View Code Duplication |
public function intersect($items) |
|
|
|
|
183
|
|
|
{ |
184
|
1 |
|
$intersect = new static; |
185
|
|
|
|
186
|
1 |
|
$dictionary = $this->getDictionary($items); |
187
|
|
|
|
188
|
1 |
|
foreach ($this->items as $item) { |
189
|
1 |
|
if (isset($dictionary[(string) $item->getKey()])) { |
190
|
1 |
|
$intersect->add($item); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
return $intersect; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get a dictionary keyed by primary keys. |
199
|
|
|
* |
200
|
|
|
* @param \ArrayAccess|array|null $items |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
11 |
|
public function getDictionary($items = null) |
204
|
|
|
{ |
205
|
11 |
|
$items = is_null($items) ? $this->items : $items; |
206
|
|
|
|
207
|
11 |
|
$dictionary = []; |
208
|
|
|
|
209
|
11 |
|
foreach ($items as $value) { |
210
|
11 |
|
$dictionary[(string) $value->getKey()] = $value; |
211
|
|
|
} |
212
|
|
|
|
213
|
11 |
|
return $dictionary; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.