Passed
Push — master ( 63e986...482c07 )
by Jonas
05:52
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 314
    public function getModels($columns = ['*'])
22
    {
23 314
        $items = $this->query->get($columns)->all();
24
25 314
        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 82
            $path = $this->model->getPathName();
27
28 82
            if (isset($items[0]->$path)) {
29 37
                $this->replacePathSeparator(
30 37
                    $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 37
                    $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 37
                foreach ($this->model->getCustomPaths() as $path) {
36 37
                    $this->replacePathSeparator(
37 37
                        $items,
38 37
                        $path['name'],
39 37
                        $path['separator']
40
                    );
41
                }
42
            }
43
        }
44
45 314
        $table = (new $this->model())->getTable();
46
47 314
        $models = $this->model->hydrate($items)->each->setTable($table);
48
49 314
        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 37
    protected function replacePathSeparator(array $items, $path, $separator)
61
    {
62 37
        foreach ($items as $item) {
63 37
            $item->$path = str_replace(
64 37
                ',',
65
                $separator,
66 37
                substr($item->$path, 1, -1)
67
            );
68
        }
69 37
    }
70
71
    /**
72
     * Get the expression grammar.
73
     *
74
     * @return \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar
75
     */
76 250
    public function getExpressionGrammar()
77
    {
78 250
        $driver = $this->query->getConnection()->getDriverName();
79
80 250
        switch ($driver) {
81 250
            case 'mysql':
82 66
                return $this->query->getConnection()->withTablePrefix(new MySqlGrammar());
83 184
            case 'pgsql':
84 66
                return $this->query->getConnection()->withTablePrefix(new PostgresGrammar());
85 118
            case 'sqlite':
86 66
                return $this->query->getConnection()->withTablePrefix(new SQLiteGrammar());
87 52
            case 'sqlsrv':
88 52
                return $this->query->getConnection()->withTablePrefix(new SqlServerGrammar());
89
        }
90
91
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
92
    }
93
94
    /**
95
     * Register all passed global scopes.
96
     *
97
     * @param array $scopes
98
     * @return $this
99
     */
100 68
    public function withGlobalScopes(array $scopes)
101
    {
102 68
        foreach ($scopes as $identifier => $scope) {
103 4
            $this->withGlobalScope($identifier, $scope);
104
        }
105
106 68
        return $this;
107
    }
108
}
109