Completed
Push — master ( 489a99...99d7d3 )
by Ryan
07:23
created

EloquentRepository::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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