Passed
Push — master ( 6ed66a...c3d33f )
by Jonas
06:02
created

Builder::getExpressionGrammar()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 0
cp 0
rs 9.6111
cc 5
nc 5
nop 0
crap 30
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder as Base;
6
use Illuminate\Database\PostgresConnection;
7
use RuntimeException;
8
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\MySqlGrammar;
9
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\PostgresGrammar;
10
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SQLiteGrammar;
11
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SqlServerGrammar;
12
13
class Builder extends Base
14
{
15
    /**
16 196
     * Get the hydrated models without eager loading.
17
     *
18 196
     * @param array $columns
19
     * @return \Illuminate\Database\Eloquent\Model[]
20 196
     */
21 51
    public function getModels($columns = ['*'])
22 51
    {
23
        $items = $this->query->get($columns)->all();
24 51
25 26
        if ($this->getConnection() instanceof PostgresConnection) {
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Staudenmeir\LaravelAdjacencyList\Eloquent\Builder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

25
        if ($this->/** @scrutinizer ignore-call */ getConnection() instanceof PostgresConnection) {
Loading history...
26 26
            $path = $this->model->getPathName();
27
28
            if (isset($items[0]->$path)) {
29
                $this->replacePathSeparator(
30
                    $items,
31 196
                    $path,
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $path of Staudenmeir\LaravelAdjac...:replacePathSeparator() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

31
                    /** @scrutinizer ignore-type */ $path,
Loading history...
32
                    $this->model->getPathSeparator()
0 ignored issues
show
Bug introduced by
It seems like $this->model->getPathSeparator() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $separator of Staudenmeir\LaravelAdjac...:replacePathSeparator() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

32
                    /** @scrutinizer ignore-type */ $this->model->getPathSeparator()
Loading history...
33 196
                );
34
35 196
                foreach ($this->model->getCustomPaths() as $path) {
36
                    $this->replacePathSeparator(
37
                        $items,
38
                        $path['name'],
39
                        $path['separator']
40
                    );
41
                }
42
            }
43
        }
44
45
        $table = (new $this->model)->getTable();
46
47
        $models = $this->model->hydrate($items)->each->setTable($table);
48
49
        return $models->all();
50
    }
51
52
    /**
53
     * Replace the separator in a PostgreSQL path column.
54
     *
55
     * @param array $items
56
     * @param string $path
57
     * @param string $separator
58
     * @return void
59
     */
60
    protected function replacePathSeparator(array $items, $path, $separator)
61
    {
62
        foreach ($items as $item) {
63
            $item->$path = str_replace(
64
                ',',
65
                $separator,
66
                substr($item->$path, 1, -1)
67
            );
68
        }
69
    }
70
71
    /**
72
     * Get the expression grammar.
73
     *
74
     * @return \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar
75
     */
76
    public function getExpressionGrammar()
77
    {
78
        $driver = $this->query->getConnection()->getDriverName();
79
80
        switch ($driver) {
81
            case 'mysql':
82
                return $this->query->getConnection()->withTablePrefix(new MySqlGrammar);
83
            case 'pgsql':
84
                return $this->query->getConnection()->withTablePrefix(new PostgresGrammar);
85
            case 'sqlite':
86
                return $this->query->getConnection()->withTablePrefix(new SQLiteGrammar);
87
            case 'sqlsrv':
88
                return $this->query->getConnection()->withTablePrefix(new SqlServerGrammar);
89
        }
90
91
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
92
    }
93
}
94