Passed
Push — master ( 951310...51c2d3 )
by Jonas
03:53
created

BuildsAdjacencyListQueries::replacePathSeparator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 3
crap 2
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\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 945
    public function getModels($columns = ['*'])
24
    {
25 945
        $items = $this->query->get($columns)->all();
26
27 945
        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 255
            $path = $this->model->getPathName();
29
30 255
            if (isset($items[0]->$path)) {
31 116
                $this->replacePathSeparator(
32 116
                    $items,
33 116
                    $path,
34 116
                    $this->model->getPathSeparator()
35 116
                );
36
37 116
                foreach ($this->model->getCustomPaths() as $path) {
38 115
                    $this->replacePathSeparator(
39 115
                        $items,
40 115
                        $path['name'],
41 115
                        $path['separator']
42 115
                    );
43
                }
44
            }
45
        }
46
47 945
        $table = (new $this->model())->getTable();
48
49 945
        $models = $this->model->hydrate($items)->each->setTable($table);
50
51 945
        return $models->all();
52
    }
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 116
    protected function replacePathSeparator(array $items, $path, $separator)
63
    {
64 116
        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
        }
71
    }
72
73
    /**
74
     * Get the expression grammar.
75
     *
76
     * @return \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar
77
     */
78 897
    public function getExpressionGrammar()
79
    {
80 897
        $driver = $this->query->getConnection()->getDriverName();
81
82
        switch ($driver) {
83 897
            case 'mysql':
84 412
                $version = $this->query->getConnection()->getReadPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
85
86 412
                $grammar = Str::contains($version, 'MariaDB')
87 169
                    ? new MariaDbGrammar($this->model)
88 243
                    : new MySqlGrammar($this->model);
89
90 412
                return $this->query->getConnection()->withTablePrefix($grammar);
91 485
            case 'pgsql':
92 243
                return $this->query->getConnection()->withTablePrefix(
93 243
                    new PostgresGrammar($this->model)
94 243
                );
95 242
            case 'sqlite':
96 242
                return $this->query->getConnection()->withTablePrefix(
97 242
                    new SQLiteGrammar($this->model)
98 242
                );
99
            case 'sqlsrv':
100
                return $this->query->getConnection()->withTablePrefix(
101
                    new SqlServerGrammar($this->model)
102
                );
103
        }
104
105
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
106
    }
107
108
    /**
109
     * Register all passed global scopes.
110
     *
111
     * @param array $scopes
112
     * @return $this
113
     */
114 256
    public function withGlobalScopes(array $scopes)
115
    {
116 256
        foreach ($scopes as $identifier => $scope) {
117 16
            $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

117
            $this->/** @scrutinizer ignore-call */ 
118
                   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...
118
        }
119
120 256
        return $this;
121
    }
122
}
123