ResourceRequest::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Oscer\Cms\Backend\Http\Requests;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Foundation\Http\FormRequest;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
9
class ResourceRequest extends FormRequest
10
{
11
    public function authorize()
12
    {
13
        return true;
14
    }
15
16
    public function rules()
17
    {
18
        return [];
19
    }
20
21
    /**
22
     * Retrieves the resource class name based on the route parameter.
23
     */
24
    public function getResource(): string
25
    {
26
        $resources = config('cms.resources');
27
28
        // Return a 404 response if the resource is not found
29
        if (! array_key_exists($this->route('resource'), $resources)) {
30
            throw new NotFoundHttpException('resource not found');
31
        }
32
33
        return $resources[$this->route('resource')];
34
    }
35
36
    /**
37
     * Returns the string representation of the underlying model.
38
     */
39
    public function getResourceModel(): string
40
    {
41
        $resource = $this->getResource();
42
43
        return $resource::$model;
44
    }
45
46
    /**
47
     * Creates a new instance of the underlying model.
48
     */
49
    public function newResourceModel(): Model
50
    {
51
        $model = $this->getResourceModel();
52
53
        return new $model();
54
    }
55
56
    public function identifier()
57
    {
58
        return $this->route('id');
59
    }
60
}
61