Builder   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 168
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A find() 0 4 1
A first() 0 4 1
A get() 0 6 1
A pluck() 0 8 2
A lists() 0 4 1
A getModels() 0 8 1
A getQuery() 0 4 1
A setQuery() 0 6 1
A setModel() 0 8 1
A getModel() 0 4 1
A __call() 0 6 1
1
<?php
2
3
namespace Magister\Services\Database\Elegant;
4
5
use Magister\Services\Database\Query\Builder as QueryBuilder;
6
7
/**
8
 * Class Builder.
9
 */
10
class Builder
11
{
12
    /**
13
     * The base query builder implementation.
14
     *
15
     * @var \Magister\Services\Database\Query\Builder
16
     */
17
    protected $query;
18
19
    /**
20
     * The model being queried.
21
     *
22
     * @var \Magister\Services\Database\Elegant\Model
23
     */
24
    protected $model;
25
26
    /**
27
     * Create a new elegant builder instance.
28
     *
29
     * @param \Magister\Services\Database\Query\Builder $query
30
     */
31
    public function __construct(QueryBuilder $query)
32
    {
33
        $this->query = $query;
34
    }
35
36
    /**
37
     * Find a model by its primary key.
38
     *
39
     * @param mixed $id
40
     *
41
     * @return \Magister\Services\Database\Elegant\Model|\Magister\Services\Support\Collection|null
42
     */
43
    public function find($id)
44
    {
45
        return $this->get()->find($id);
46
    }
47
48
    /**
49
     * Execute the query and get the first result.
50
     *
51
     * @return \Magister\Services\Database\Elegant\Model|static|null
52
     */
53
    public function first()
54
    {
55
        return $this->get()->first();
56
    }
57
58
    /**
59
     * Execute the query as a select statement.
60
     *
61
     * @return \Magister\Services\Support\Collection|static[]
62
     */
63
    public function get()
64
    {
65
        $models = $this->getModels();
66
67
        return $this->model->newCollection($models);
68
    }
69
70
    /**
71
     * Pluck a single column from the database.
72
     *
73
     * @param string $column
74
     *
75
     * @return mixed
76
     */
77
    public function pluck($column)
78
    {
79
        $result = $this->first();
80
81
        if ($result) {
82
            return $result->$column;
83
        }
84
    }
85
86
    /**
87
     * Get an array with the values of a given column.
88
     *
89
     * @param string $column
90
     * @param string $key
91
     *
92
     * @return array
93
     */
94
    public function lists($column, $key = null)
95
    {
96
        return $this->query->lists($column, $key);
97
    }
98
99
    /**
100
     * Get the hydrated models.
101
     *
102
     * @return array
103
     */
104
    public function getModels()
105
    {
106
        $results = $this->query->get();
107
108
        $connection = $this->model->getConnectionName();
109
110
        return $this->model->hydrate($results, $connection)->all();
0 ignored issues
show
Bug introduced by
It seems like $results defined by $this->query->get() on line 106 can also be of type false or null; however, Magister\Services\Databa...legant\Model::hydrate() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
111
    }
112
113
    /**
114
     * Get the underlying query builder instance.
115
     *
116
     * @return \Magister\Services\Database\Query\Builder|static
117
     */
118
    public function getQuery()
119
    {
120
        return $this->query;
121
    }
122
123
    /**
124
     * Set the underlying query builder instance.
125
     *
126
     * @param \Magister\Services\Database\Query\Builder $query
127
     *
128
     * @return $this
129
     */
130
    public function setQuery($query)
131
    {
132
        $this->query = $query;
133
134
        return $this;
135
    }
136
137
    /**
138
     * Set the model instance being queried.
139
     *
140
     * @param \Magister\Services\Database\Elegant\Model $model
141
     *
142
     * @return $this
143
     */
144
    public function setModel(Model $model)
145
    {
146
        $this->model = $model;
147
148
        $this->query->from($model->getUrl());
149
150
        return $this;
151
    }
152
153
    /**
154
     * Get the model instance being queried.
155
     *
156
     * @return \Magister\Services\Database\Elegant\Model
157
     */
158
    public function getModel()
159
    {
160
        return $this->model;
161
    }
162
163
    /**
164
     * Dynamically handle calls into the query instance.
165
     *
166
     * @param string $method
167
     * @param array  $parameters
168
     *
169
     * @return mixed
170
     */
171
    public function __call($method, $parameters)
172
    {
173
        call_user_func_array([$this->query, $method], $parameters);
174
175
        return $this;
176
    }
177
}
178