Passed
Branch testing (559273)
by Roman
09:16
created

Collection::getNextPageToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace lroman242\LaravelCassandra;
4
5
use \Cassandra\Rows;
6
use lroman242\LaravelCassandra\Eloquent\Model;
7
use Illuminate\Support\Arr;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Database\Eloquent\Collection as BaseCollection;
10
11
class Collection extends BaseCollection
12
{
13
    /**
14
     * Cassandra rows instance
15
     *
16
     * @var \Cassandra\Rows
17
     */
18
    private $rows;
19
20
    /**
21
     * Items model
22
     *
23
     * @var Model
24
     */
25
    private $model;
26
27
    /**
28
     * Create a new collection.
29
     *
30
     * @param  mixed  $items
31
     * @param  Model  $model
32
     */
33 39
    public function __construct($items = [], Model $model = null)
34
    {
35 39
        if ($items instanceof Rows) {
0 ignored issues
show
Bug introduced by
The class Cassandra\Rows does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
36 39
            $this->rows = $items;
37
        }
38 39
        $this->model = $model;
39
40 39
        parent::__construct($this->prepareItems($items));
41 39
    }
42
43
    /**
44
     * Prepare items for collection
45
     *
46
     * @return array|Model[]
47
     */
48 39
    protected function prepareItems($items)
49
    {
50 39
        if ($this->model !== null && ($items instanceof Rows || is_array($items))) {
0 ignored issues
show
Bug introduced by
The class Cassandra\Rows does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
51 33
            $models = [];
52
53 33
            foreach ($items as $row) {
54 30
                $models[] = $this->model->newFromBuilder($row);
55
            }
56
57 33
            return $models;
58
        } else {
59 39
            return $items;
60
        }
61
    }
62
63
    /**
64
     * Next page token
65
     *
66
     * @return mixed
67
     */
68 2
    public function getNextPageToken()
69
    {
70 2
        return $this->rows->pagingStateToken();
71
    }
72
73
    /**
74
     * Last page indicator
75
     * @return bool
76
     */
77 35
    public function isLastPage()
78
    {
79 35
        if ($this->rows === null) {
80 2
            return true;
81
        }
82
83 34
        return $this->rows->isLastPage();
84
    }
85
86
    /**
87
     * Get next page
88
     *
89
     * @return Collection
90
     */
91 3
    public function nextPage()
92
    {
93 3
        if (!$this->isLastPage()) {
94 3
            return new self($this->rows->nextPage(), $this->model);
95
        }
96 1
    }
97
98
    /**
99
     * Get rows instance
100
     *
101
     * @return \Cassandra\Rows
102
     */
103 33
    public function getRows()
104
    {
105 33
        return $this->rows;
106
    }
107
108
    /**
109
     * Update current collection with results from
110
     * the next page
111
     *
112
     * @return Collection
113
     */
114 2
    public function appendNextPage()
115
    {
116 2
        $nextPage = $this->nextPage();
117
118 2
        if ($nextPage) {
119 2
            $this->items = array_merge($this->items, $nextPage->toArray());
120 2
            $this->rows = $nextPage->getRows();
121
        }
122
123 2
        return $this;
124
    }
125
126
    /**
127
     * Find a model in the collection by key.
128
     *
129
     * @param  mixed  $key
130
     * @param  mixed  $default
131
     *
132
     * @return \Illuminate\Database\Eloquent\Model|static
133
     */
134 7
    public function find($key, $default = null)
135
    {
136 7
        if ($key instanceof Model) {
137 1
            $key = $key->getKey();
138
        }
139
140 7
        if ($key instanceof Arrayable) {
141 3
            $key = $key->toArray();
142
        }
143
144 7
        if (is_array($key)) {
145 3
            if ($this->isEmpty()) {
146 1
                return new static([], $this->model);
147
            }
148
149 2
            return $this->whereIn($this->first()->getKeyName(), $key);
150
        }
151
152 4
        return Arr::first($this->items, function ($model) use ($key) {
153 4
            return $model->getKey() == $key;
154 4
        }, $default);
155
    }
156
157
    /**
158
     * Merge the collection with the given items.
159
     *
160
     * @param  \ArrayAccess|array  $items
161
     * @return static
162
     */
163
    public function merge($items)
164
    {
165
        $dictionary = $this->getDictionary();
166
167
        foreach ($items as $item) {
168
            $dictionary[(string) $item->getKey()] = $item;
169
        }
170
171
        return new static(array_values($dictionary), $this->model);
172
    }
173
174
    /**
175
     * Reload a fresh model instance from the database for all the entities.
176
     *
177
     * @param  array|string  $with
178
     * @return static
179
     */
180
    public function fresh($with = [])
181
    {
182
        if ($this->isEmpty()) {
183
            return new static([], $this->model);
184
        }
185
186
        $model = $this->first();
187
188
        $freshModels = $model->newQueryWithoutScopes()
189
            ->with(is_string($with) ? func_get_args() : $with)
190
            ->whereIn($model->getKeyName(), $this->modelKeys())
191
            ->get()
192
            ->getDictionary();
193
194
        return $this->map(function ($model) use ($freshModels) {
195
            return $model->exists && isset($freshModels[(string) $model->getKey()])
196
                ? $freshModels[(string) $model->getKey()] : null;
197
        });
198
    }
199
200
    /**
201
     * Diff the collection with the given items.
202
     *
203
     * @param  \ArrayAccess|array  $items
204
     * @return static
205
     */
206 View Code Duplication
    public function diff($items)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
    {
208
        $diff = new static([], $this->model);
209
210
        $dictionary = $this->getDictionary($items);
211
212
        foreach ($this->items as $item) {
213
            if (!isset($dictionary[(string) $item->getKey()])) {
214
                $diff->add($item);
215
            }
216
        }
217
218
        return $diff;
219
    }
220
221
    /**
222
     * Intersect the collection with the given items.
223
     *
224
     * @param  \ArrayAccess|array  $items
225
     * @return static
226
     */
227 View Code Duplication
    public function intersect($items)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
228
    {
229
        $intersect = new static([], $this->model);
230
231
        $dictionary = $this->getDictionary($items);
232
233
        foreach ($this->items as $item) {
234
            if (isset($dictionary[(string) $item->getKey()])) {
235
                $intersect->add($item);
236
            }
237
        }
238
239
        return $intersect;
240
    }
241
242
    /**
243
     * Return only unique items from the collection.
244
     *
245
     * @param  string|callable|null  $key
246
     * @param  bool  $strict
247
     * @return static|\Illuminate\Support\Collection
248
     */
249
    public function unique($key = null, $strict = false)
250
    {
251
        if (!is_null($key)) {
252
            return parent::unique($key, $strict);
253
        }
254
255
        return new static(array_values($this->getDictionary()), $this->model);
256
    }
257
258
    /**
259
     * Returns only the models from the collection with the specified keys.
260
     *
261
     * @param  mixed  $keys
262
     * @return static
263
     */
264
    public function only($keys)
265
    {
266
        if (is_null($keys)) {
267
            return new static($this->items, $this->model);
268
        }
269
270
        $dictionary = Arr::only($this->getDictionary(), $keys);
271
272
        return new static(array_values($dictionary), $this->model);
273
    }
274
275
    /**
276
     * Returns all models in the collection except the models with specified keys.
277
     *
278
     * @param  mixed  $keys
279
     * @return static
280
     */
281 1
    public function except($keys)
282
    {
283 1
        $dictionary = Arr::except($this->getDictionary(), $keys);
284
285 1
        return new static(array_values($dictionary), $this->model);
286
    }
287
288
289
    /**
290
     * Get a dictionary keyed by primary keys.
291
     *
292
     * @param  \ArrayAccess|array|null  $items
293
     * @return array
294
     */
295 2
    public function getDictionary($items = null)
296
    {
297 2
        $items = is_null($items) ? $this->items : $items;
298
299 2
        $dictionary = [];
300
301 2
        foreach ($items as $value) {
302 2
            $dictionary[(string) $value->getKey()] = $value;
303
        }
304
305 2
        return $dictionary;
306
    }
307
}
308