Passed
Push — master ( 4e5334...4eb7e1 )
by Jonas
04:13
created

Builder::getModels()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 29
ccs 16
cts 16
cp 1
rs 9.7333
cc 4
nc 3
nop 1
crap 4
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
     * Get the hydrated models without eager loading.
17
     *
18
     * @param array $columns
19
     * @return \Illuminate\Database\Eloquent\Model[]
20
     */
21 278
    public function getModels($columns = ['*'])
22
    {
23 278
        $items = $this->query->get($columns)->all();
24
25 278
        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 73
            $path = $this->model->getPathName();
27
28 73
            if (isset($items[0]->$path)) {
29 35
                $this->replacePathSeparator(
30 35
                    $items,
31
                    $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 35
                    $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
                );
34
35 35
                foreach ($this->model->getCustomPaths() as $path) {
36 35
                    $this->replacePathSeparator(
37 35
                        $items,
38 35
                        $path['name'],
39 35
                        $path['separator']
40
                    );
41
                }
42
            }
43
        }
44
45 278
        $table = (new $this->model)->getTable();
46
47 278
        $models = $this->model->hydrate($items)->each->setTable($table);
48
49 278
        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 35
    protected function replacePathSeparator(array $items, $path, $separator)
61
    {
62 35
        foreach ($items as $item) {
63 35
            $item->$path = str_replace(
64 35
                ',',
65
                $separator,
66 35
                substr($item->$path, 1, -1)
67
            );
68
        }
69 35
    }
70
71
    /**
72
     * Get the expression grammar.
73
     *
74
     * @return \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar
75
     */
76 214
    public function getExpressionGrammar()
77
    {
78 214
        $driver = $this->query->getConnection()->getDriverName();
79
80 214
        switch ($driver) {
81 214
            case 'mysql':
82 57
                return $this->query->getConnection()->withTablePrefix(new MySqlGrammar);
83 157
            case 'pgsql':
84 57
                return $this->query->getConnection()->withTablePrefix(new PostgresGrammar);
85 100
            case 'sqlite':
86 57
                return $this->query->getConnection()->withTablePrefix(new SQLiteGrammar);
87 43
            case 'sqlsrv':
88 43
                return $this->query->getConnection()->withTablePrefix(new SqlServerGrammar);
89
        }
90
91
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
92
    }
93
}
94