Completed
Push — master ( c0b066...2cb525 )
by Abdelrahman
03:51 queued 01:59
created

EloquentRepository::createModel()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 8
nc 5
nop 0
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|null $perPage
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) {
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) = array_pad($where, 4, null);
139
140
            $this->where($attribute, $operator, $value, $boolean);
141
142
            return $this->prepareQuery($this->createModel())->get($attributes);
143
        });
144
    }
145
146
    /**
147
     * Find all entities matching whereIn conditions.
148
     *
149
     * @param array $where
150
     * @param array $attributes
151
     *
152
     * @return \Illuminate\Support\Collection
153
     */
154
    public function findWhereIn(array $where, $attributes = ['*'])
155
    {
156
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
157
            list($attribute, $values, $boolean, $not) = array_pad($where, 4, null);
158
159
            $this->whereIn($attribute, $values, $boolean, $not);
160
161
            return $this->prepareQuery($this->createModel())->get($attributes);
162
        });
163
    }
164
165
    /**
166
     * Find all entities matching whereNotIn conditions.
167
     *
168
     * @param array $where
169
     * @param array $attributes
170
     *
171
     * @return \Illuminate\Support\Collection
172
     */
173
    public function findWhereNotIn(array $where, $attributes = ['*'])
174
    {
175
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
176
            list($attribute, $values, $boolean) = array_pad($where, 3, null);
177
178
            $this->whereNotIn($attribute, $values, $boolean);
179
180
            return $this->prepareQuery($this->createModel())->get($attributes);
181
        });
182
    }
183
184
    /**
185
     * Create a new entity with the given attributes.
186
     *
187
     * @param array $attributes
188
     *
189
     * @return array
190
     */
191
    public function create(array $attributes = [])
192
    {
193
        // Create a new instance
194
        $instance = $this->createModel();
195
196
        // Fill instance with data
197
        $instance->fill($attributes);
198
199
        // Save the instance
200
        $created = $instance->save();
201
202
        // Fire the created event
203
        $this->getContainer('events')->fire($this->getRepositoryId().'.entity.created', [$this, $instance]);
204
205
        // Return instance
206
        return [
207
            $created,
208
            $instance,
209
        ];
210
    }
211
212
    /**
213
     * Update an entity with the given attributes.
214
     *
215
     * @param mixed $id
216
     * @param array $attributes
217
     *
218
     * @return array
219
     */
220
    public function update($id, array $attributes = [])
221
    {
222
        // Find the given instance
223
        $updated  = false;
224
        $instance = $id instanceof Model ? $id : $this->find($id);
225
226
        if ($instance) {
227
            // Fill instance with data
228
            $instance->fill($attributes);
229
230
            // Save the instance
231
            $updated = $instance->save();
232
233
            // Fire the updated event
234
            $this->getContainer('events')->fire($this->getRepositoryId().'.entity.updated', [$this, $instance]);
235
        }
236
237
        return [
238
            $updated,
239
            $instance,
240
        ];
241
    }
242
243
    /**
244
     * Delete an entity with the given id.
245
     *
246
     * @param mixed $id
247
     *
248
     * @return array
249
     */
250
    public function delete($id)
251
    {
252
        // Find the given instance
253
        $deleted  = false;
254
        $instance = $id instanceof Model ? $id : $this->find($id);
255
256
        if ($instance) {
257
            // Delete the instance
258
            $deleted = $instance->delete();
259
260
            // Fire the deleted event
261
            $this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleted', [$this, $instance]);
262
        }
263
264
        return [
265
            $deleted,
266
            $instance,
267
        ];
268
    }
269
}
270