50 lines
1.8 KiB
PHP
50 lines
1.8 KiB
PHP
<?php
|
|
|
|
use Reservair\Logger\Logger;
|
|
|
|
/**
|
|
* Handles submitting a form. If any element fails, nothing is persisted.
|
|
*/
|
|
class RsvFormSubmission {
|
|
public function submit(string $formId, array $data) : array {
|
|
$repo = new RsvFormDefinitionRepository();
|
|
$row = $repo->get((int) $formId);
|
|
|
|
if ($row === null) {
|
|
return ['success' => false, 'errors' => [['element' => '', 'code' => 'not_found', 'message' => 'Form not found']]];
|
|
}
|
|
|
|
$definition = new RsvFormDefinition($formId, $row['definition']);
|
|
$form_data = new RsvFormData($data);
|
|
$processor = new RsvFormProcessor();
|
|
|
|
// Persist the submission first so element handlers can link to it.
|
|
$submit_repo = new RsvFormSubmitRepository();
|
|
$submit_id = $submit_repo->add((int) $definition->getId(), $data);
|
|
|
|
try {
|
|
$result = $processor->submit($definition, $submit_id, $form_data);
|
|
} catch (\Throwable $e) {
|
|
Logger::error($e);
|
|
$this->discard_submission($submit_repo, $submit_id);
|
|
return ['success' => false, 'errors' => [['element' => '', 'code' => 'internal_error', 'message' => 'An unexpected error occurred.']]];
|
|
}
|
|
|
|
if ($result->hasErrors()) {
|
|
$this->discard_submission($submit_repo, $submit_id);
|
|
return ['success' => false, 'errors' => $result->getErrors()];
|
|
}
|
|
|
|
return ['success' => true, 'submit_id' => $submit_id, 'values' => $result->getValues()];
|
|
}
|
|
|
|
/** Remove a submission whose run failed. */
|
|
private function discard_submission(RsvFormSubmitRepository $repo, int $submit_id): void {
|
|
try {
|
|
$repo->delete($submit_id);
|
|
} catch (\Throwable $e) {
|
|
Logger::error($e);
|
|
}
|
|
}
|
|
}
|