Completed
Push — master ( 3ea209...a94746 )
by Bas
04:04
created

Builder::aggregate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 9
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);
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;
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);
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
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
        return $results;
125
    }
126
127
    /**
128
     * Update a record in the database.
129
     *
130
     * @param  array  $values
131
     * @return int
132
     */
133
    public function update(array $values)
134
    {
135
        $response =  $this->connection->update($this->grammar->compileUpdate($this, $values)->aqb);
136
        $this->aqb = new QueryBuilder();
137
        return $response;
138
    }
139
140
    /**
141
     * Delete a record from the database.
142
     *
143
     * @param  mixed  $_key
144
     * @return int
145
     */
146
    public function delete($_key = null)
147
    {
148
        $response = $this->connection->delete($this->grammar->compileDelete($this, $_key)->aqb);
149
        $this->aqb = new QueryBuilder();
150
        return $response;
151
    }
152
153
    public function registerAlias(string $table, string $alias) : void
154
    {
155
        $this->aliasRegistry[$table] = $alias;
156
    }
157
158
    public function getAlias(string $table) : string
159
    {
160
        return $this->aliasRegistry[$table];
161
    }
162
163
    /**
164
     * Execute an aggregate function on the database.
165
     *
166
     * @param  string  $function
167
     * @param  array   $columns
168
     * @return mixed
169
     */
170
    public function aggregate($function, $columns = ['*'])
171
    {
172
        $results = $this->cloneWithout($this->unions ? [] : ['columns'])
173
            ->setAggregate($function, $columns)
174
            ->get($columns);
175
        if (! $results->isEmpty()) {
176
            return $results[0];
177
        }
178
        return false;
179
    }
180
181
    /**
182
     * Put the query's results in random order.
183
     *
184
     * @param  string  $seed
185
     * @return $this
186
     */
187
    public function inRandomOrder($seed = '')
188
    {
189
        return $this->orderByRaw($this->grammar->compileRandom($this));
190
    }
191
192
}
193