Completed
Push — master ( db5413...63e986 )
by Jonas
26s queued 11s
created

Builder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 97.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 36
c 1
b 0
f 0
dl 0
loc 94
ccs 36
cts 37
cp 0.973
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpressionGrammar() 0 16 5
A replacePathSeparator() 0 7 2
A withGlobalScopes() 0 7 2
A getModels() 0 29 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
     * Register all passed global scopes.
17
     *
18
     * @param  array|null  $scopes
19
     * @return $this
20
     */
21 48
    public function withGlobalScopes(array $scopes)
22
	{
23 48
		foreach ($scopes as $identifier => $scope) {
24
			$this->withGlobalScope($identifier, $scope);
25
		}
26
27 48
        return $this;
28
	}
29
30
    /**
31
     * Get the hydrated models without eager loading.
32
     *
33
     * @param array $columns
34
     * @return \Illuminate\Database\Eloquent\Model[]
35
     */
36 290
    public function getModels($columns = ['*'])
37
    {
38 290
        $items = $this->query->get($columns)->all();
39
40 290
        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

40
        if ($this->/** @scrutinizer ignore-call */ getConnection() instanceof PostgresConnection) {
Loading history...
41 76
            $path = $this->model->getPathName();
42
43 76
            if (isset($items[0]->$path)) {
44 37
                $this->replacePathSeparator(
45 37
                    $items,
46
                    $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

46
                    /** @scrutinizer ignore-type */ $path,
Loading history...
47 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

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