DynamicSettingsController::store()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace SaasReady\Http\Controllers;
4
5
use Illuminate\Http\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\JsonResponse 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\Routing\Controller;
0 ignored issues
show
Bug introduced by
The type Illuminate\Routing\Controller 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 Illuminate\Support\Facades\Event;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Event 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...
8
use SaasReady\Events\DynamicSetting\DynamicSettingCreated;
9
use SaasReady\Events\DynamicSetting\DynamicSettingDeleted;
10
use SaasReady\Events\DynamicSetting\DynamicSettingUpdated;
11
use SaasReady\Http\Requests\DynamicSetting\DynamicSettingDestroyRequest;
12
use SaasReady\Http\Requests\DynamicSetting\DynamicSettingIndexRequest;
13
use SaasReady\Http\Requests\DynamicSetting\DynamicSettingShowRequest;
14
use SaasReady\Http\Requests\DynamicSetting\DynamicSettingStoreRequest;
15
use SaasReady\Http\Requests\DynamicSetting\DynamicSettingUpdateRequest;
16
use SaasReady\Http\Responses\DynamicSettingResource;
17
use SaasReady\Models\DynamicSetting;
0 ignored issues
show
Bug introduced by
The type SaasReady\Models\DynamicSetting 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...
18
19
class DynamicSettingsController extends Controller
20
{
21
    public function index(DynamicSettingIndexRequest $request): JsonResponse
22
    {
23
        $dynamicSettings = DynamicSetting::orderBy('created_at', 'DESC')
24
            ->when($request->input('source_type'), fn ($q) => $q->where([
25
                'model_type' => $request->input('source_type'),
26
            ]))
27
            ->paginate($request->integer('limit') ?: 10);
28
29
        return DynamicSettingResource::collection($dynamicSettings)->response();
30
    }
31
32
    public function show(
33
        DynamicSettingShowRequest $request,
34
        DynamicSetting $dynamicSetting
35
    ): JsonResponse {
36
        return (new DynamicSettingResource($dynamicSetting))->response();
37
    }
38
39
    public function store(DynamicSettingStoreRequest $request): JsonResponse
40
    {
41
        $relatedModel = $request->getRelatedModel();
42
43
        $dynamicSetting = DynamicSetting::create([
44
            ...$request->validated(),
45
            'model_id' => $relatedModel->getKey(),
46
            'model_type' => $relatedModel->getMorphClass(),
47
        ]);
48
49
        Event::dispatch(new DynamicSettingCreated($dynamicSetting));
50
51
        return new JsonResponse([
52
            'uuid' => $dynamicSetting->uuid,
53
        ], 201);
54
    }
55
56
    public function update(
57
        DynamicSettingUpdateRequest $request,
58
        DynamicSetting $dynamicSetting
59
    ): JsonResponse {
60
        $relatedModel = $request->getRelatedModel();
61
62
        $dynamicSetting->update([
63
            ...$request->validated(),
64
            'model_id' => $relatedModel?->getKey() ?? $dynamicSetting->model_id,
65
            'model_type' => $relatedModel?->getMorphClass() ?? $dynamicSetting->model_type,
66
        ]);
67
68
        Event::dispatch(new DynamicSettingUpdated($dynamicSetting));
69
70
        return new JsonResponse([
71
            'uuid' => $dynamicSetting->uuid,
72
        ]);
73
    }
74
75
    public function destroy(
76
        DynamicSettingDestroyRequest $request,
77
        DynamicSetting $dynamicSetting
78
    ): JsonResponse {
79
        if ($dynamicSetting->model_id === null && $dynamicSetting->model_type === null) {
80
            return new JsonResponse([
81
                'error' => 'Global dynamic setting can not be deleted',
82
            ], 400);
83
        }
84
85
        $dynamicSetting->delete();
86
87
        Event::dispatch(new DynamicSettingDeleted($dynamicSetting));
88
89
        return new JsonResponse();
90
    }
91
}
92