Completed
Push — develop ( 0e6100...5d337c )
by yuuki
02:33 queued 31s
created

QueryBuilder::upsert()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 27
Code Lines 15

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 13
CRAP Score 6.7718

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 27
loc 27
ccs 13
cts 18
cp 0.7222
rs 8.439
cc 6
eloc 15
nc 7
nop 1
crap 6.7718
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
namespace Ytake\LaravelCouchbase\Database;
13
14
use Illuminate\Database\Query\Builder;
15
16
/**
17
 * Class QueryBuilder
18
 * supported N1QL.
19
 *
20
 * @see http://developer.couchbase.com/documentation/server/4.1/n1ql/n1ql-language-reference/index.html
21
 */
22
class QueryBuilder extends Builder
23
{
24
    /**
25
     * The database connection instance.
26
     *
27
     * @var \Ytake\LaravelCouchbase\Database\CouchbaseConnection
28
     */
29
    protected $connection;
30
31
    /**
32
     * The database query grammar instance.
33
     *
34
     * @var \Ytake\LaravelCouchbase\Query\Grammar
35
     */
36
    protected $grammar;
37
38
    /** @var string  use-keys-clause */
39
    public $key;
40
41
    /** @var string[]  returning-clause */
42
    public $returning = ['*'];
43
44
    /**
45
     * @param $key
46
     *
47
     * @return $this
48
     */
49 5
    public function key($key)
50
    {
51 5
        $this->key = $key;
52
53 5
        return $this;
54
    }
55
56
    /**
57
     * @param array $column
58
     *
59
     * @return $this
60
     */
61 2
    public function returning(array $column = ['*'])
62
    {
63 2
        $this->returning = $column;
64
65 2
        return $this;
66
    }
67
68
    /**
69
     * Insert a new record into the database.
70
     *
71
     * @param array $values
72
     *
73
     * @return bool
74
     */
75 4 View Code Duplication
    public function insert(array $values)
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...
76
    {
77 4
        if (empty($values)) {
78
            return true;
79
        }
80
81 4
        if (!is_array(reset($values))) {
82 4
            $values = [$values];
83 4
        } else {
84
            foreach ($values as $key => $value) {
85
                ksort($value);
86
                $values[$key] = $value;
87
            }
88
        }
89
90 4
        $bindings = [];
91
92 4
        foreach ($values as $record) {
93 4
            foreach ($record as $key => $value) {
94 4
                $bindings[$key] = $value;
95 4
            }
96 4
        }
97
98 4
        $sql = $this->grammar->compileInsert($this, $values);
99
100 4
        return $this->connection->insert($sql, $bindings);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->connection...nsert($sql, $bindings); (integer) is incompatible with the return type of the parent method Illuminate\Database\Query\Builder::insert of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
101
    }
102
103
    /**
104
     * supported N1QL upsert query.
105
     *
106
     * @param array $values
107
     *
108
     * @return bool|mixed
109
     */
110 1 View Code Duplication
    public function upsert(array $values)
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...
111
    {
112 1
        if (empty($values)) {
113
            return true;
114
        }
115
116 1
        if (!is_array(reset($values))) {
117 1
            $values = [$values];
118 1
        } else {
119
            foreach ($values as $key => $value) {
120
                ksort($value);
121
                $values[$key] = $value;
122
            }
123
        }
124
125 1
        $bindings = [];
126
127 1
        foreach ($values as $record) {
128 1
            foreach ($record as $key => $value) {
129 1
                $bindings[$key] = $value;
130 1
            }
131 1
        }
132
133 1
        $sql = $this->grammar->compileUpsert($this, $values);
134
135 1
        return $this->connection->upsert($sql, $bindings);
136
    }
137
}
138