Builder::setPaginationStateToken()   A
last analyzed

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\Query;
4
5
use lroman242\LaravelCassandra\Collection;
6
use lroman242\LaravelCassandra\Connection;
7
use Illuminate\Database\Query\Builder as BaseBuilder;
8
use Illuminate\Support\Arr;
9
10
class Builder extends BaseBuilder
11
{
12
    /**
13
     * Use cassandra filtering
14
     *
15
     * @var bool
16
     */
17
    public $allowFiltering = false;
18
19
    /**
20
     * Size of fetched page
21
     *
22
     * @var null|int
23
     */
24
    protected $pageSize = null;
25
26
    /**
27
     * Pagination state token
28
     *
29
     * @var null|string
30
     */
31
    protected $paginationStateToken = null;
32
33
    /**
34
     * Indicate what amount of pages should be fetched
35
     * all or single
36
     *
37
     * @var bool
38
     */
39
    protected $fetchAllResults = true;
40
41
    /**
42
     * @inheritdoc
43
     */
44 61
    public function __construct(Connection $connection, Grammar $grammar = null, Processor $processor = null)
45
    {
46 61
        $this->connection = $connection;
47 61
        $this->grammar = $grammar ?: $connection->getQueryGrammar();
48 61
        $this->processor = $processor ?: $connection->getPostProcessor();
49 61
    }
50
51
    /**
52
     * Support "allow filtering"
53
     */
54 3
    public function allowFiltering($bool = true) {
55 3
        $this->allowFiltering = (bool) $bool;
56
57 3
        return $this;
58
    }
59
60
    /**
61
     * Insert a new record into the database.
62
     *
63
     * @param  array  $values
64
     * @return bool
65
     */
66 54
    public function insert(array $values)
67
    {
68
        // Since every insert gets treated like a batch insert, we will make sure the
69
        // bindings are structured in a way that is convenient when building these
70
        // inserts statements by verifying these elements are actually an array.
71 54
        if (empty($values)) {
72
            return true;
73
        }
74
75 54
        if (!is_array(reset($values))) {
76 51
            $values = [$values];
77
78 51
            return $this->connection->insert(
79 51
                $this->grammar->compileInsert($this, $values),
80 51
                $this->cleanBindings(Arr::flatten($values, 1))
81
            );
82
        }
83
84
        // Here, we'll generate the insert queries for every record and send those
85
        // for a batch query
86
        else {
87 3
            $queries = [];
88 3
            $bindings = [];
89
90 3
            foreach ($values as $key => $value) {
91 3
                ksort($value);
92
93 3
                $queries[] = $this->grammar->compileInsert($this, $value);
94 3
                $bindings[] = $this->cleanBindings(Arr::flatten($value, 1));
95
            }
96
97 3
            return $this->connection->insertBulk($queries, $bindings);
0 ignored issues
show
Bug introduced by
The method insertBulk() does not exist on Illuminate\Database\ConnectionInterface. Did you maybe mean insert()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
98
        }
99
    }
100
    
101
    /**
102
     * Execute the query as a "select" statement.
103
     *
104
     * @param  array  $columns
105
     *
106
     * @return \Illuminate\Support\Collection
107
     */
108 56
    public function get($columns = ['*'])
109
    {
110 56
        $original = $this->columns;
111
112 56
        if (is_null($original)) {
113 56
            $this->columns = $columns;
114
        }
115
116
        //Set up custom options
117 56
        $options = [];
118 56
        if ($this->pageSize !== null && (int) $this->pageSize > 0) {
119 20
            $options['page_size'] = (int) $this->pageSize;
120
        }
121 56
        if ($this->paginationStateToken !== null) {
122 1
            $options['paging_state_token'] = $this->paginationStateToken;
123
        }
124
125
        // Process select with custom options
126
        /** @var \Cassandra\Rows $results */
127 56
        $results = $this->processor->processSelect($this, $this->runSelect($options));
128
129
        // Get results from all pages
130 56
        $collection = new Collection($results);
131
132 56
        if ($this->fetchAllResults) {
133 40
            while (!$results->isLastPage()) {
134 2
                $results = $results->nextPage();
135 2
                foreach ($results as $row) {
136 2
                    $collection->push($row);
137
                }
138
            }
139
        }
140 56
        $collection->setRowsInstance($results);
141
142 56
        $this->columns = $original;
0 ignored issues
show
Documentation Bug introduced by
It seems like $original can be null. However, the property $columns is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
143
144 56
        return $collection;
145
    }
146
147
    /**
148
     * Run the query as a "select" statement against the connection.
149
     *
150
     * @param array $options
151
     *
152
     * @return array
153
     */
154 56
    protected function runSelect(array $options = [])
155
    {
156 56
        return $this->connection->select(
157 56
            $this->toSql(), $this->getBindings(), !$this->useWritePdo, $options
0 ignored issues
show
Unused Code introduced by
The call to ConnectionInterface::select() has too many arguments starting with !$this->useWritePdo.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
158
        );
159
    }
160
161
    /**
162
     * Set pagination state token to fetch
163
     * next page
164
     *
165
     * @param string $token
166
     *
167
     * @return Builder
168
     */
169 1
    public function setPaginationStateToken($token = null)
170
    {
171 1
        $this->paginationStateToken = $token;
172
173 1
        return $this;
174
    }
175
176
    /**
177
     * Set page size
178
     *
179
     * @param int $pageSize
180
     *
181
     * @return Builder
182
     */
183 20
    public function setPageSize($pageSize = null)
184
    {
185 20
        if ($pageSize !== null) {
186 20
            $this->pageSize = (int) $pageSize;
187
        } else {
188 1
            $this->pageSize = $pageSize;
189
        }
190
191 20
        return $this;
192
    }
193
194
    /**
195
     * Get collection with single page results
196
     *
197
     * @param $columns array
198
     *
199
     * @return \Illuminate\Support\Collection
200
     */
201 19
    public function getPage($columns = ['*'])
202
    {
203 19
        $this->fetchAllResults = false;
204
205 19
        $result = $this->get($columns);
206
207 19
        $this->fetchAllResults = true;
208
209 19
        return $result;
210
    }
211
}
212