Completed
Push — master ( 7456c2...7265be )
by Ryan
07:55
created

EloquentRepository::findAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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 instance.
83
     *
84
     * @return EloquentModel
85
     */
86
    public function newInstance()
87
    {
88
        return $this->model->newInstance();
89
    }
90
91
    /**
92
     * Count all records.
93
     *
94
     * @return int
95
     */
96
    public function count()
97
    {
98
        return $this->model->count();
99
    }
100
101
    /**
102
     * Return a paginated collection.
103
     *
104
     * @param array $parameters
105
     * @return LengthAwarePaginator
106
     */
107
    public function paginate(array $parameters = [])
108
    {
109
        $paginator = array_pull($parameters, 'paginator');
110
        $perPage   = array_pull($parameters, 'per_page', config('streams::system.per_page', 15));
111
112
        /* @var Builder $query */
113
        $query = $this->model->newQuery();
114
115
        /**
116
         * First apply any desired scope.
117
         */
118
        if ($scope = array_pull($parameters, 'scope')) {
119
            call_user_func([$query, camel_case($scope)], array_pull($parameters, 'scope_arguments', []));
120
        }
121
122
        /**
123
         * Lastly we need to loop through all of the
124
         * parameters and assume the rest are methods
125
         * to call on the query builder.
126
         */
127
        foreach ($parameters as $method => $arguments) {
128
129
            $method = camel_case($method);
130
131
            if (in_array($method, ['update', 'delete'])) {
132
                continue;
133
            }
134
135
            if (is_array($arguments)) {
136
                call_user_func_array([$query, $method], $arguments);
137
            } else {
138
                call_user_func_array([$query, $method], [$arguments]);
139
            }
140
        }
141
142
        if ($paginator === 'simple') {
143
            $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 148 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...
144
        } else {
145
            $pagination = $query->paginate($perPage);
146
        }
147
148
        return $pagination;
149
    }
150
151
    /**
152
     * Save a record.
153
     *
154
     * @param EloquentModel $entry
155
     * @return bool
156
     */
157
    public function save(EloquentModel $entry)
158
    {
159
        return $entry->save();
160
    }
161
162
    /**
163
     * Update multiple records.
164
     *
165
     * @param array $attributes
166
     * @return bool
167
     */
168
    public function update(array $attributes = [])
169
    {
170
        return $this->model->update($attributes);
171
    }
172
173
    /**
174
     * Delete a record.
175
     *
176
     * @param EloquentModel $entry
177
     * @return bool
178
     */
179
    public function delete(EloquentModel $entry)
180
    {
181
        return $entry->delete();
182
    }
183
184
    /**
185
     * Force delete a record.
186
     *
187
     * @param EloquentModel $entry
188
     * @return bool
189
     */
190
    public function forceDelete(EloquentModel $entry)
191
    {
192
        $entry->forceDelete();
193
194
        return true;
195
    }
196
197
    /**
198
     * Restore a trashed record.
199
     *
200
     * @param EloquentModel $entry
201
     * @return bool
202
     */
203
    public function restore(EloquentModel $entry)
204
    {
205
        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...
206
    }
207
208
    /**
209
     * Truncate the entries.
210
     *
211
     * @return $this
212
     */
213
    public function truncate()
214
    {
215
        $this->model->flushCache();
216
217
        foreach ($this->model->all() as $entry) {
218
            $this->delete($entry);
219
        }
220
221
        $this->model->truncate(); // Clear trash
222
223
        if ($this->model->isTranslatable() && $translation = $this->model->getTranslationModel()) {
224
225
            $translation->flushCache();
226
227
            foreach ($translation->all() as $entry) {
228
                $this->delete($entry);
229
            }
230
231
            $translation->truncate(); // Clear trash
232
        }
233
234
        return $this;
235
    }
236
237
    /**
238
     * Set the model.
239
     *
240
     * @param EloquentModel $model
241
     * @return $this
242
     */
243
    public function setModel(EloquentModel $model)
244
    {
245
        $this->model = $model;
246
247
        return $this;
248
    }
249
250
    /**
251
     * Get the model.
252
     *
253
     * @return EloquentModel
254
     */
255
    public function getModel()
256
    {
257
        return $this->model;
258
    }
259
260
    /**
261
     * Pipe non-existing calls through hooks.
262
     *
263
     * @param $method
264
     * @param $parameters
265
     * @return mixed|null
266
     */
267
    public function __call($method, $parameters)
268
    {
269
        if ($this->hasHook($hook = snake_case($method))) {
270
            return $this->call($hook, $parameters);
271
        }
272
273
        return null;
274
    }
275
}
276