Passed
Pull Request — master (#38)
by Bas
17:19
created

CompilesJoins::compileCrossJoin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
4
5
use LaravelFreelancerNL\Aranguent\Query\Builder;
6
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
7
8
trait CompilesJoins
9
{
10
    /**
11
     * Compile the "join" portions of the query.
12
     *
13
     * @param Builder $builder
14
     * @param array   $joins
15
     *
16
     * @return string
17
     */
18
    protected function compileJoins(Builder $builder, $joins)
19
    {
20
        foreach ($joins as $join) {
21
            $compileMethod = 'compile' . ucfirst($join->type) . 'Join';
22
            $builder = $this->$compileMethod($builder, $join);
23
        }
24
25
        return $builder;
26
    }
27
28
    protected function compileInnerJoin(Builder $builder, $join)
29
    {
30
        $table = $join->table;
31
        $alias = $this->generateTableAlias($table);
0 ignored issues
show
Bug introduced by
It seems like generateTableAlias() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

31
        /** @scrutinizer ignore-call */ 
32
        $alias = $this->generateTableAlias($table);
Loading history...
32
        $this->registerTableAlias($table, $alias);
0 ignored issues
show
Bug introduced by
It seems like registerTableAlias() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

32
        $this->/** @scrutinizer ignore-call */ 
33
               registerTableAlias($table, $alias);
Loading history...
33
        $builder->aqb = $builder->aqb->for($alias, $table)
34
            ->filter($this->compileWheresToArray($join));
0 ignored issues
show
Bug introduced by
It seems like compileWheresToArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

34
            ->filter($this->/** @scrutinizer ignore-call */ compileWheresToArray($join));
Loading history...
35
36
        return $builder;
37
    }
38
39
    protected function compileLeftJoin(Builder $builder, $join)
40
    {
41
        $table = $join->table;
42
        $alias = $this->generateTableAlias($table);
43
        $this->registerTableAlias($table, $alias);
44
45
        $resultsToJoin = (new QueryBuilder())
46
            ->for($alias, $table)
47
            ->filter($this->compileWheresToArray($join))
48
            ->return($alias);
49
50
        $builder->aqb = $builder->aqb->let($table, $resultsToJoin)
51
            ->for(
52
                $alias,
53
                $builder->aqb->if(
54
                    [$builder->aqb->length($table), '>', 0],
55
                    $table,
56
                    '[]'
57
                )
58
            );
59
60
        return $builder;
61
    }
62
63
    protected function compileCrossJoin(Builder $builder, $join)
64
    {
65
        $table = $join->table;
66
        $alias = $this->generateTableAlias($table);
67
        $builder->aqb = $builder->aqb->for($alias, $table);
68
69
        return $builder;
70
    }
71
}
72