BuildsAdjacencyListQueries   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 51
c 1
b 0
f 0
dl 0
loc 116
ccs 54
cts 54
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getModels() 0 29 4
A replacePathSeparator() 0 7 2
A withGlobalScopes() 0 7 2
B getExpressionGrammar() 0 38 9
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Traits;
4
5
use Illuminate\Database\PostgresConnection;
6
use RuntimeException;
7
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\FirebirdGrammar;
8
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\MariaDbGrammar;
9
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\MySqlGrammar;
10
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\PostgresGrammar;
11
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SingleStoreGrammar;
12
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SQLiteGrammar;
13
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SqlServerGrammar;
14
15
trait BuildsAdjacencyListQueries
16
{
17
    /**
18
     * Get the hydrated models without eager loading.
19
     *
20
     * @param array $columns
21
     * @return \Illuminate\Database\Eloquent\Model[]
22
     */
23
    public function getModels($columns = ['*'])
24 1196
    {
25
        $items = $this->query->get($columns)->all();
26 1196
27
        if ($this->getConnection() instanceof PostgresConnection) {
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

27
        if ($this->/** @scrutinizer ignore-call */ getConnection() instanceof PostgresConnection) {
Loading history...
28 1196
            $path = $this->model->getPathName();
29 256
30
            if (isset($items[0]->$path)) {
31 256
                $this->replacePathSeparator(
32 116
                    $items,
33 116
                    $path,
34 116
                    $this->model->getPathSeparator()
35 116
                );
36 116
37
                foreach ($this->model->getCustomPaths() as $path) {
38 116
                    $this->replacePathSeparator(
39 115
                        $items,
40 115
                        $path['name'],
41 115
                        $path['separator']
42 115
                    );
43 115
                }
44
            }
45
        }
46
47
        $table = (new $this->model())->getTable();
48 1196
49
        $models = $this->model->hydrate($items)->each->setTable($table);
50 1196
51
        return $models->all();
52 1196
    }
53
54
    /**
55
     * Replace the separator in a PostgreSQL path column.
56
     *
57
     * @param array $items
58
     * @param string $path
59
     * @param string $separator
60
     * @return void
61
     */
62
    protected function replacePathSeparator(array $items, $path, $separator)
63 116
    {
64
        foreach ($items as $item) {
65 116
            $item->$path = str_replace(
66 116
                ',',
67 116
                $separator,
68 116
                substr($item->$path, 1, -1)
69 116
            );
70 116
        }
71
    }
72
73
    /**
74
     * Get the expression grammar.
75
     *
76
     * @return \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar
77
     */
78
    public function getExpressionGrammar()
79 1119
    {
80
        $driver = $this->query->getConnection()->getDriverName();
81 1119
82
        switch ($driver) {
83
            case 'mysql':
84 1119
                $grammar = $this->query->getConnection()->isMaria()
85 412
                    ? new MariaDbGrammar($this->model)
86
                    : new MySqlGrammar($this->model);
87 412
88 169
                return $this->query->getConnection()->withTablePrefix($grammar);
89 243
            case 'mariadb':
90
                return $this->query->getConnection()->withTablePrefix(
91 412
                    new MariaDbGrammar($this->model)
92 707
                );
93 243
            case 'pgsql':
94 243
                return $this->query->getConnection()->withTablePrefix(
95 243
                    new PostgresGrammar($this->model)
96 464
                );
97 242
            case 'sqlite':
98 242
                return $this->query->getConnection()->withTablePrefix(
99 242
                    new SQLiteGrammar($this->model)
100 222
                );
101 158
            case 'sqlsrv':
102 158
                return $this->query->getConnection()->withTablePrefix(
103 158
                    new SqlServerGrammar($this->model)
104 64
                );
105 64
            case 'singlestore':
106 64
                return $this->query->getConnection()->withTablePrefix(
107 64
                    new SingleStoreGrammar($this->model)
108
                );
109
            case 'firebird':
110
                return $this->query->getConnection()->withTablePrefix(
111
                    new FirebirdGrammar($this->model)
112
                );
113
        }
114
115
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
116
    }
117
118
    /**
119 310
     * Register all passed global scopes.
120
     *
121 310
     * @param array $scopes
122 20
     * @return $this
123
     */
124
    public function withGlobalScopes(array $scopes)
125 310
    {
126
        foreach ($scopes as $identifier => $scope) {
127
            $this->withGlobalScope($identifier, $scope);
0 ignored issues
show
Bug introduced by
The method withGlobalScope() does not exist on Staudenmeir\LaravelAdjac...ldsAdjacencyListQueries. Did you maybe mean withGlobalScopes()? ( Ignorable by Annotation )

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

127
            $this->/** @scrutinizer ignore-call */ 
128
                   withGlobalScope($identifier, $scope);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
        }
129
130
        return $this;
131
    }
132
}
133