Completed
Push — master ( 38efb8...5cc558 )
by Ryan
05:49
created

EloquentRepository::guard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Model;
2
3
use Anomaly\Streams\Platform\Entry\EntryModel;
4
use Anomaly\Streams\Platform\Model\Contract\EloquentRepositoryInterface;
5
use Anomaly\Streams\Platform\Traits\FiresCallbacks;
6
use Anomaly\Streams\Platform\Traits\Hookable;
7
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8
use Illuminate\Database\Eloquent\Builder;
9
10
/**
11
 * Class EloquentRepository
12
 *
13
 * @property EloquentModel|EntryModel $model
14
 *
15
 * @link   http://pyrocms.com/
16
 * @author PyroCMS, Inc. <[email protected]>
17
 * @author Ryan Thompson <[email protected]>
18
 */
19
class EloquentRepository implements EloquentRepositoryInterface
20
{
21
22
    use FiresCallbacks;
23
    use Hookable;
24
25
    /**
26
     * Return all records.
27
     *
28
     * @return EloquentCollection
29
     */
30
    public function all()
31
    {
32
        return $this->model->all();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->model->all(); of type Illuminate\Database\Eloq...base\Eloquent\Builder[] adds the type Illuminate\Database\Eloquent\Builder[] to the return on line 32 which is incompatible with the return type declared by the interface Anomaly\Streams\Platform...epositoryInterface::all of type Anomaly\Streams\Platform\Model\EloquentCollection.
Loading history...
33
    }
34
35
    /**
36
     * Find a record by it's ID.
37
     *
38
     * @param $id
39
     * @return EloquentModel
40
     */
41
    public function find($id)
42
    {
43
        return $this->model->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find 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...
44
    }
45
46
    /**
47
     * Find all records by IDs.
48
     *
49
     * @param  array $ids
50
     * @return EloquentCollection
51
     */
52
    public function findAll(array $ids)
53
    {
54
        return $this->model->whereIn('id', $ids)->get();
0 ignored issues
show
Documentation Bug introduced by
The method whereIn 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...
55
    }
56
57
    /**
58
     * Find a trashed record by it's ID.
59
     *
60
     * @param $id
61
     * @return null|EloquentModel
62
     */
63
    public function findTrashed($id)
64
    {
65
        return $this->model
0 ignored issues
show
Bug introduced by
The method onlyTrashed() does not exist on Anomaly\Streams\Platform\Model\EloquentModel. Did you maybe mean trashed()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
66
            ->onlyTrashed()
67
            ->orderBy('id', 'ASC')
68
            ->where('id', $id)
69
            ->first();
70
    }
71
72
    /**
73
     * Create a new record.
74
     *
75
     * @param  array $attributes
76
     * @return EloquentModel
77
     */
78
    public function create(array $attributes)
79
    {
80
        return $this->model->create($attributes);
81
    }
82
83
    /**
84
     * Return a new query builder.
85
     *
86
     * @return Builder
87
     */
88
    public function newQuery()
89
    {
90
        return $this->model->newQuery();
91
    }
92
93
    /**
94
     * Return a new instance.
95
     *
96
     * @param array $attributes
97
     * @return EloquentModel
98
     */
99
    public function newInstance(array $attributes = [])
100
    {
101
        return $this->model->newInstance($attributes);
102
    }
103
104
    /**
105
     * Count all records.
106
     *
107
     * @return int
108
     */
109
    public function count()
110
    {
111
        return $this->model->count();
0 ignored issues
show
Documentation Bug introduced by
The method count 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...
112
    }
113
114
    /**
115
     * Return a paginated collection.
116
     *
117
     * @param  array $parameters
118
     * @return LengthAwarePaginator
119
     */
120
    public function paginate(array $parameters = [])
121
    {
122
        $paginator = array_pull($parameters, 'paginator');
123
        $perPage   = array_pull($parameters, 'per_page', config('streams::system.per_page', 15));
124
125
        /* @var Builder $query */
126
        $query = $this->model->newQuery();
127
128
        /*
129
         * First apply any desired scope.
130
         */
131
        if ($scope = array_pull($parameters, 'scope')) {
132
            call_user_func([$query, camel_case($scope)], array_pull($parameters, 'scope_arguments', []));
133
        }
134
135
        /*
136
         * Lastly we need to loop through all of the
137
         * parameters and assume the rest are methods
138
         * to call on the query builder.
139
         */
140
        foreach ($parameters as $method => $arguments) {
141
            $method = camel_case($method);
142
143
            if (in_array($method, ['update', 'delete'])) {
144
                continue;
145
            }
146
147
            if (is_array($arguments)) {
148
                call_user_func_array([$query, $method], $arguments);
149
            } else {
150
                call_user_func_array([$query, $method], [$arguments]);
151
            }
152
        }
153
154
        if ($paginator === 'simple') {
155
            $pagination = $query->simplePaginate($perPage);
156
        } else {
157
            $pagination = $query->paginate($perPage);
158
        }
159
160
        return $pagination;
161
    }
162
163
    /**
164
     * Save a record.
165
     *
166
     * @param  EloquentModel $entry
167
     * @return bool
168
     */
169
    public function save(EloquentModel $entry)
170
    {
171
        return $entry->save();
172
    }
173
174
    /**
175
     * Update multiple records.
176
     *
177
     * @param  array $attributes
178
     * @return bool
179
     */
180
    public function update(array $attributes = [])
181
    {
182
        return $this->model->update($attributes);
183
    }
184
185
    /**
186
     * Delete a record.
187
     *
188
     * @param  EloquentModel $entry
189
     * @return bool
190
     */
191
    public function delete(EloquentModel $entry)
192
    {
193
        return $entry->delete();
194
    }
195
196
    /**
197
     * Force delete a record.
198
     *
199
     * @param  EloquentModel $entry
200
     * @return bool
201
     */
202
    public function forceDelete(EloquentModel $entry)
203
    {
204
        $entry->forceDelete();
205
206
        /*
207
         * If we were not able to force delete
208
         */
209
210
        return !$entry->exists;
211
    }
212
213
    /**
214
     * Restore a trashed record.
215
     *
216
     * @param  EloquentModel $entry
217
     * @return bool
218
     */
219
    public function restore(EloquentModel $entry)
220
    {
221
        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...
222
    }
223
224
    /**
225
     * Truncate the entries.
226
     *
227
     * @return $this
228
     */
229
    public function truncate()
230
    {
231
        $this->model->flushCache();
232
233
        foreach ($this->model->all() as $entry) {
234
            $this->delete($entry);
235
        }
236
237
        $this->model->truncate(); // Clear trash
0 ignored issues
show
Documentation Bug introduced by
The method truncate 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...
238
239
        if ($this->model->isTranslatable() && $translation = $this->model->getTranslationModel()) {
240
            $translation->flushCache();
241
242
            foreach ($translation->all() as $entry) {
243
                $this->delete($entry);
244
            }
245
246
            $translation->truncate(); // Clear trash
0 ignored issues
show
Documentation Bug introduced by
The method truncate 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...
247
        }
248
249
        return $this;
250
    }
251
252
    /**
253
     * Guard the model.
254
     *
255
     * @return $this
256
     */
257
    public function guard()
258
    {
259
        $this->model->reguard();
260
261
        return $this;
262
    }
263
264
    /**
265
     * Unguard the model.
266
     *
267
     * @return $this
268
     */
269
    public function unguard()
270
    {
271
        $this->model->unguard();
272
273
        return $this;
274
    }
275
276
    /**
277
     * Set the model.
278
     *
279
     * @param  EloquentModel $model
280
     * @return $this
281
     */
282
    public function setModel(EloquentModel $model)
283
    {
284
        $this->model = $model;
285
286
        return $this;
287
    }
288
289
    /**
290
     * Get the model.
291
     *
292
     * @return EloquentModel
293
     */
294
    public function getModel()
295
    {
296
        return $this->model;
297
    }
298
299
    /**
300
     * Pipe non-existing calls through hooks.
301
     *
302
     * @param $method
303
     * @param $parameters
304
     * @return mixed|null
305
     */
306 View Code Duplication
    public function __call($method, $parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
307
    {
308
        if ($this->hasHook($hook = snake_case($method))) {
309
            return $this->call($hook, $parameters);
310
        }
311
312
        return null;
313
    }
314
}
315