Passed
Push — master ( 3415eb...56b5c5 )
by Jonas
04:04
created

ManagesViews::getViewColumnListing()   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 1
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\Support\Str;
6
7
trait ManagesViews
8
{
9
    /**
10
     * Create a new view on the schema.
11
     *
12
     * @param string $name
13
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query
14
     * @param array|null $columns
15
     * @param bool $orReplace
16
     * @return void
17
     */
18 40
    public function createView($name, $query, array $columns = null, $orReplace = false)
19
    {
20 40
        $query = $this->getQueryString($query);
21
22 40
        $this->connection->statement(
23 40
            $this->grammar->compileCreateView($name, $query, $columns, $orReplace)
24
        );
25 40
    }
26
27
    /**
28
     * Create a new view on the schema or replace an existing one.
29
     *
30
     * @param string $name
31
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query
32
     * @param array|null $columns
33
     * @return void
34
     */
35 4
    public function createOrReplaceView($name, $query, array $columns = null)
36
    {
37 4
        $this->createView($name, $query, $columns, true);
38 4
    }
39
40
    /**
41
     * Convert the query and its bindings to an SQL string.
42
     *
43
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query
44
     * @return string
45
     */
46 40
    protected function getQueryString($query)
47
    {
48 40
        if (is_string($query)) {
49 4
            return $query;
50
        }
51
52 36
        $bindings = $this->connection->prepareBindings($query->getBindings());
53
54 36
        foreach ($bindings as &$binding) {
55 36
            if (is_object($binding)) {
56 4
                $binding = (string) $binding;
57
            }
58
59 36
            if (is_string($binding)) {
60 8
                $binding = $this->connection->getPdo()->quote($binding);
61
            }
62
        }
63
64 36
        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

64
        return Str::replaceArray('?', $bindings, /** @scrutinizer ignore-type */ $query->toSql());
Loading history...
65
    }
66
67
    /**
68
     * Rename a view on the schema.
69
     *
70
     * @param string $from
71
     * @param string $to
72
     * @return void
73
     */
74 3
    public function renameView($from, $to)
75
    {
76 3
        $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

76
        $this->/** @scrutinizer ignore-call */ 
77
               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...
77 3
    }
78
79
    /**
80
     * Drop a view from the schema.
81
     *
82
     * @param string $name
83
     * @param bool $ifExists
84
     * @return void
85
     */
86 33
    public function dropView($name, $ifExists = false)
87
    {
88 33
        $this->connection->statement(
89 33
            $this->grammar->compileDropView($name, $ifExists)
90
        );
91 33
    }
92
93
    /**
94
     * Drop a view from the schema if it exists.
95
     *
96
     * @param string $name
97
     * @return void
98
     */
99 44
    public function dropViewIfExists($name)
100
    {
101 44
        $this->dropView($name, true);
102 44
    }
103
104
    /**
105
     * Determine if the given view exists on the schema.
106
     *
107
     * @param string $name
108
     * @return bool
109
     */
110 12
    public function hasView($name)
111
    {
112 12
        $view = $this->connection->getTablePrefix().$name;
113
114 12
        $view = $this->connection->selectOne(
115 12
            $this->grammar->compileViewExists(),
116 12
            $this->getBindingsForHasView($view)
117
        );
118
119 12
        return !is_null($view);
120
    }
121
122
    /**
123
     * Get the bindings for a "Has View" statement.
124
     *
125
     * @param string $view
126
     * @return array
127
     */
128 3
    protected function getBindingsForHasView($view)
129
    {
130 3
        return [$this->connection->getDatabaseName(), $view];
131
    }
132
133
    /**
134
     * Get the column listing for a given view.
135
     *
136
     * @param string $name
137
     * @return array
138
     */
139 3
    public function getViewColumnListing($name)
140
    {
141 3
        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

141
        return $this->/** @scrutinizer ignore-call */ getColumnListing($name);
Loading history...
142
    }
143
}
144