2023-03-14 22:29:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Contribution\Actions;
|
|
|
|
|
|
|
|
use App\Contribution\ContributionFactory;
|
2023-03-14 22:40:46 +01:00
|
|
|
use App\Contribution\Documents\ContributionDocument;
|
2023-03-14 22:29:39 +01:00
|
|
|
use App\Rules\JsonBase64Rule;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
use Lorisleiva\Actions\ActionRequest;
|
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
2023-03-14 22:36:31 +01:00
|
|
|
use Zoomyboy\Tex\BaseCompiler;
|
2023-03-14 22:29:39 +01:00
|
|
|
use Zoomyboy\Tex\Tex;
|
|
|
|
|
|
|
|
class GenerateAction
|
|
|
|
{
|
|
|
|
use AsAction;
|
|
|
|
|
|
|
|
/**
|
2023-03-14 22:40:46 +01:00
|
|
|
* @param class-string<ContributionDocument> $document
|
|
|
|
* @param array<string, mixed> $payload
|
2023-03-14 22:29:39 +01:00
|
|
|
*/
|
2023-03-14 22:36:31 +01:00
|
|
|
public function handle(string $document, array $payload): BaseCompiler
|
2023-03-14 22:29:39 +01:00
|
|
|
{
|
|
|
|
return Tex::compile($document::fromRequest($payload));
|
|
|
|
}
|
|
|
|
|
2023-03-14 23:57:52 +01:00
|
|
|
public function asController(ActionRequest $request): BaseCompiler
|
2023-03-14 22:29:39 +01:00
|
|
|
{
|
|
|
|
$payload = $this->payload($request);
|
|
|
|
$type = data_get($payload, 'type');
|
|
|
|
ValidateAction::validateType($type);
|
|
|
|
Validator::make($payload, app(ContributionFactory::class)->rules($type))->validate();
|
|
|
|
|
|
|
|
return $this->handle($type, $payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-14 22:36:31 +01:00
|
|
|
* @return array<string, mixed>
|
2023-03-14 22:29:39 +01:00
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'payload' => [new JsonBase64Rule()],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
private function payload(ActionRequest $request): array
|
|
|
|
{
|
2023-10-30 22:58:25 +01:00
|
|
|
return json_decode(rawurldecode(base64_decode($request->input('payload', ''))), true);
|
2023-03-14 22:29:39 +01:00
|
|
|
}
|
|
|
|
}
|