Passed
Push — master ( 2a2829...d91029 )
by Jonas
06:26
created

BuildsAdjacencyListQueries::getExpressionGrammar()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 32
ccs 24
cts 24
cp 1
rs 8.6506
cc 7
nc 7
nop 0
crap 7
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Traits;
4
5
use Illuminate\Database\PostgresConnection;
6
use Illuminate\Support\Str;
7
use PDO;
8
use RuntimeException;
9
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\MariaDbGrammar;
10
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\MySqlGrammar;
11
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\PostgresGrammar;
12
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SingleStoreGrammar;
13
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SQLiteGrammar;
14
use Staudenmeir\LaravelAdjacencyList\Query\Grammars\SqlServerGrammar;
15
16
trait BuildsAdjacencyListQueries
17
{
18
    /**
19
     * Get the hydrated models without eager loading.
20
     *
21
     * @param array $columns
22
     * @return \Illuminate\Database\Eloquent\Model[]
23
     */
24 1196
    public function getModels($columns = ['*'])
25
    {
26 1196
        $items = $this->query->get($columns)->all();
27
28 1196
        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

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

122
            $this->/** @scrutinizer ignore-call */ 
123
                   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...
123
        }
124
125 310
        return $this;
126
    }
127
}
128