-
-
Notifications
You must be signed in to change notification settings - Fork 681
Expand file tree
/
Copy pathCreateConfigurationAction.php
More file actions
75 lines (64 loc) · 2.72 KB
/
Copy pathCreateConfigurationAction.php
File metadata and controls
75 lines (64 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace HiEvents\Http\Actions\Admin\Configurations;
use HiEvents\DomainObjects\Enums\Feature;
use HiEvents\DomainObjects\Enums\Role;
use HiEvents\Http\Actions\BaseAction;
use HiEvents\Repository\Interfaces\OrganizerConfigurationRepositoryInterface;
use HiEvents\Resources\Organizer\OrganizerConfigurationResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Symfony\Component\HttpFoundation\Response;
class CreateConfigurationAction extends BaseAction
{
public function __construct(
private readonly OrganizerConfigurationRepositoryInterface $repository,
) {}
public function __invoke(Request $request): JsonResponse
{
$this->minimumAllowedRole(Role::SUPERADMIN);
$validated = $request->validate([
'name' => 'required|string|max:255',
'application_fees' => 'required|array',
'application_fees.fixed' => 'required|numeric|min:0',
'application_fees.percentage' => 'required|numeric|min:0|max:100',
'application_fees.currency' => 'sometimes|string|size:3|alpha|uppercase',
'features' => ['sometimes', 'nullable', 'array', self::validFeatureMap()],
'upgrades_to_id' => [
'sometimes',
'nullable',
'integer',
Rule::exists('organizer_configurations', 'id')->whereNull('deleted_at'),
],
'bypass_application_fees' => 'sometimes|boolean',
]);
$configuration = $this->repository->create([
'name' => $validated['name'],
'is_system_default' => false,
'application_fees' => $validated['application_fees'],
'features' => $validated['features'] ?? null,
'upgrades_to_id' => $validated['upgrades_to_id'] ?? null,
'bypass_application_fees' => $validated['bypass_application_fees'] ?? false,
]);
return $this->jsonResponse(
new OrganizerConfigurationResource($configuration),
statusCode: Response::HTTP_CREATED,
wrapInData: true
);
}
public static function validFeatureMap(): callable
{
return static function (string $attribute, mixed $value, callable $fail) {
$knownFeatures = array_column(Feature::cases(), 'value');
foreach ((array) $value as $feature => $enabled) {
if (! in_array($feature, $knownFeatures, true)) {
$fail(__('Unknown feature: :feature', ['feature' => $feature]));
}
if (! is_bool($enabled)) {
$fail(__('Feature values must be true or false.'));
}
}
};
}
}