Test Setup Failed
Push — testing ( 8dbf7d...8fdb90 )
by Roman
03:25
created

Collection::diff()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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
    public function setRowsInstance(Rows $rows)
29
    {
30
        $this->rows = $rows;
31
32
        return $this;
33 52
    }
34
35 52
    /**
36 51
     * Next page token
37
     *
38 52
     * @return mixed
39
     */
40 52
    public function getNextPageToken()
41 52
    {
42
        if ($this->rows === null) {
43
            return null;
44
        }
45
46
        return $this->rows->pagingStateToken();
47
    }
48 52
49
    /**
50 52
     * Last page indicator
51 45
     * @return bool
52
     */
53 45
    public function isLastPage()
54 42
    {
55 42
        if ($this->rows === null) {
56
            return true;
57 42
        }
58
59
        return $this->rows->isLastPage();
60
    }
61 45
62
    /**
63 52
     * Get next page
64
     *
65
     * @return Collection
66
     */
67
    public function nextPage()
68
    {
69
        if ($this->rows !== null && !$this->isLastPage()) {
70
            /** @var Model $instance */
71
            $model = $this->first();
72 2
73
            $nextPageRows = $this->rows->nextPage();
74 2
            $nextPageCollection = $model->newCassandraCollection($nextPageRows);
75
76
            return $nextPageCollection;
77
        }
78
79
        return new self;
80
    }
81 44
82
    /**
83 44
     * Get rows instance
84 2
     *
85
     * @return \Cassandra\Rows
86
     */
87 43
    public function getRows()
88
    {
89
        return $this->rows;
90
    }
91
92
    /**
93
     * Update current collection with results from
94
     * the next page
95 4
     *
96
     * @return Collection
97 4
     */
98 4
    public function appendNextPage()
99
    {
100 1
        $nextPage = $this->nextPage();
101
102
        if (!$nextPage->isEmpty()) {
103
            $this->items = array_merge($this->items, $nextPage->toArray());
104
            $this->rows = $nextPage->getRows();
105
        }
106
107 45
        return $this;
108
    }
109 45
110
    /**
111
     * Merge the collection with the given items.
112
     *
113
     * @param  \ArrayAccess|array  $items
114
     * @return static
115
     */
116
    public function merge($items)
117
    {
118 2
        $dictionary = $this->getDictionary();
119
120 2
        foreach ($items as $item) {
121
            $dictionary[(string) $item->getKey()] = $item;
122 2
        }
123 2
124 2
        return new static(array_values($dictionary));
125
    }
126
127 2
    /**
128
     * Reload a fresh model instance from the database for all the entities.
129
     *
130
     * @param  array|string  $with
131
     * @return static
132
     */
133
    public function fresh($with = [])
134
    {
135
        if ($this->isEmpty()) {
136
            return new static([]);
137
        }
138 7
139
        $model = $this->first();
140 7
141 1
        $freshModels = $model->newQueryWithoutScopes()
142
            ->whereIn($model->getKeyName(), $this->modelKeys())
143
            ->get()
144 7
            ->getDictionary();
145 3
146
        return $this->map(function ($model) use ($freshModels) {
147
            if ($model->exists && isset($freshModels[$model->getKey()])) {
148 7
                return $freshModels[$model->getKey()];
149 3
            } else {
150 1
                return null;
151
            }
152
        });
153 2
    }
154
}
155