Issues (81)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Repositories/EloquentRepository.php (18 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Repository\Repositories;
6
7
use Illuminate\Support\Arr;
8
use Illuminate\Pagination\Paginator;
9
use Illuminate\Database\Eloquent\Model;
10
use Rinvex\Repository\Exceptions\RepositoryException;
11
use Rinvex\Repository\Exceptions\EntityNotFoundException;
12
13
class EloquentRepository extends BaseRepository
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function createModel()
19
    {
20
        if (is_string($model = $this->getModel())) {
21
            if (! class_exists($class = '\\'.ltrim($model, '\\'))) {
22
                throw new RepositoryException("Class {$model} does NOT exist!");
23
            }
24
25
            $model = $this->getContainer()->make($class);
26
        }
27
28
        // Set the connection used by the model
29
        if (! empty($this->connection)) {
30
            $model = $model->setConnection($this->connection);
31
        }
32
33
        if (! $model instanceof Model) {
34
            throw new RepositoryException("Class {$model} must be an instance of \\Illuminate\\Database\\Eloquent\\Model");
35
        }
36
37
        return $model;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function find($id, $attributes = ['*'])
44
    {
45
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($id, $attributes) {
46
            return $this->prepareQuery($this->createModel())->find($id, $attributes);
47
        });
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function findOrFail($id, $attributes = ['*'])
54
    {
55
        $result = $this->find($id, $attributes);
56
57
        if (is_array($id)) {
58
            if (count($result) === count(array_unique($id))) {
59
                return $result;
60
            }
61
        } elseif (! is_null($result)) {
62
            return $result;
63
        }
64
65
        throw new EntityNotFoundException($this->getModel(), $id);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function findOrNew($id, $attributes = ['*'])
72
    {
73
        if (! is_null($entity = $this->find($id, $attributes))) {
74
            return $entity;
75
        }
76
77
        return $this->createModel();
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function findBy($attribute, $value, $attributes = ['*'])
84
    {
85
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($attribute, $value, $attributes) {
86
            return $this->prepareQuery($this->createModel())->where($attribute, '=', $value)->first($attributes);
87
        });
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function findFirst($attributes = ['*'])
94
    {
95
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($attributes) {
96
            return $this->prepareQuery($this->createModel())->first($attributes);
97
        });
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function findAll($attributes = ['*'])
104
    {
105
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($attributes) {
106
            return $this->prepareQuery($this->createModel())->get($attributes);
107
        });
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function paginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null)
114
    {
115
        $page = $page ?: Paginator::resolveCurrentPage($pageName);
116
117
        return $this->executeCallback(static::class, __FUNCTION__, array_merge(func_get_args(), compact('page')), function () use ($perPage, $attributes, $pageName, $page) {
118
            return $this->prepareQuery($this->createModel())->paginate($perPage, $attributes, $pageName, $page);
119
        });
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function simplePaginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null)
126
    {
127
        $page = $page ?: Paginator::resolveCurrentPage($pageName);
128
129
        return $this->executeCallback(static::class, __FUNCTION__, array_merge(func_get_args(), compact('page')), function () use ($perPage, $attributes, $pageName, $page) {
130
            return $this->prepareQuery($this->createModel())->simplePaginate($perPage, $attributes, $pageName, $page);
131
        });
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function findWhere(array $where, $attributes = ['*'])
138
    {
139
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
140
            [$attribute, $operator, $value, $boolean] = array_pad($where, 4, null);
0 ignored issues
show
The variable $attribute does not exist. Did you mean $attributes?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
The variable $operator does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $boolean does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
141
142
            $this->where($attribute, $operator, $value, $boolean);
0 ignored issues
show
The variable $attribute does not exist. Did you mean $attributes?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
143
144
            return $this->prepareQuery($this->createModel())->get($attributes);
145
        });
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function findWhereIn(array $where, $attributes = ['*'])
152
    {
153
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
154
            [$attribute, $values, $boolean, $not] = array_pad($where, 4, null);
0 ignored issues
show
The variable $attribute does not exist. Did you mean $attributes?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
The variable $values does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $boolean does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $not does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
155
156
            $this->whereIn($attribute, $values, $boolean, $not);
0 ignored issues
show
The variable $attribute does not exist. Did you mean $attributes?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
157
158
            return $this->prepareQuery($this->createModel())->get($attributes);
159
        });
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function findWhereNotIn(array $where, $attributes = ['*'])
166
    {
167
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
168
            [$attribute, $values, $boolean] = array_pad($where, 3, null);
0 ignored issues
show
The variable $attribute does not exist. Did you mean $attributes?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
The variable $values does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $boolean does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
169
170
            $this->whereNotIn($attribute, $values, $boolean);
0 ignored issues
show
The variable $attribute does not exist. Did you mean $attributes?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
171
172
            return $this->prepareQuery($this->createModel())->get($attributes);
173
        });
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function findWhereHas(array $where, $attributes = ['*'])
180
    {
181
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($where, $attributes) {
182
            [$relation, $callback, $operator, $count] = array_pad($where, 4, null);
0 ignored issues
show
The variable $relation does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $callback does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $operator does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $count does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
183
184
            $this->whereHas($relation, $callback, $operator, $count);
185
186
            return $this->prepareQuery($this->createModel())->get($attributes);
187
        });
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function create(array $attributes = [], bool $syncRelations = false)
194
    {
195
        // Create a new instance
196
        $entity = $this->createModel();
197
198
        // Fire the created event
199
        $this->getContainer('events')->dispatch($this->getRepositoryId().'.entity.creating', [$this, $entity]);
200
201
        // Extract relationships
202
        if ($syncRelations) {
203
            $relations = $this->extractRelations($entity, $attributes);
204
            Arr::forget($attributes, array_keys($relations));
205
        }
206
207
        // Fill instance with data
208
        $entity->fill($attributes);
209
210
        // Save the instance
211
        $created = $entity->save();
212
213
        // Sync relationships
214
        if ($syncRelations && isset($relations)) {
215
            $this->syncRelations($entity, $relations);
216
        }
217
218
        // Fire the created event
219
        $this->getContainer('events')->dispatch($this->getRepositoryId().'.entity.created', [$this, $entity]);
220
221
        // Return instance
222
        return $created ? $entity : $created;
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function update($id, array $attributes = [], bool $syncRelations = false)
229
    {
230
        $updated = false;
231
232
        // Find the given instance
233
        $entity = $id instanceof Model ? $id : $this->find($id);
234
235
        if ($entity) {
236
            // Fire the updated event
237
            $this->getContainer('events')->dispatch($this->getRepositoryId().'.entity.updating', [$this, $entity]);
238
239
            // Extract relationships
240
            if ($syncRelations) {
241
                $relations = $this->extractRelations($entity, $attributes);
242
                Arr::forget($attributes, array_keys($relations));
243
            }
244
245
            // Fill instance with data
246
            $entity->fill($attributes);
247
248
            //Check if we are updating attributes values
249
            $dirty = $entity->getDirty();
250
251
            // Update the instance
252
            $updated = $entity->save();
253
254
            // Sync relationships
255
            if ($syncRelations && isset($relations)) {
256
                $this->syncRelations($entity, $relations);
257
            }
258
259
            if (count($dirty) > 0) {
260
                // Fire the updated event
261
                $this->getContainer('events')->dispatch($this->getRepositoryId().'.entity.updated', [$this, $entity]);
262
            }
263
        }
264
265
        return $updated ? $entity : $updated;
266
    }
267
268
    /**
269
     * {@inheritdoc}
270
     */
271
    public function delete($id)
272
    {
273
        $deleted = false;
274
275
        // Find the given instance
276
        $entity = $id instanceof Model ? $id : $this->find($id);
277
278
        if ($entity) {
279
            // Fire the deleted event
280
            $this->getContainer('events')->dispatch($this->getRepositoryId().'.entity.deleting', [$this, $entity]);
281
282
            // Delete the instance
283
            $deleted = $entity->delete();
284
285
            // Fire the deleted event
286
            $this->getContainer('events')->dispatch($this->getRepositoryId().'.entity.deleted', [$this, $entity]);
287
        }
288
289
        return $deleted ? $entity : $deleted;
290
    }
291
292
    /**
293
     * {@inheritdoc}
294
     */
295
    public function restore($id)
296
    {
297
        $restored = false;
298
299
        // Find the given instance
300
        $entity = $id instanceof Model ? $id : $this->find($id);
301
302
        if ($entity) {
303
            // Fire the restoring event
304
            $this->getContainer('events')->dispatch($this->getRepositoryId().'.entity.restoring', [$this, $entity]);
305
306
            // Restore the instance
307
            $restored = $entity->restore();
308
309
            // Fire the restored event
310
            $this->getContainer('events')->dispatch($this->getRepositoryId().'.entity.restored', [$this, $entity]);
311
        }
312
313
        return $restored ? $entity : $restored;
314
    }
315
316
    /**
317
     * {@inheritdoc}
318
     */
319
    public function beginTransaction(): void
320
    {
321
        $this->getContainer('db')->beginTransaction();
322
    }
323
324
    /**
325
     * {@inheritdoc}
326
     */
327
    public function commit(): void
328
    {
329
        $this->getContainer('db')->commit();
330
    }
331
332
    /**
333
     * {@inheritdoc}
334
     */
335
    public function rollBack(): void
336
    {
337
        $this->getContainer('db')->rollBack();
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343
    public function count($columns = '*'): int
344
    {
345
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($columns) {
346
            return $this->prepareQuery($this->createModel())->count($columns);
347
        });
348
    }
349
350
    /**
351
     * {@inheritdoc}
352
     */
353
    public function min($column)
354
    {
355
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($column) {
356
            return $this->prepareQuery($this->createModel())->min($column);
357
        });
358
    }
359
360
    /**
361
     * {@inheritdoc}
362
     */
363
    public function max($column)
364
    {
365
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($column) {
366
            return $this->prepareQuery($this->createModel())->max($column);
367
        });
368
    }
369
370
    /**
371
     * {@inheritdoc}
372
     */
373
    public function avg($column)
374
    {
375
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($column) {
376
            return $this->prepareQuery($this->createModel())->avg($column);
377
        });
378
    }
379
380
    /**
381
     * {@inheritdoc}
382
     */
383
    public function sum($column)
384
    {
385
        return $this->executeCallback(static::class, __FUNCTION__, func_get_args(), function () use ($column) {
386
            return $this->prepareQuery($this->createModel())->sum($column);
387
        });
388
    }
389
390
    /**
391
     * Extract relationships.
392
     *
393
     * @param mixed $entity
394
     * @param array $attributes
395
     *
396
     * @return array
397
     */
398
    protected function extractRelations($entity, array $attributes): array
399
    {
400
        $relations = [];
401
        $potential = array_diff(array_keys($attributes), $entity->getFillable());
402
403
        array_walk($potential, function ($relation) use ($entity, $attributes, &$relations) {
404
            if (method_exists($entity, $relation)) {
405
                $relations[$relation] = [
406
                    'values' => $attributes[$relation],
407
                    'class' => get_class($entity->{$relation}()),
408
                ];
409
            }
410
        });
411
412
        return $relations;
413
    }
414
415
    /**
416
     * Sync relationships.
417
     *
418
     * @param mixed $entity
419
     * @param array $relations
420
     * @param bool  $detaching
421
     *
422
     * @return void
423
     */
424
    protected function syncRelations($entity, array $relations, $detaching = true): void
425
    {
426
        foreach ($relations as $method => $relation) {
427
            switch ($relation['class']) {
428
                case 'Illuminate\Database\Eloquent\Relations\BelongsToMany':
429
                default:
430
                    $entity->{$method}()->sync((array) $relation['values'], $detaching);
431
                    break;
432
            }
433
        }
434
    }
435
}
436