Passed
Push — main ( b3daef...4d79c5 )
by Jonas
03:48
created

PostgresGrammar   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 7
c 1
b 0
f 0
dl 0
loc 30
ccs 8
cts 8
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compileParentKeyOfFirstPathSegment() 0 7 1
A compileFirstPathSegment() 0 5 1
1
<?php
2
3
namespace Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Query\Grammars;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Database\Query\Grammars\PostgresGrammar as Base;
7
8
class PostgresGrammar extends Base implements EagerLimitGrammar
9
{
10
    /**
11
     * Compile an extraction of the first path segment.
12
     *
13
     * @param string $pathColumn
14
     * @return string
15
     */
16 10
    public function compileFirstPathSegment(string $pathColumn): string
17
    {
18 10
        $pathColumn = $this->wrap($pathColumn);
19
20 10
        return $pathColumn . '[1]';
21
    }
22
23
    /**
24
     * Compile an extraction of the first path segment's parent key.
25
     *
26
     * @param string $pathColumn
27
     * @param string $parentKeyColumn
28
     * @param \Illuminate\Database\Query\Builder $parentKeyQuery
29
     * @return string
30
     */
31 2
    public function compileParentKeyOfFirstPathSegment(string $pathColumn, string $parentKeyColumn, Builder $parentKeyQuery): string
32
    {
33 2
        $pathColumn = $this->wrap($pathColumn);
34 2
        $parentKeyColumn = $this->wrap($parentKeyColumn);
35 2
        $sql = $parentKeyQuery->toSql();
36
37 2
        return "case when array_length($pathColumn, 1) = 1 then $parentKeyColumn else ($sql) end";
38
    }
39
}
40