Completed
Push — master ( 89deac...cf3c32 )
by Bas
02:49
created

Builder::insert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query;
4
5
use Illuminate\Database\ConnectionInterface;
6
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Collection;
9
use InvalidArgumentException;
10
use LaravelFreelancerNL\Aranguent\Connection;
11
use LaravelFreelancerNL\Aranguent\Query\Grammar;
12
use LaravelFreelancerNL\Aranguent\Query\Processor;
13
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException;
14
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
15
16
class Builder extends IlluminateQueryBuilder
17
{
18
    /**
19
     * @var Grammar
20
     */
21
    public $grammar;
22
23
    /**
24
     * @var Connection
25
     */
26
    public $connection;
27
28
    /**
29
     * @var QueryBuilder
30
     */
31
    public $aqb;
32
33
    /**
34
     * Alias' are AQL variables
35
     * Sticking with the SQL based naming as this is the Laravel driver.
36
     * @var QueryBuilder
37
     */
38
    protected $aliasRegistry = [];
39
40
    /**
41
     * @override
42
     * Create a new query builder instance.
43
     *
44
     * @param ConnectionInterface $connection
45
     * @param Grammar $grammar
46
     * @param Processor $processor
47
     * @param QueryBuilder|null $aqb
48
     */
49
    public function __construct(ConnectionInterface $connection,
50
                                Grammar $grammar = null,
51
                                Processor $processor = null,
52
                                QueryBuilder $aqb = null)
53
    {
54
        $this->connection = $connection;
0 ignored issues
show
Documentation Bug introduced by
$connection is of type Illuminate\Database\ConnectionInterface, but the property $connection was declared to be of type LaravelFreelancerNL\Aranguent\Connection. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
55
        $this->grammar = $grammar ?: $connection->getQueryGrammar();
56
        $this->processor = $processor ?: $connection->getPostProcessor();
57
        if (!$aqb instanceof QueryBuilder) {
58
            $aqb = new QueryBuilder();
59
        }
60
        $this->aqb = $aqb;
61
    }
62
63
    /**
64
     * Run the query as a "select" statement against the connection.
65
     *
66
     * @return array
67
     */
68
    protected function runSelect()
69
    {
70
        $response = $this->connection->select($this->grammar->compileSelect($this)->aqb);
0 ignored issues
show
Bug introduced by
The property aqb does not exist on string.
Loading history...
71
        $this->aqb = new QueryBuilder();
72
        return $response;
73
    }
74
75
    /**
76
     * Get the SQL representation of the query.
77
     *
78
     * @return string
79
     */
80
    public function toSql()
81
    {
82
        return $this->grammar->compileSelect($this)->aqb->query;
0 ignored issues
show
Bug introduced by
The property aqb does not exist on string.
Loading history...
83
    }
84
85
    /**
86
     * Insert a new record into the database.
87
     * @param array $values
88
     * @return bool
89
     * @throws BindException
90
     */
91
    public function insert(array $values) : bool
92
    {
93
        $response = $this->getConnection()->insert($this->grammar->compileInsert($this, $values)->aqb);
0 ignored issues
show
Bug introduced by
The property aqb does not exist on string.
Loading history...
94
        $this->aqb = new QueryBuilder();
95
        return $response;
96
    }
97
98
    /**
99
     * Insert a new record and get the value of the primary key.
100
     *
101
     * @param array $values
102
     * @param string|null $sequence
103
     * @return int
104
     * @throws BindException
105
     */
106
    public function insertGetId(array $values, $sequence = null)
107
    {
108
        $response = $this->getConnection()->execute($this->grammar->compileInsertGetId($this, $values, $sequence)->aqb);
0 ignored issues
show
Bug introduced by
The property aqb does not exist on string.
Loading history...
Unused Code introduced by
The call to LaravelFreelancerNL\Aran...r::compileInsertGetId() has too many arguments starting with $sequence. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
        $response = $this->getConnection()->execute($this->grammar->/** @scrutinizer ignore-call */ compileInsertGetId($this, $values, $sequence)->aqb);

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. Please note the @ignore annotation hint above.

Loading history...
109
        $this->aqb = new QueryBuilder();
110
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type array which is incompatible with the documented return type integer.
Loading history...
111
    }
112
113
    /**
114
     * Execute the query as a "select" statement.
115
     *
116
     * @param  array|string  $columns
117
     * @return Collection
118
     */
119
    public function get($columns = ['*'])
120
    {
121
        $results = collect($this->onceWithColumns(Arr::wrap($columns), function () {
122
            return $this->runSelect();
123
        }));
124
        $this->aqb = new QueryBuilder();
125
        return $results;
126
    }
127
128
    /**
129
     * Update a record in the database.
130
     *
131
     * @param  array  $values
132
     * @return int
133
     */
134
    public function update(array $values)
135
    {
136
        $response =  $this->connection->update($this->grammar->compileUpdate($this, $values)->aqb);
0 ignored issues
show
Bug introduced by
The property aqb does not exist on string.
Loading history...
137
        $this->aqb = new QueryBuilder();
138
        return $response;
139
    }
140
141
    /**
142
     * Delete a record from the database.
143
     *
144
     * @param  mixed  $_key
145
     * @return int
146
     */
147
    public function delete($_key = null)
148
    {
149
        $response = $this->connection->delete($this->grammar->compileDelete($this, $_key)->aqb);
0 ignored issues
show
Bug introduced by
The property aqb does not exist on string.
Loading history...
150
        $this->aqb = new QueryBuilder();
151
        return $response;
152
    }
153
154
    public function registerAlias(string $table, string $alias) : void
155
    {
156
        $this->aliasRegistry[$table] = $alias;
157
    }
158
159
    public function getAlias(string $table) : string
160
    {
161
        return $this->aliasRegistry[$table];
162
    }
163
164
}
165