Test Setup Failed
Push — master ( 5cf724...b9960b )
by Roman
14:09
created

Builder::insert()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace fuitad\LaravelCassandra\Query;
4
5
use fuitad\LaravelCassandra\Connection;
6
use Illuminate\Database\Query\Builder as BaseBuilder;
7
use Illuminate\Support\Arr;
8
9
class Builder extends BaseBuilder
10
{
11
    public $allowFiltering = false;
12
13
    /**
14
     * @inheritdoc
15
     */
16
    public function __construct(Connection $connection, Grammar $grammar = null, Processor $processor = null)
17
    {
18
        $this->connection = $connection;
19
        $this->grammar = $grammar ?: $connection->getQueryGrammar();
20
        $this->processor = $processor ?: $connection->getPostProcessor();
21
    }
22
23
    /**
24
     * Support "allow filtering"
25
     */
26
    public function allowFiltering($bool = true) {
27
        $this->allowFiltering = (bool) $bool;
28
29
        return $this;
30
    }
31
32
    /**
33
     * Insert a new record into the database.
34
     *
35
     * @param  array  $values
36
     * @return bool
37
     */
38
    public function insert(array $values)
39
    {
40
        // Since every insert gets treated like a batch insert, we will make sure the
41
        // bindings are structured in a way that is convenient when building these
42
        // inserts statements by verifying these elements are actually an array.
43
        if (empty($values)) {
44
            return true;
45
        }
46
47
        if (!is_array(reset($values))) {
48
            $values = [$values];
49
50
            return $this->connection->insert(
51
                $this->grammar->compileInsert($this, $values),
52
                $this->cleanBindings(Arr::flatten($values, 1))
53
            );
54
        }
55
56
        // Here, we'll generate the insert queries for every record and send those
57
        // for a batch query
58
        else {
59
            $queries = [];
60
            $bindings = [];
61
62
            foreach ($values as $key => $value) {
63
                ksort($value);
64
65
                $queries[] = $this->grammar->compileInsert($this, $value);
66
                $bindings[] = $this->cleanBindings(Arr::flatten($value, 1));
67
            }
68
69
            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...
70
        }
71
    }
72
    
73
    
74
    /**
75
     * Execute the query as a "select" statement.
76
     *
77
     * @param  array  $columns
78
     * @return \Illuminate\Support\Collection
79
     */
80
    public function get($columns = ['*'])
81
    {
82
        $original = $this->columns;
83
84
        if (is_null($original)) {
85
            $this->columns = $columns;
86
        }
87
88
        $results = $this->processor->processSelect($this, $this->runSelect());
89
90
        $collection = [];
91
        while (true) {
92
            $collection = array_merge($collection, collect($results)->toArray());
93
            if ($results->isLastPage()) {
94
                break;
95
            }
96
97
            $results = $results->nextPage();
98
        }
99
100
        $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...
101
102
        return collect($collection);
103
    }
104
105
106
}
107