Passed
Push — main ( 0416fa...c3f05b )
by Seth
01:34 queued 10s
created

EventIndexRequest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 50
rs 10
c 1
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRelatedModel() 0 4 1
A getUserId() 0 3 1
A rules() 0 9 1
A getLimit() 0 3 2
A passedValidation() 0 11 3
A getEndpointName() 0 3 1
1
<?php
2
3
namespace SaasReady\Http\Requests\Event;
4
5
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Validation\ValidationException;
0 ignored issues
show
Bug introduced by
The type Illuminate\Validation\ValidationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SaasReady\Http\Requests\BaseFormRequest;
8
9
class EventIndexRequest extends BaseFormRequest
10
{
11
    private ?Model $source = null;
12
13
    protected function getEndpointName(): string
14
    {
15
        return 'events.index';
16
    }
17
18
    public function rules(): array
19
    {
20
        return [
21
            'limit' => 'required|int|max:100',
22
            'page' => 'required|int|min:1',
23
            'source_type' => 'required|string',
24
            'source_id' => 'required|int',
25
            'user_id' => 'nullable|int',
26
            'load_related_model' => 'nullable|bool',
27
        ];
28
    }
29
30
    protected function passedValidation()
31
    {
32
        if (!class_exists($this->input('source_type'))) {
33
            throw ValidationException::withMessages([
34
                'source_type' => 'Source Type is not a valid Eloquent Class',
35
            ]);
36
        }
37
38
        if (!$this->getRelatedModel()) {
39
            throw ValidationException::withMessages([
40
                'source_id' => 'Source is invalid',
41
            ]);
42
        }
43
    }
44
45
    public function getLimit(): int
46
    {
47
        return $this->input('limit') ?: 10;
48
    }
49
50
    public function getUserId(): int
51
    {
52
        return $this->integer('user_id');
53
    }
54
55
    public function getRelatedModel(): ?Model
56
    {
57
        return $this->source
58
            ??= $this->input('source_type')::find($this->input('source_id'));
59
    }
60
}
61