-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPostStoreSettingHandler.php
More file actions
88 lines (76 loc) · 2.99 KB
/
Copy pathPostStoreSettingHandler.php
File metadata and controls
88 lines (76 loc) · 2.99 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
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace Admin\Setting\Handler;
use Admin\Admin\Service\AdminServiceInterface;
use Admin\App\Exception\NotFoundException;
use Admin\Setting\InputFilter\CreateSettingInputFilter;
use Admin\Setting\InputFilter\Input\ValueInput;
use Admin\Setting\Service\SettingServiceInterface;
use Core\Setting\Entity\Setting;
use Core\Setting\Enum\SettingIdentifierEnum;
use Dot\DependencyInjection\Attribute\Inject;
use Fig\Http\Message\StatusCodeInterface;
use Laminas\Authentication\AuthenticationServiceInterface;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function assert;
use function is_array;
use function json_decode;
class PostStoreSettingHandler implements RequestHandlerInterface
{
#[Inject(
AuthenticationServiceInterface::class,
AdminServiceInterface::class,
SettingServiceInterface::class,
)]
public function __construct(
protected AuthenticationServiceInterface $authenticationService,
protected AdminServiceInterface $adminService,
protected SettingServiceInterface $settingService,
) {
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$data = json_decode($request->getBody()->getContents(), true);
$value = $data['value'] ?? null;
$identifier = $request->getAttribute('identifier');
$inputFilter = (new CreateSettingInputFilter())
->add(new ValueInput('value', true))
->setData([
'identifier' => $identifier,
'value' => $value,
]);
if (! $inputFilter->isValid()) {
$messages = $inputFilter->getMessages();
return new JsonResponse([
'error' => [
'messages' => is_array($messages) ? $messages : [$messages],
],
], StatusCodeInterface::STATUS_BAD_REQUEST);
}
try {
$admin = $this->adminService->findAdmin($this->authenticationService->getIdentity()->getUuid());
} catch (NotFoundException $exception) {
return new JsonResponse([
'error' => [
'messages' => [
$exception->getMessage(),
],
],
], StatusCodeInterface::STATUS_BAD_REQUEST);
}
$identifier = SettingIdentifierEnum::tryFrom($identifier);
assert($identifier instanceof SettingIdentifierEnum);
$setting = $this->settingService->findOneBy(['admin' => $admin, 'identifier' => $identifier]);
if ($setting instanceof Setting) {
$setting = $this->settingService->updateSetting($setting, $value);
} else {
$setting = $this->settingService->createSetting($admin, $identifier, $value);
}
return new JsonResponse([
'data' => $setting->getArrayCopy(),
]);
}
}