Completed
Push — develop ( 301c4e...f79c61 )
by Abdelrahman
05:11
created

EloquentRepository::retrieveModel()   B

Complexity

Conditions 6
Paths 15

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 6
eloc 10
nc 15
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 Rinvex\Repository\Exceptions\RepositoryException;
20
21
class EloquentRepository extends BaseRepository
22
{
23
    /**
24
     * Create a new repository model instance.
25
     *
26
     * @throws \Rinvex\Repository\Exceptions\RepositoryException
27
     *
28
     * @return \Illuminate\Database\Eloquent\Model
29
     */
30
    public function createModel()
31
    {
32
        if (is_string($model = $this->getModel())) {
33
            if (! class_exists($class = '\\'.ltrim($model, '\\'))) {
34
                throw new RepositoryException("Class {$model} does NOT exist!");
35
            }
36
37
            $model = $this->getContainer()->make($class);
38
        }
39
40
        if (! $model instanceof Model) {
41
            throw new RepositoryException("Class {$model} must be an instance of \\Illuminate\\Database\\Eloquent\\Model");
42
        }
43
44
        return $model;
45
    }
46
47
    /**
48
     * Find an entity by it's primary key.
49
     *
50
     * @param int   $id
51
     * @param array $attributes
52
     *
53
     * @return \Illuminate\Database\Eloquent\Model
54
     */
55
    public function find($id, $attributes = ['*'])
56
    {
57
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($id, $attributes) {
58
            return $this->prepareQuery($this->createModel())->find($id, $attributes);
59
        });
60
    }
61
62
    /**
63
     * Find an entity by one of it's attributes.
64
     *
65
     * @param string $attribute
66
     * @param string $value
67
     * @param array  $attributes
68
     *
69
     * @return \Illuminate\Database\Eloquent\Model
70
     */
71
    public function findBy($attribute, $value, $attributes = ['*'])
72
    {
73
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attribute, $value, $attributes) {
74
            return $this->prepareQuery($this->createModel())->where($attribute, '=', $value)->first($attributes);
75
        });
76
    }
77
78
    /**
79
     * Find all entities.
80
     *
81
     * @param array $attributes
82
     *
83
     * @return \Illuminate\Support\Collection
84
     */
85
    public function findAll($attributes = ['*'])
86
    {
87
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attributes) {
88
            return $this->prepareQuery($this->createModel())->get($attributes);
89
        });
90
    }
91
92
    /**
93
     * Paginate all entities.
94
     *
95
     * @param int|null $perPage
96
     * @param array    $attributes
97
     * @param string   $pageName
98
     * @param int|null $page
99
     *
100
     * @throws \InvalidArgumentException
101
     *
102
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
103
     */
104
    public function paginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null)
105
    {
106
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($perPage, $attributes, $pageName, $page) {
107
            return $this->prepareQuery($this->createModel())->paginate($perPage, $attributes, $pageName, $page);
108
        });
109
    }
110
111
    /**
112
     * Paginate all entities into a simple paginator.
113
     *
114
     * @param  int    $perPage
0 ignored issues
show
Documentation introduced by
Should the type for parameter $perPage not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
115
     * @param  array  $attributes
116
     * @param  string $pageName
117
     *
118
     * @return \Illuminate\Contracts\Pagination\Paginator
119
     */
120
    public function simplePaginate($perPage = null, $attributes = ['*'], $pageName = 'page')
121
    {
122
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($perPage, $attributes, $pageName) {
1 ignored issue
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 141 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...
123
            return $this->prepareQuery($this->createModel())->simplePaginate($perPage, $attributes, $pageName);
124
        });
125
    }
126
127
    /**
128
     * Find all entities matching where conditions.
129
     *
130
     * @param array $where
131
     * @param array $attributes
132
     *
133
     * @return \Illuminate\Support\Collection
134
     */
135
    public function findWhere(array $where, $attributes = ['*'])
136
    {
137
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
138
            list($attribute, $operator, $value, $boolean) = $where;
139
            $this->where($attribute, $operator, $value, $boolean);
140
141
            return $this->prepareQuery($this->createModel())->get($attributes);
142
        });
143
    }
144
145
    /**
146
     * Find all entities matching whereIn conditions.
147
     *
148
     * @param array  $where
149
     * @param array  $attributes
150
     *
151
     * @return \Illuminate\Support\Collection
152
     */
153
    public function findWhereIn(array $where, $attributes = ['*'])
154
    {
155
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
156
            list($attribute, $values, $boolean, $not) = $where;
157
            $this->whereIn($attribute, $values, $boolean, $not);
158
159
            return $this->prepareQuery($this->createModel())->get($attributes);
160
        });
161
    }
162
163
    /**
164
     * Find all entities matching whereNotIn conditions.
165
     *
166
     * @param array  $where
167
     * @param array  $attributes
168
     *
169
     * @return \Illuminate\Support\Collection
170
     */
171
    public function findWhereNotIn(array $where, $attributes = ['*'])
172
    {
173
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
1 ignored issue
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...
174
            list($attribute, $values, $boolean) = $where;
175
            $this->whereNotIn($attribute, $values, $boolean);
176
177
            return $this->prepareQuery($this->createModel())->get($attributes);
178
        });
179
    }
180
181
    /**
182
     * Create a new entity with the given attributes.
183
     *
184
     * @param array $attributes
185
     *
186
     * @return array
1 ignored issue
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<boolean|Model>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
187
     */
188
    public function create(array $attributes = [])
189
    {
190
        // Create a new instance
191
        $instance = $this->createModel();
192
193
        // Fill instance with data
194
        $instance->fill($attributes);
195
196
        // Save the instance
197
        $created = $instance->save();
198
199
        // Fire the created event
200
        $this->getContainer('events')->fire($this->getRepositoryId().'.entity.created', [$this, $instance]);
201
202
        // Return instance
203
        return [
204
            $created,
205
            $instance,
206
        ];
207
    }
208
209
    /**
210
     * Update an entity with the given attributes.
211
     *
212
     * @param mixed $id
213
     * @param array $attributes
214
     *
215
     * @return array
1 ignored issue
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<boolean|Model>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
216
     */
217
    public function update($id, array $attributes = [])
218
    {
219
        // Find the given instance
220
        $updated  = false;
221
        $instance = $id instanceof Model ? $id : $this->find($id);
222
223
        if ($instance) {
224
            // Fill instance with data
225
            $instance->fill($attributes);
226
227
            // Save the instance
228
            $updated = $instance->save();
229
230
            // Fire the updated event
231
            $this->getContainer('events')->fire($this->getRepositoryId().'.entity.updated', [$this, $instance]);
232
        }
233
234
        return [
235
            $updated,
236
            $instance,
237
        ];
238
    }
239
240
    /**
241
     * Delete an entity with the given id.
242
     *
243
     * @param mixed $id
244
     *
245
     * @return array
1 ignored issue
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<boolean|null|Model>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
246
     */
247
    public function delete($id)
248
    {
249
        // Find the given instance
250
        $deleted  = false;
251
        $instance = $id instanceof Model ? $id : $this->find($id);
252
253
        if ($instance) {
254
            // Delete the instance
255
            $deleted = $instance->delete();
256
257
            // Fire the deleted event
258
            $this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleted', [$this, $instance]);
259
        }
260
261
        return [
262
            $deleted,
263
            $instance,
264
        ];
265
    }
266
}
267