Builder::aggregate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Encima\Albero\Extensions\Query;
4
5
use Illuminate\Database\Query\Builder as BaseBuilder;
6
7
class Builder extends BaseBuilder
8
{
9
    /**
10
     * Replace the "order by" clause of the current query.
11
     *
12
     * @param  string  $column
13
     * @param  string  $direction
14
     * @return \Illuminate\Database\Query\Builder|static
15
     */
16
    public function reOrderBy(?string $column, string $direction = 'asc'): self
17
    {
18
        $this->orders = null;
19
20
        if (!is_null($column)) {
21
            return $this->orderBy($column, $direction);
22
        }
23
24
        return $this;
25
    }
26
27
    /**
28
     * Execute an aggregate function on the database.
29
     *
30
     * @param  string  $function
31
     * @param  array   $columns
32
     * @return mixed
33
     */
34
    public function aggregate($function, $columns = ['*'])
35
    {
36
        // Postgres doesn't like ORDER BY when there's no GROUP BY clause
37
        if (!isset($this->groups)) {
38
            $this->reOrderBy(null);
39
        }
40
41
        return parent::aggregate($function, $columns);
42
    }
43
}
44