Completed
Branch testing (8fdb90)
by Roman
10:31
created

Collection::setRowsInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 = $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[$model->getKey()])) {
148 2
                return $freshModels[$model->getKey()];
149
            } else {
150 2
                return null;
151
            }
152 3
        });
153
    }
154
}
155