Completed
Branch testing (f4ada3)
by Roman
09:11
created

Builder::addUpdatedAtColumn()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 10
cts 12
cp 0.8333
rs 9.584
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4.074
1
<?php
2
3
namespace lroman242\LaravelCassandra\Eloquent;
4
5
use Cassandra\Rows;
6
use lroman242\LaravelCassandra\Collection;
7
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8
9
class Builder extends EloquentBuilder
10
{
11
    /**
12
     * Create a collection of models from plain arrays.
13
     *
14
     * @param  \Cassandra\Rows|array  $rows
15
     *
16
     * @return Collection
17
     */
18 25
    public function hydrateRows($rows)
19
    {
20 25
        $instance = $this->newModelInstance();
21
22 25
        return $instance->newCassandraCollection($rows);
23
    }
24
25
    /**
26
     * Execute the query as a "select" statement.
27
     *
28
     * @param  array  $columns
29
     *
30
     * @return Collection
31
     *
32
     * @throws \Exception
33
     */
34 7
    public function getPage($columns = ['*'])
35
    {
36 7
        $builder = $this->applyScopes();
37
38 7
        return $builder->getModelsPage($columns);
39
    }
40
41
    /**
42
     * Get the hydrated models without eager loading.
43
     *
44
     * @param  array  $columns
45
     *
46
     * @return Collection
47
     *
48
     * @throws \Exception
49
     */
50 7 View Code Duplication
    public function getModelsPage($columns = ['*'])
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...
51
    {
52 7
        $results = $this->query->getPage($columns);
53
54 7
        if ($results instanceof Collection) {
55 7
            $rows = $results->getRows();
56 7
            if ($rows->isLastPage()) {
57 1
                $results = $results->all();
58
            } else {
59 7
                $results = $rows;
60
            }
61
        } elseif (!$results instanceof Rows) {
62
            throw new \Exception('Invalid type of getPage response. Expected lroman242\LaravelCassandra\Collection or Cassandra\Rows');
63
        }
64
65 7
        return $this->model->hydrateRows($results);
66
    }
67
68
    /**
69
     * Execute the query as a "select" statement.
70
     *
71
     * @param array $columns
72
     *
73
     * @return \Illuminate\Database\Eloquent\Collection|static[]
74
     *
75
     * @throws \Exception
76
     */
77 19
    public function get($columns = ['*'])
78
    {
79 19
        $builder = $this->applyScopes();
80
81 19
        return $builder->getModels($columns);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $builder->getModels($columns); (Illuminate\Database\Eloquent\Model[]) is incompatible with the return type of the parent method Illuminate\Database\Eloquent\Builder::get of type Illuminate\Database\Eloq...base\Eloquent\Builder[].

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...
82
    }
83
84
    /**
85
     * Get the hydrated models without eager loading.
86
     *
87
     * @param  array  $columns
88
     *
89
     * @return \Illuminate\Database\Eloquent\Collection|static[]
90
     *
91
     * @throws \Exception
92
     */
93 19 View Code Duplication
    public function getModels($columns = ['*'])
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...
94
    {
95 19
        $results = $this->query->get($columns);
96
97 19
        if ($results instanceof Collection) {
98 19
            $rows = $results->getRows();
99 19
            if ($rows->isLastPage()) {
100 19
                $results = $results->all();
101
            } else {
102 19
                $results = $rows;
103
            }
104
        } elseif (!$results instanceof Rows) {
105
            throw new \Exception('Invalid type of getPage response. Expected lroman242\LaravelCassandra\Collection or Cassandra\Rows');
106
        }
107
108 19
        return $this->model->hydrateRows($results);
109
    }
110
111
    /**
112
     * Add the "updated at" column to an array of values.
113
     *
114
     * @param  array  $values
115
     * @return array
116
     */
117 2
    protected function addUpdatedAtColumn(array $values)
118
    {
119 2
        if (!$this->model->usesTimestamps() ||
120 2
            is_null($this->model->getUpdatedAtColumn())) {
121
            return $values;
122
        }
123
124 2
        $column = $this->model->getUpdatedAtColumn();
125
126 2
        $values = array_merge(
127 2
            [$column => $this->model->freshTimestampString()],
128 2
            $values
129
        );
130
131 2
        $values[$this->qualifyColumn($column)] = $values[$column];
132 2
        if ($column != $this->qualifyColumn($column)) {
133
            unset($values[$column]);
134
        }
135
136 2
        return $values;
137
    }
138
139
140
}
141