opnform/app/Service/Forms/Webhooks/AbstractWebhookHandler.php

68 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Service\Forms\Webhooks;
use App\Models\Forms\Form;
use App\Service\Forms\FormSubmissionFormatter;
use Spatie\WebhookServer\WebhookCall;
use Vinkla\Hashids\Facades\Hashids;
abstract class AbstractWebhookHandler
{
public function __construct(protected Form $form, protected array $data)
{
}
abstract protected function getProviderName(): ?string;
abstract protected function getWebhookUrl(): ?string;
/**
* Default webhook payload. Can be changed in child classes.
*/
protected function getWebhookData(): array
{
2024-02-20 11:49:30 +00:00
$formatter = (new FormSubmissionFormatter($this->form, $this->data))
2024-02-23 10:54:12 +00:00
->useSignedUrlForFiles()
->showHiddenFields();
$formattedData = [];
foreach ($formatter->getFieldsWithValue() as $field) {
$formattedData[$field['name']] = $field['value'];
}
$data = [
'form_title' => $this->form->title,
'form_slug' => $this->form->slug,
'submission' => $formattedData,
];
if ($this->form->is_pro && $this->form->editable_submissions) {
2024-02-23 10:54:12 +00:00
$data['edit_link'] = $this->form->share_url.'?submission_id='.Hashids::encode($this->data['submission_id']);
}
return $data;
}
abstract protected function shouldRun(): bool;
public function handle()
{
2024-02-23 10:54:12 +00:00
if (! $this->shouldRun()) {
return;
}
WebhookCall::create()
// Add context on error, used to notify form owner
->meta([
'type' => 'form_submission',
'data' => $this->data,
'form' => $this->form,
'provider' => $this->getProviderName(),
])
->url($this->getWebhookUrl())
->doNotSign()
->payload($this->getWebhookData())
->dispatchSync();
}
}