Completed
Pull Request — master (#73)
by
unknown
03:12
created

EloquentRepository::findWhereIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 1
Metric Value
c 7
b 1
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
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\Database\Eloquent\Model;
19
use Illuminate\Pagination\Paginator;
20
use Rinvex\Repository\Exceptions\RepositoryException;
21
22
class EloquentRepository extends BaseRepository
23
{
24
    /**
25
     * Create a new repository model instance.
26
     *
27
     * @throws \Rinvex\Repository\Exceptions\RepositoryException
28
     *
29
     * @return \Illuminate\Database\Eloquent\Model
30
     */
31
    public function createModel()
32
    {
33
        if (is_string($model = $this->getModel())) {
34
            if (! class_exists($class = '\\'.ltrim($model, '\\'))) {
35
                throw new RepositoryException("Class {$model} does NOT exist!");
36
            }
37
38
            $model = $this->getContainer()->make($class);
39
        }
40
41
        if (! $model instanceof Model) {
42
            throw new RepositoryException("Class {$model} must be an instance of \\Illuminate\\Database\\Eloquent\\Model");
43
        }
44
45
        return $model;
46
    }
47
48
    /**
49
     * Find an entity by it's primary key.
50
     *
51
     * @param int   $id
52
     * @param array $attributes
53
     *
54
     * @return \Illuminate\Database\Eloquent\Model
55
     */
56
    public function find($id, $attributes = ['*'])
57
    {
58
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($id, $attributes) {
59
            return $this->prepareQuery($this->createModel())->find($id, $attributes);
60
        });
61
    }
62
63
    /**
64
     * Find an entity by one of it's attributes.
65
     *
66
     * @param string $attribute
67
     * @param string $value
68
     * @param array  $attributes
69
     *
70
     * @return \Illuminate\Database\Eloquent\Model
71
     */
72
    public function findBy($attribute, $value, $attributes = ['*'])
73
    {
74
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attribute, $value, $attributes) {
75
            return $this->prepareQuery($this->createModel())->where($attribute, '=', $value)->first($attributes);
76
        });
77
    }
78
79
    /**
80
     * Find all entities.
81
     *
82
     * @param array $attributes
83
     *
84
     * @return \Illuminate\Support\Collection
85
     */
86
    public function findAll($attributes = ['*'])
87
    {
88
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attributes) {
89
            return $this->prepareQuery($this->createModel())->get($attributes);
90
        });
91
    }
92
93
    /**
94
     * Paginate all entities.
95
     *
96
     * @param int|null $perPage
97
     * @param array    $attributes
98
     * @param string   $pageName
99
     * @param int|null $page
100
     *
101
     * @throws \InvalidArgumentException
102
     *
103
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
104
     */
105
    public function paginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null)
106
    {
107
        $page = $page ?: Paginator::resolveCurrentPage($pageName);
108
109
        return $this->executeCallback(get_called_class(), __FUNCTION__, array_merge(func_get_args(), compact('page')), function () use ($perPage, $attributes, $pageName, $page) {
110
            return $this->prepareQuery($this->createModel())->paginate($perPage, $attributes, $pageName, $page);
111
        });
112
    }
113
114
    /**
115
     * Paginate all entities into a simple paginator.
116
     *
117
     * @param int|null $perPage
118
     * @param array    $attributes
119
     * @param string   $pageName
120
     * @param int|null $page
121
     *
122
     * @return \Illuminate\Contracts\Pagination\Paginator
123
     */
124
    public function simplePaginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null)
125
    {
126
        $page = $page ?: Paginator::resolveCurrentPage($pageName);
127
128
        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...
129
            return $this->prepareQuery($this->createModel())->simplePaginate($perPage, $attributes, $pageName, $page);
130
        });
131
    }
132
133
    /**
134
     * Find all entities matching where conditions.
135
     *
136
     * @param array $where
137
     * @param array $attributes
138
     *
139
     * @return \Illuminate\Support\Collection
140
     */
141
    public function findWhere(array $where, $attributes = ['*'])
142
    {
143
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
144
            list($attribute, $operator, $value, $boolean) = array_pad($where, 4, null);
145
146
            $this->where($attribute, $operator, $value, $boolean);
147
148
            return $this->prepareQuery($this->createModel())->get($attributes);
149
        });
150
    }
151
152
    /**
153
     * Find all entities matching whereIn conditions.
154
     *
155
     * @param array $where
156
     * @param array $attributes
157
     *
158
     * @return \Illuminate\Support\Collection
159
     */
160
    public function findWhereIn(array $where, $attributes = ['*'])
161
    {
162
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
163
            list($attribute, $values, $boolean, $not) = array_pad($where, 4, null);
164
165
            $this->whereIn($attribute, $values, $boolean, $not);
166
167
            return $this->prepareQuery($this->createModel())->get($attributes);
168
        });
169
    }
170
171
    /**
172
     * Find all entities matching whereNotIn conditions.
173
     *
174
     * @param array $where
175
     * @param array $attributes
176
     *
177
     * @return \Illuminate\Support\Collection
178
     */
179
    public function findWhereNotIn(array $where, $attributes = ['*'])
180
    {
181
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
182
            list($attribute, $values, $boolean) = array_pad($where, 3, null);
183
184
            $this->whereNotIn($attribute, $values, $boolean);
185
186
            return $this->prepareQuery($this->createModel())->get($attributes);
187
        });
188
    }
189
190
    /**
191
     * Create a new entity with the given attributes.
192
     *
193
     * @param array $attributes
194
     *
195
     * @return array
196
     */
197
    public function create(array $attributes = [])
198
    {
199
        // Create a new instance
200
        $instance = $this->createModel();
201
202
        // Fill instance with data
203
        $instance->fill($attributes);
204
205
        // Save the instance
206
        $created = $instance->save();
207
208
        // Fire the created event
209
        $this->getContainer('events')->fire($this->getRepositoryId().'.entity.created', [$this, $instance]);
210
211
        // Return instance
212
        return [
213
            $created,
214
            $instance,
215
        ];
216
    }
217
218
    /**
219
     * Update an entity with the given attributes.
220
     *
221
     * @param mixed $id
222
     * @param array $attributes
223
     *
224
     * @return array
225
     */
226
    public function update($id, array $attributes = [])
227
    {
228
        // Find the given instance
229
        $updated  = false;
230
        $instance = $id instanceof Model ? $id : $this->find($id);
231
232
        if ($instance) {
233
            // Fill instance with data
234
            $instance->fill($attributes);
235
236
            // Save the instance
237
            $updated = $instance->save();
238
239
            // Fire the updated event
240
            $this->getContainer('events')->fire($this->getRepositoryId().'.entity.updated', [$this, $instance]);
241
        }
242
243
        return [
244
            $updated,
245
            $instance,
246
        ];
247
    }
248
    
249
    /**
250
     * {@inheritDoc}
251
     */
252
    public function store($id, array $input)
253
    {
254
        return ! $id ? $this->create($input) : $this->update($id, $input);
255
    }
256
257
    /**
258
     * Delete an entity with the given id.
259
     *
260
     * @param mixed $id
261
     *
262
     * @return array
263
     */
264
    public function delete($id)
265
    {
266
        // Find the given instance
267
        $deleted  = false;
268
        $instance = $id instanceof Model ? $id : $this->find($id);
269
270
        if ($instance) {
271
            // Delete the instance
272
            $deleted = $instance->delete();
273
274
            // Fire the deleted event
275
            $this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleted', [$this, $instance]);
276
        }
277
278
        return [
279
            $deleted,
280
            $instance,
281
        ];
282
    }
283
}
284