-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreRequest.php
More file actions
145 lines (120 loc) · 5.3 KB
/
Copy pathStoreRequest.php
File metadata and controls
145 lines (120 loc) · 5.3 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Mitwork\Kalkan\Http\Actions;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Str;
use Mitwork\Kalkan\CacheDocument;
use Mitwork\Kalkan\CacheRequest;
use Mitwork\Kalkan\Enums\AuthType;
use Mitwork\Kalkan\Events\DocumentSaved;
use Mitwork\Kalkan\Events\RequestSaved;
use Mitwork\Kalkan\Http\Requests\PrepareServiceRequest;
use Mitwork\Kalkan\Services\CacheDocumentService;
use Mitwork\Kalkan\Services\CacheRequestService;
use Mitwork\Kalkan\Services\IntegrationService;
use Mitwork\Kalkan\Services\KalkanValidationService;
use Mitwork\Kalkan\Services\QrCodeGenerationService;
class StoreRequest extends BaseAction
{
public function __construct(
public IntegrationService $integrationService,
public QrCodeGenerationService $qrCodeGenerationService,
public KalkanValidationService $validationService,
public CacheDocumentService $documentService,
public CacheRequestService $requestService,
) {}
/**
* Шаг 2 - формирование сервисного запроса
*
* В данном примере содержимое документа сохраняется
* только в кэш, в реально жизни это может быть база
* или шина данных.
*
* Последующие запросы используют этот идентификатор
* для запроса и работы с данными.
*/
public function store(PrepareServiceRequest $request): JsonResponse
{
$id = Str::uuid()->toString();
$attributes = $request->validated();
if (! isset($attributes['auth']) || ! isset($attributes['auth']['type'])) {
if (config('kalkan.options.auth.type') === AuthType::BEARER->value) {
if ($token = request()->bearerToken()) {
$attributes['auth'] = [
'type' => AuthType::BEARER->value,
'token' => $token,
];
} else {
$attributes['auth'] = [
'type' => AuthType::BEARER->value,
'token' => Str::random(32),
];
}
} else {
$attributes['auth'] = [
'type' => AuthType::NONE->value,
'token' => '',
];
}
} elseif ($attributes['auth']['type'] === AuthType::BEARER->value && empty($attributes['auth']['token'])) {
$attributes['auth'] = [
'type' => AuthType::BEARER->value,
'token' => Str::random(32),
];
} elseif ($attributes['auth']['type'] === AuthType::NONE->value) {
$attributes['auth'] = [
'type' => AuthType::NONE->value,
'token' => '',
];
}
if (! isset($attributes['organisation']) || empty(array_filter($attributes['organisation']))) {
$attributes['organisation'] = [
'nameRu' => config('kalkan.options.organisation.nameRu'),
'nameKz' => config('kalkan.options.organisation.nameKz'),
'nameEn' => config('kalkan.options.organisation.nameEn'),
'bin' => config('kalkan.options.organisation.bin'),
];
} else {
$attributes['organisation']['nameRu'] = $attributes['organisation']['name'];
$attributes['organisation']['nameKz'] = $attributes['organisation']['name'];
$attributes['organisation']['nameEn'] = $attributes['organisation']['name'];
unset($attributes['organisation']['name']);
}
$serviceRequest = new CacheRequest(...$attributes);
$files = $serviceRequest->files;
foreach ($files as &$file) {
if (! isset($file['id'])) {
$file = new CacheDocument(...$file);
$file['id'] = hrtime(true);
if (! $this->documentService->add($file['id'], $file->toArray())) {
return response()->json([
'message' => __('kalkan::messages.unable_to_save_document'),
], 500);
}
DocumentSaved::dispatch($file['id'], $file->toArray());
}
}
if (! $this->requestService->add($id, $serviceRequest->toArray())) {
return response()->json([
'message' => __('kalkan::messages.unable_to_save_request'),
], 500);
}
RequestSaved::dispatch($id, $serviceRequest->toArray());
$link = $this->generateSignedLink(config('kalkan.actions.generate-service-link'), ['id' => $id]);
$result = $this->qrCodeGenerationService->generate($link);
return response()->json([
'id' => $id,
'url' => $link,
'links' => [
'qr' => [
'uri' => $result->getDataUri(),
'raw' => base64_encode($result->getString()),
],
'app' => [
'mobile' => sprintf(config('kalkan.links.mobile'), urlencode($link)),
'business' => sprintf(config('kalkan.links.business'), urlencode($link)),
],
],
'expire' => time() + config('kalkan.ttl'),
]);
}
}