Passed
Push — master ( b9affb...b0865c )
by Jonas
14:48 queued 32s
created

ManagesViews::createMaterializedView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\LaravelMigrationViews\Schema\Builders;
4
5
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6
use Illuminate\Database\Query\Builder as QueryBuilder;
7
use Illuminate\Support\Str;
8
9
trait ManagesViews
10
{
11
    /**
12
     * Create a new view on the schema.
13
     *
14
     * @param string $name
15
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query
16
     * @param array|null $columns
17
     * @param bool $orReplace
18 40
     * @param bool $materialized
19
     * @return void
20 40
     */
21
    public function createView($name, $query, array $columns = null, $orReplace = false, bool $materialized = false)
22 40
    {
23 40
        $query = $this->getQueryString($query);
24 40
25
        $this->connection->statement(
26
            $this->grammar->compileCreateView($name, $query, $columns, $orReplace, $materialized)
27
        );
28
    }
29
30
    /**
31
     * Create a new view on the schema or replace an existing one.
32
     *
33
     * @param string $name
34
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query
35 4
     * @param array|null $columns
36
     * @return void
37 4
     */
38
    public function createOrReplaceView($name, $query, array $columns = null)
39
    {
40
        $this->createView($name, $query, $columns, true);
41
    }
42
43
    /**
44
     * Create a new materialized view on the schema.
45
     *
46 40
     * @param string $name
47
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query
48 40
     * @param array|null $columns
49 4
     * @return void
50
     */
51
    public function createMaterializedView(string $name, EloquentBuilder|QueryBuilder $query, array $columns = null): void
52 36
    {
53 36
        $this->createView($name, $query, $columns, materialized: true);
54 36
    }
55
56 36
    /**
57
     * Convert the query and its bindings to an SQL string.
58
     *
59
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query
60
     * @return string
61
     */
62
    protected function getQueryString($query)
63
    {
64
        if (is_string($query)) {
65 36
            return $query;
66
        }
67 36
68
        $bindings = $this->stringifyBindings(
69 36
            $query->getBindings()
0 ignored issues
show
Bug introduced by
It seems like $query->getBindings() can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Builder; however, parameter $bindings of Staudenmeir\LaravelMigra...ws::stringifyBindings() does only seem to accept array, 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

69
            /** @scrutinizer ignore-type */ $query->getBindings()
Loading history...
70 36
        );
71 4
72
        return Str::replaceArray('?', $bindings, $query->toSql());
0 ignored issues
show
Bug introduced by
It seems like $query->toSql() can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Builder; however, parameter $subject of Illuminate\Support\Str::replaceArray() 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

72
        return Str::replaceArray('?', $bindings, /** @scrutinizer ignore-type */ $query->toSql());
Loading history...
73
    }
74 36
75 15
    /**
76
     * Stringify the query bindings.
77
     *
78
     * @param array $bindings
79 36
     * @return array
80
     */
81
    protected function stringifyBindings(array $bindings)
82
    {
83
        $bindings = $this->connection->prepareBindings($bindings);
84
85
        foreach ($bindings as &$binding) {
86
            if (is_object($binding)) {
87
                $binding = (string) $binding;
88
            }
89 3
90
            if (is_string($binding)) {
91 3
                $binding = $this->connection->getPdo()->quote($binding);
92
            }
93
        }
94
95
        return $bindings;
96
    }
97
98
    /**
99
     * Rename a view on the schema.
100
     *
101 33
     * @param string $from
102
     * @param string $to
103 33
     * @return void
104 33
     */
105 33
    public function renameView($from, $to)
106
    {
107
        $this->rename($from, $to);
0 ignored issues
show
Bug introduced by
The method rename() does not exist on Staudenmeir\LaravelMigra...a\Builders\ManagesViews. Did you maybe mean renameView()? ( Ignorable by Annotation )

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

107
        $this->/** @scrutinizer ignore-call */ 
108
               rename($from, $to);

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...
108
    }
109
110
    /**
111
     * Drop a view from the schema.
112
     *
113
     * @param string $name
114 44
     * @param bool $ifExists
115
     * @return void
116 44
     */
117
    public function dropView($name, $ifExists = false)
118
    {
119
        $this->connection->statement(
120
            $this->grammar->compileDropView($name, $ifExists)
121
        );
122
    }
123
124
    /**
125 12
     * Drop a view from the schema if it exists.
126
     *
127 12
     * @param string $name
128
     * @return void
129 12
     */
130 12
    public function dropViewIfExists($name)
131 12
    {
132 12
        $this->dropView($name, true);
133
    }
134 12
135
    /**
136
     * Determine if the given view exists on the schema.
137
     *
138
     * @param string $name
139
     * @return bool
140
     */
141
    public function hasView($name)
142
    {
143 3
        $view = $this->connection->getTablePrefix().$name;
144
145 3
        $view = $this->connection->selectOne(
146
            $this->grammar->compileViewExists(),
147
            $this->getBindingsForHasView($view)
148
        );
149
150
        return !is_null($view);
151
    }
152
153
    /**
154 3
     * Get the bindings for a "Has View" statement.
155
     *
156 3
     * @param string $view
157
     * @return array
158
     */
159
    protected function getBindingsForHasView($view)
160
    {
161
        return [$this->connection->getDatabaseName(), $view];
162
    }
163
164
    /**
165
     * Get the column listing for a given view.
166
     *
167
     * @param string $name
168
     * @return array
169
     */
170
    public function getViewColumnListing($name)
171
    {
172
        return $this->getColumnListing($name);
0 ignored issues
show
Bug introduced by
It seems like getColumnListing() 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

172
        return $this->/** @scrutinizer ignore-call */ getColumnListing($name);
Loading history...
173
    }
174
175
    /**
176
     * Refresh a materialized view on the schema.
177
     *
178
     * @param string $name
179
     * @return void
180
     */
181
    public function refreshMaterializedView(string $name): void
182
    {
183
        $this->connection->statement(
184
            $this->grammar->compileRefreshMaterializedView($name)
185
        );
186
    }
187
}
188