Completed
Pull Request — master (#29)
by yuuki
05:25
created

Blueprint   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 136
Duplicated Lines 17.65 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 24
loc 136
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A connector() 0 4 1
A setOptions() 0 4 1
A create() 0 4 1
A drop() 0 4 1
A dropPrimary() 0 7 2
A dropIndex() 0 5 1
A primaryIndex() 11 11 2
A index() 13 13 2
A getTable() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 */
12
13
namespace Ytake\LaravelCouchbase\Schema;
14
15
use Ytake\LaravelCouchbase\Database\CouchbaseConnection;
16
17
/**
18
 * Class Blueprint
19
 *
20
 * @author Yuuki Takezawa<[email protected]>
21
 */
22
class Blueprint extends \Illuminate\Database\Schema\Blueprint
23
{
24
    use NotSupportedTrait;
25
26
    /** @var  CouchbaseConnection */
27
    protected $connection;
28
29
    /** @var string[] */
30
    protected $options = [
31
        'bucketType'   => 'couchbase',
32
        'saslPassword' => '',
33
        'flushEnabled' => true,
34
    ];
35
36
    /**
37
     * @param CouchbaseConnection $connection
38
     */
39
    public function connector(CouchbaseConnection $connection)
40
    {
41
        $this->connection = $connection;
42
    }
43
44
    /**
45
     * @param array $options
46
     */
47
    public function setOptions(array $options)
48
    {
49
        $this->options = array_merge($this->options, $options);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->options, $options) of type array is incompatible with the declared type array<integer,string> of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
    }
51
52
    /**
53
     * @return bool
54
     */
55
    public function create()
56
    {
57
        return $this->connection->manager()->createBucket($this->table, $this->options);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function drop()
64
    {
65
        return $this->connection->manager()->removeBucket($this->table);
66
    }
67
68
    /**
69
     * drop for N1QL primary index
70
     *
71
     * @param string $index
72
     * @param bool   $ignoreIfNotExist
73
     *
74
     * @return mixed
75
     */
76
    public function dropPrimary($index = null, $ignoreIfNotExist = false)
77
    {
78
        $index = (is_null($index)) ? "" : $index;
79
80
        return $this->connection->openBucket($this->getTable())
81
            ->manager()->dropN1qlPrimaryIndex($index, $ignoreIfNotExist);
82
    }
83
84
    /**
85
     * drop for N1QL secondary index
86
     *
87
     * @param string $index
88
     * @param bool   $ignoreIfNotExist
89
     *
90
     * @return mixed
91
     */
92
    public function dropIndex($index, $ignoreIfNotExist = false)
93
    {
94
        return $this->connection->openBucket($this->getTable())
95
            ->manager()->dropN1qlIndex($index, $ignoreIfNotExist);
96
    }
97
98
    /**
99
     * Specify the primary index for the current bucket.
100
     *
101
     * @param string|null $name
102
     * @param boolean     $ignoreIfExist  if a primary index already exists, an exception will be thrown unless this is
103
     *                                    set to true.
104
     * @param boolean     $defer          true to defer building of the index until buildN1qlDeferredIndexes()}is
105
     *                                    called (or a direct call to the corresponding query service API).
106
     *
107
     * @return mixed
108
     */
109 View Code Duplication
    public function primaryIndex($name = null, $ignoreIfExist = false, $defer = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $name = (is_null($name)) ? "" : $name;
112
113
        return $this->connection->openBucket($this->getTable())
114
            ->manager()->createN1qlPrimaryIndex(
115
                $name,
116
                $ignoreIfExist,
117
                $defer
118
            );
119
    }
120
121
    /**
122
     * Specify a secondary index for the current bucket.
123
     *
124
     * @param array   $columns            the JSON fields to index.
125
     * @param string  $name               the name of the index.
126
     * @param string  $whereClause        the WHERE clause of the index.
127
     * @param boolean $ignoreIfExist      if a secondary index already exists with that name, an exception will be
128
     *                                    thrown unless this is set to true.
129
     * @param boolean $defer              true to defer building of the index until buildN1qlDeferredIndexes() is
130
     *                                    called (or a direct call to the corresponding query service API).
131
     *
132
     * @return mixed
133
     */
134 View Code Duplication
    public function index($columns, $name = null, $whereClause = '', $ignoreIfExist = false, $defer = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        $name = (is_null($name)) ? $this->getTable() . "_secondary_index" : $name;
137
138
        return $this->connection->openBucket($this->getTable())
139
            ->manager()->createN1qlIndex(
140
                $name,
141
                $columns,
142
                $whereClause,
143
                $ignoreIfExist,
144
                $defer
145
            );
146
    }
147
148
    /**
149
     * Get the table the blueprint describes.
150
     *
151
     * @return string
152
     */
153
    public function getTable()
154
    {
155
        return $this->table;
156
    }
157
}
158