Completed
Push — master ( 5e4dcc...80763c )
by Ryan
08:06
created

EloquentRepository::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Anomaly\Streams\Platform\Model;
2
3
use Anomaly\Streams\Platform\Model\Contract\EloquentRepositoryInterface;
4
use Anomaly\Streams\Platform\Traits\FiresCallbacks;
5
use Anomaly\Streams\Platform\Traits\Hookable;
6
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
7
use Illuminate\Database\Eloquent\Builder;
8
9
/**
10
 * Class EloquentRepository
11
 *
12
 * @link          http://anomaly.is/streams-platform
13
 * @author        AnomalyLabs, Inc. <[email protected]>
14
 * @author        Ryan Thompson <[email protected]>
15
 * @package       Anomaly\Streams\Platform\Model
16
 */
17
class EloquentRepository implements EloquentRepositoryInterface
18
{
19
20
    use FiresCallbacks;
21
    use Hookable;
22
23
    /**
24
     * Return all records.
25
     *
26
     * @return EloquentCollection
27
     */
28
    public function all()
29
    {
30
        return $this->model->all();
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
    }
32
33
    /**
34
     * Find a record by it's ID.
35
     *
36
     * @param $id
37
     * @return EloquentModel
38
     */
39
    public function find($id)
40
    {
41
        return $this->model->find($id);
42
    }
43
44
    /**
45
     * Find all records by IDs.
46
     *
47
     * @param array $ids
48
     * @return EloquentCollection
49
     */
50
    public function findAll(array $ids)
51
    {
52
        return $this->model->whereIn('id', $ids)->get();
53
    }
54
55
    /**
56
     * Find a trashed record by it's ID.
57
     *
58
     * @param $id
59
     * @return null|EloquentModel
60
     */
61
    public function findTrashed($id)
62
    {
63
        return $this->model
64
            ->onlyTrashed()
65
            ->orderBy('id', 'ASC')
66
            ->where('id', $id)
67
            ->first();
68
    }
69
70
    /**
71
     * Create a new record.
72
     *
73
     * @param array $attributes
74
     * @return EloquentModel
75
     */
76
    public function create(array $attributes)
77
    {
78
        return $this->model->create($attributes);
79
    }
80
81
    /**
82
     * Return a new query builder.
83
     *
84
     * @return Builder
85
     */
86
    public function newQuery()
87
    {
88
        return $this->model->newQuery();
89
    }
90
91
    /**
92
     * Return a new instance.
93
     *
94
     * @return EloquentModel
95
     */
96
    public function newInstance()
97
    {
98
        return $this->model->newInstance();
99
    }
100
101
    /**
102
     * Count all records.
103
     *
104
     * @return int
105
     */
106
    public function count()
107
    {
108
        return $this->model->count();
109
    }
110
111
    /**
112
     * Return a paginated collection.
113
     *
114
     * @param array $parameters
115
     * @return LengthAwarePaginator
116
     */
117
    public function paginate(array $parameters = [])
118
    {
119
        $paginator = array_pull($parameters, 'paginator');
120
        $perPage   = array_pull($parameters, 'per_page', config('streams::system.per_page', 15));
121
122
        /* @var Builder $query */
123
        $query = $this->model->newQuery();
124
125
        /**
126
         * First apply any desired scope.
127
         */
128
        if ($scope = array_pull($parameters, 'scope')) {
129
            call_user_func([$query, camel_case($scope)], array_pull($parameters, 'scope_arguments', []));
130
        }
131
132
        /**
133
         * Lastly we need to loop through all of the
134
         * parameters and assume the rest are methods
135
         * to call on the query builder.
136
         */
137
        foreach ($parameters as $method => $arguments) {
138
139
            $method = camel_case($method);
140
141
            if (in_array($method, ['update', 'delete'])) {
142
                continue;
143
            }
144
145
            if (is_array($arguments)) {
146
                call_user_func_array([$query, $method], $arguments);
147
            } else {
148
                call_user_func_array([$query, $method], [$arguments]);
149
            }
150
        }
151
152
        if ($paginator === 'simple') {
153
            $pagination = $query->simplePaginate($perPage);
0 ignored issues
show
Bug Compatibility introduced by
The expression $query->simplePaginate($perPage); of type Illuminate\Pagination\Paginator adds the type Illuminate\Pagination\Paginator to the return on line 158 which is incompatible with the return type declared by the interface Anomaly\Streams\Platform...toryInterface::paginate of type Illuminate\Contracts\Pag...on\LengthAwarePaginator.
Loading history...
154
        } else {
155
            $pagination = $query->paginate($perPage);
156
        }
157
158
        return $pagination;
159
    }
160
161
    /**
162
     * Save a record.
163
     *
164
     * @param EloquentModel $entry
165
     * @return bool
166
     */
167
    public function save(EloquentModel $entry)
168
    {
169
        return $entry->save();
170
    }
171
172
    /**
173
     * Update multiple records.
174
     *
175
     * @param array $attributes
176
     * @return bool
177
     */
178
    public function update(array $attributes = [])
179
    {
180
        return $this->model->update($attributes);
181
    }
182
183
    /**
184
     * Delete a record.
185
     *
186
     * @param EloquentModel $entry
187
     * @return bool
188
     */
189
    public function delete(EloquentModel $entry)
190
    {
191
        return $entry->delete();
192
    }
193
194
    /**
195
     * Force delete a record.
196
     *
197
     * @param EloquentModel $entry
198
     * @return bool
199
     */
200
    public function forceDelete(EloquentModel $entry)
201
    {
202
        $entry->forceDelete();
203
204
        /**
205
         * If we were not able to force delete
206
         */
207
        return !$entry->exists;
208
    }
209
210
    /**
211
     * Restore a trashed record.
212
     *
213
     * @param EloquentModel $entry
214
     * @return bool
215
     */
216
    public function restore(EloquentModel $entry)
217
    {
218
        return $entry->restore();
0 ignored issues
show
Documentation Bug introduced by
The method restore does not exist on object<Anomaly\Streams\P...rm\Model\EloquentModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
219
    }
220
221
    /**
222
     * Truncate the entries.
223
     *
224
     * @return $this
225
     */
226
    public function truncate()
227
    {
228
        $this->model->flushCache();
229
230
        foreach ($this->model->all() as $entry) {
231
            $this->delete($entry);
232
        }
233
234
        $this->model->truncate(); // Clear trash
235
236
        if ($this->model->isTranslatable() && $translation = $this->model->getTranslationModel()) {
237
238
            $translation->flushCache();
239
240
            foreach ($translation->all() as $entry) {
241
                $this->delete($entry);
242
            }
243
244
            $translation->truncate(); // Clear trash
245
        }
246
247
        return $this;
248
    }
249
250
    /**
251
     * Set the model.
252
     *
253
     * @param EloquentModel $model
254
     * @return $this
255
     */
256
    public function setModel(EloquentModel $model)
257
    {
258
        $this->model = $model;
259
260
        return $this;
261
    }
262
263
    /**
264
     * Get the model.
265
     *
266
     * @return EloquentModel
267
     */
268
    public function getModel()
269
    {
270
        return $this->model;
271
    }
272
273
    /**
274
     * Pipe non-existing calls through hooks.
275
     *
276
     * @param $method
277
     * @param $parameters
278
     * @return mixed|null
279
     */
280
    public function __call($method, $parameters)
281
    {
282
        if ($this->hasHook($hook = snake_case($method))) {
283
            return $this->call($hook, $parameters);
284
        }
285
286
        return null;
287
    }
288
}
289