Completed
Pull Request — develop (#87)
by Abdelrahman
04:25 queued 01:31
created

EloquentRepository::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Repository Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Repository Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Repository\Repositories;
17
18
use Illuminate\Pagination\Paginator;
19
use Illuminate\Database\Eloquent\Model;
20
use Rinvex\Repository\Exceptions\RepositoryException;
21
22
class EloquentRepository extends BaseRepository
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function createModel()
28
    {
29
        if (is_string($model = $this->getModel())) {
30
            if (! class_exists($class = '\\'.ltrim($model, '\\'))) {
31
                throw new RepositoryException("Class {$model} does NOT exist!");
32
            }
33
34
            $model = $this->getContainer()->make($class);
35
        }
36
37
        // Set the connection used by the model
38
        if (! empty($this->connection)) {
39
            $model = $model->setConnection($this->connection);
40
        }
41
42
        if (! $model instanceof Model) {
43
            throw new RepositoryException("Class {$model} must be an instance of \\Illuminate\\Database\\Eloquent\\Model");
44
        }
45
46
        return $model;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function find($id, $attributes = ['*'])
53
    {
54
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($id, $attributes) {
55
            return $this->prepareQuery($this->createModel())->find($id, $attributes);
56
        });
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function findBy($attribute, $value, $attributes = ['*'])
63
    {
64
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attribute, $value, $attributes) {
65
            return $this->prepareQuery($this->createModel())->where($attribute, '=', $value)->first($attributes);
66
        });
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function findFirst($attributes = ['*'])
73
    {
74
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attributes) {
75
            return $this->prepareQuery($this->createModel())->first($attributes);
76
        });
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function findAll($attributes = ['*'])
83
    {
84
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attributes) {
85
            return $this->prepareQuery($this->createModel())->get($attributes);
86
        });
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function paginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null)
93
    {
94
        $page = $page ?: Paginator::resolveCurrentPage($pageName);
95
96
        return $this->executeCallback(get_called_class(), __FUNCTION__, array_merge(func_get_args(), compact('page')), function () use ($perPage, $attributes, $pageName, $page) {
97
            return $this->prepareQuery($this->createModel())->paginate($perPage, $attributes, $pageName, $page);
98
        });
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function simplePaginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null)
105
    {
106
        $page = $page ?: Paginator::resolveCurrentPage($pageName);
107
108
        return $this->executeCallback(get_called_class(), __FUNCTION__, array_merge(func_get_args(), compact('page')), function () use ($perPage, $attributes, $pageName, $page) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 178 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
109
            return $this->prepareQuery($this->createModel())->simplePaginate($perPage, $attributes, $pageName, $page);
110
        });
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function findWhere(array $where, $attributes = ['*'])
117
    {
118
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
119
            list($attribute, $operator, $value, $boolean) = array_pad($where, 4, null);
120
121
            $this->where($attribute, $operator, $value, $boolean);
122
123
            return $this->prepareQuery($this->createModel())->get($attributes);
124
        });
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function findWhereIn(array $where, $attributes = ['*'])
131
    {
132
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
133
            list($attribute, $values, $boolean, $not) = array_pad($where, 4, null);
134
135
            $this->whereIn($attribute, $values, $boolean, $not);
136
137
            return $this->prepareQuery($this->createModel())->get($attributes);
138
        });
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function findWhereNotIn(array $where, $attributes = ['*'])
145
    {
146
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
147
            list($attribute, $values, $boolean) = array_pad($where, 3, null);
148
149
            $this->whereNotIn($attribute, $values, $boolean);
150
151
            return $this->prepareQuery($this->createModel())->get($attributes);
152
        });
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function findWhereHas(array $where, $attributes = ['*'])
159
    {
160
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
161
            list($relation, $callback, $operator, $count) = array_pad($where, 4, null);
162
163
            $this->whereHas($relation, $callback, $operator, $count);
164
165
            return $this->prepareQuery($this->createModel())->get($attributes);
166
        });
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function create(array $attributes = [])
173
    {
174
        // Create a new instance
175
        $instance = $this->createModel();
176
177
        // Fire the created event
178
        $this->getContainer('events')->fire($this->getRepositoryId().'.entity.creating', [$this, $instance]);
179
180
        // Fill instance with data
181
        $instance->fill($attributes);
182
183
        // Save the instance
184
        $created = $instance->save();
185
186
        // Fire the created event
187
        $this->getContainer('events')->fire($this->getRepositoryId().'.entity.created', [$this, $instance]);
188
189
        // Return instance
190
        return $created ? $instance : $created;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function update($id, array $attributes = [])
197
    {
198
        $updated = false;
199
200
        // Find the given instance
201
        $instance = $id instanceof Model ? $id : $this->find($id);
202
203
        if ($instance) {
204
            // Fire the updated event
205
            $this->getContainer('events')->fire($this->getRepositoryId().'.entity.updating', [$this, $instance]);
206
207
            // Fill instance with data
208
            $instance->fill($attributes);
209
210
            // Update the instance
211
            $updated = $instance->save();
212
213
            // Fire the updated event
214
            $this->getContainer('events')->fire($this->getRepositoryId().'.entity.updated', [$this, $instance]);
215
        }
216
217
        return $updated ? $instance : $updated;
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function delete($id)
224
    {
225
        $deleted  = false;
226
227
        // Find the given instance
228
        $instance = $id instanceof Model ? $id : $this->find($id);
229
230
        if ($instance) {
231
            // Fire the deleted event
232
            $this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleting', [$this, $instance]);
233
234
            // Delete the instance
235
            $deleted = $instance->delete();
236
237
            // Fire the deleted event
238
            $this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleted', [$this, $instance]);
239
        }
240
241
        return $deleted ? $instance : $deleted;
242
    }
243
244
    /**
245
     * {@inheritdoc}
246
     */
247
    public function beginTransaction()
248
    {
249
        $this->getContainer('db')->beginTransaction();
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255
    public function commit()
256
    {
257
        $this->getContainer('db')->commit();
258
    }
259
260
    /**
261
     * {@inheritdoc}
262
     */
263
    public function rollBack()
264
    {
265
        $this->getContainer('db')->rollBack();
266
    }
267
268
    /**
269
     * {@inheritdoc}
270
     */
271
    public function count($columns = '*')
272
    {
273
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($columns) {
274
            return $this->prepareQuery($this->createModel())->count($columns);
275
        });
276
    }
277
278
    /**
279
     * {@inheritdoc}
280
     */
281
    public function min($column)
282
    {
283
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) {
284
            return $this->prepareQuery($this->createModel())->min($column);
285
        });
286
    }
287
288
    /**
289
     * {@inheritdoc}
290
     */
291
    public function max($column)
292
    {
293
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) {
294
            return $this->prepareQuery($this->createModel())->max($column);
295
        });
296
    }
297
298
    /**
299
     * {@inheritdoc}
300
     */
301
    public function avg($column)
302
    {
303
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) {
304
            return $this->prepareQuery($this->createModel())->avg($column);
305
        });
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311
    public function sum($column)
312
    {
313
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) {
314
            return $this->prepareQuery($this->createModel())->sum($column);
315
        });
316
    }
317
}
318