Lint PHP code psr-12, add GH action

This commit is contained in:
Julien Nahum
2024-02-23 11:54:12 +01:00
parent e85e4df7fe
commit 62971a2ef4
226 changed files with 2338 additions and 2144 deletions

View File

@@ -4,8 +4,6 @@ namespace App\Service\Forms\Webhooks;
use App\Models\Forms\Form;
use App\Service\Forms\FormSubmissionFormatter;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Str;
use Spatie\WebhookServer\WebhookCall;
use Vinkla\Hashids\Facades\Hashids;
@@ -21,14 +19,12 @@ abstract class AbstractWebhookHandler
/**
* Default webhook payload. Can be changed in child classes.
* @return array
*/
protected function getWebhookData(): array
{
$formatter = (new FormSubmissionFormatter($this->form, $this->data))
->useSignedUrlForFiles()
->showHiddenFields()
;
->useSignedUrlForFiles()
->showHiddenFields();
$formattedData = [];
foreach ($formatter->getFieldsWithValue() as $field) {
@@ -41,7 +37,7 @@ abstract class AbstractWebhookHandler
'submission' => $formattedData,
];
if ($this->form->is_pro && $this->form->editable_submissions) {
$data['edit_link'] = $this->form->share_url . '?submission_id=' . Hashids::encode($this->data['submission_id']);
$data['edit_link'] = $this->form->share_url.'?submission_id='.Hashids::encode($this->data['submission_id']);
}
return $data;
@@ -51,8 +47,10 @@ abstract class AbstractWebhookHandler
public function handle()
{
if (!$this->shouldRun()) return;
if (! $this->shouldRun()) {
return;
}
WebhookCall::create()
// Add context on error, used to notify form owner
->meta([

View File

@@ -3,12 +3,11 @@
namespace App\Service\Forms\Webhooks;
use App\Service\Forms\FormSubmissionFormatter;
use Vinkla\Hashids\Facades\Hashids;
use Illuminate\Support\Arr;
use Vinkla\Hashids\Facades\Hashids;
class DiscordHandler extends AbstractWebhookHandler
{
protected function getProviderName(): string
{
return 'Discord';
@@ -21,66 +20,66 @@ class DiscordHandler extends AbstractWebhookHandler
protected function getWebhookData(): array
{
$settings = (array) Arr::get((array)$this->form->notification_settings, 'discord', []);
$settings = (array) Arr::get((array) $this->form->notification_settings, 'discord', []);
$externalLinks = [];
if(Arr::get($settings, 'link_open_form', true)){
$externalLinks[] = '[**🔗 Open Form**](' . $this->form->share_url . ')';
if (Arr::get($settings, 'link_open_form', true)) {
$externalLinks[] = '[**🔗 Open Form**]('.$this->form->share_url.')';
}
if(Arr::get($settings, 'link_edit_form', true)){
$editFormURL = front_url('forms/' . $this->form->slug . '/show');
$externalLinks[] = '[**✍️ Edit Form**](' . $editFormURL . ')';
if (Arr::get($settings, 'link_edit_form', true)) {
$editFormURL = front_url('forms/'.$this->form->slug.'/show');
$externalLinks[] = '[**✍️ Edit Form**]('.$editFormURL.')';
}
if (Arr::get($settings, 'link_edit_submission', true) && $this->form->editable_submissions) {
$submissionId = Hashids::encode($this->data['submission_id']);
$externalLinks[] = '[**✍️ ' . $this->form->editable_submissions_button_text . '**](' . $this->form->share_url . '?submission_id=' . $submissionId . ')';
$externalLinks[] = '[**✍️ '.$this->form->editable_submissions_button_text.'**]('.$this->form->share_url.'?submission_id='.$submissionId.')';
}
$color = hexdec(str_replace('#', '', $this->form->color));
$blocks = [];
if(Arr::get($settings, 'include_submission_data', true)){
$submissionString = "";
if (Arr::get($settings, 'include_submission_data', true)) {
$submissionString = '';
$formatter = (new FormSubmissionFormatter($this->form, $this->data))->outputStringsOnly();
foreach ($formatter->getFieldsWithValue() as $field) {
$tmpVal = is_array($field['value']) ? implode(",", $field['value']) : $field['value'];
$submissionString .= "**" . ucfirst($field['name']) . "**: " . $tmpVal . "\n";
$tmpVal = is_array($field['value']) ? implode(',', $field['value']) : $field['value'];
$submissionString .= '**'.ucfirst($field['name']).'**: '.$tmpVal."\n";
}
$blocks[] = [
"type" => "rich",
"color" => $color,
"description" => $submissionString
'type' => 'rich',
'color' => $color,
'description' => $submissionString,
];
}
if(Arr::get($settings, 'views_submissions_count', true)){
$countString = '**👀 Views**: ' . (string)$this->form->views_count . " \n";
$countString .= '**🖊️ Submissions**: ' . (string)$this->form->submissions_count;
if (Arr::get($settings, 'views_submissions_count', true)) {
$countString = '**👀 Views**: '.(string) $this->form->views_count." \n";
$countString .= '**🖊️ Submissions**: '.(string) $this->form->submissions_count;
$blocks[] = [
"type" => "rich",
"color" => $color,
"description" => $countString
'type' => 'rich',
'color' => $color,
'description' => $countString,
];
}
if(count($externalLinks) > 0){
if (count($externalLinks) > 0) {
$blocks[] = [
"type" => "rich",
"color" => $color,
"description" => implode(' - ', $externalLinks)
'type' => 'rich',
'color' => $color,
'description' => implode(' - ', $externalLinks),
];
}
return [
'content' => 'New submission for your form **' . $this->form->title . '**',
'content' => 'New submission for your form **'.$this->form->title.'**',
'tts' => false,
'username' => config('app.name'),
'avatar_url' => asset('img/logo.png'),
'embeds' => $blocks
'embeds' => $blocks,
];
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl())
return ! is_null($this->getWebhookUrl())
&& str_contains($this->getWebhookUrl(), 'https://discord.com/api/webhooks')
&& $this->form->is_pro;
}

View File

@@ -2,9 +2,6 @@
namespace App\Service\Forms\Webhooks;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Str;
class SimpleWebhookHandler extends AbstractWebhookHandler
{
protected function getProviderName(): string
@@ -19,6 +16,6 @@ class SimpleWebhookHandler extends AbstractWebhookHandler
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl()) && $this->form->is_pro;
return ! is_null($this->getWebhookUrl()) && $this->form->is_pro;
}
}

View File

@@ -3,12 +3,11 @@
namespace App\Service\Forms\Webhooks;
use App\Service\Forms\FormSubmissionFormatter;
use Vinkla\Hashids\Facades\Hashids;
use Illuminate\Support\Arr;
use Vinkla\Hashids\Facades\Hashids;
class SlackHandler extends AbstractWebhookHandler
{
protected function getProviderName(): string
{
return 'Slack';
@@ -21,18 +20,18 @@ class SlackHandler extends AbstractWebhookHandler
protected function getWebhookData(): array
{
$settings = (array) Arr::get((array)$this->form->notification_settings, 'slack', []);
$settings = (array) Arr::get((array) $this->form->notification_settings, 'slack', []);
$externalLinks = [];
if(Arr::get($settings, 'link_open_form', true)){
$externalLinks[] = '*<' . $this->form->share_url . '|🔗 Open Form>*';
if (Arr::get($settings, 'link_open_form', true)) {
$externalLinks[] = '*<'.$this->form->share_url.'|🔗 Open Form>*';
}
if(Arr::get($settings, 'link_edit_form', true)){
$editFormURL = front_url('forms/' . $this->form->slug . '/show');
$externalLinks[] = '*<' . $editFormURL . '|✍️ Edit Form>*';
if (Arr::get($settings, 'link_edit_form', true)) {
$editFormURL = front_url('forms/'.$this->form->slug.'/show');
$externalLinks[] = '*<'.$editFormURL.'|✍️ Edit Form>*';
}
if (Arr::get($settings, 'link_edit_submission', true) && $this->form->editable_submissions) {
$submissionId = Hashids::encode($this->data['submission_id']);
$externalLinks[] = '*<' . $this->form->share_url . '?submission_id=' . $submissionId . '|✍️ ' . $this->form->editable_submissions_button_text . '>*';
$externalLinks[] = '*<'.$this->form->share_url.'?submission_id='.$submissionId.'|✍️ '.$this->form->editable_submissions_button_text.'>*';
}
$blocks = [
@@ -40,57 +39,57 @@ class SlackHandler extends AbstractWebhookHandler
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => 'New submission for your form *' . $this->form->title . '*',
]
]
'text' => 'New submission for your form *'.$this->form->title.'*',
],
],
];
if(Arr::get($settings, 'include_submission_data', true)){
if (Arr::get($settings, 'include_submission_data', true)) {
$submissionString = '';
$formatter = (new FormSubmissionFormatter($this->form, $this->data))->outputStringsOnly();
foreach ($formatter->getFieldsWithValue() as $field) {
$tmpVal = is_array($field['value']) ? implode(',', $field['value']) : $field['value'];
$submissionString .= '>*' . ucfirst($field['name']) . '*: ' . $tmpVal . " \n";
$submissionString .= '>*'.ucfirst($field['name']).'*: '.$tmpVal." \n";
}
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $submissionString,
]
],
];
}
if(Arr::get($settings, 'views_submissions_count', true)){
$countString = '*👀 Views*: ' . (string)$this->form->views_count . " \n";
$countString .= '*🖊️ Submissions*: ' . (string)$this->form->submissions_count;
if (Arr::get($settings, 'views_submissions_count', true)) {
$countString = '*👀 Views*: '.(string) $this->form->views_count." \n";
$countString .= '*🖊️ Submissions*: '.(string) $this->form->submissions_count;
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $countString,
]
],
];
}
if(count($externalLinks) > 0){
if (count($externalLinks) > 0) {
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => implode(' ', $externalLinks),
]
],
];
}
return [
'blocks' => $blocks
'blocks' => $blocks,
];
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl())
return ! is_null($this->getWebhookUrl())
&& str_contains($this->getWebhookUrl(), 'https://hooks.slack.com/')
&& $this->form->is_pro;
}

View File

@@ -6,10 +6,13 @@ use App\Models\Forms\Form;
class WebhookHandlerProvider
{
const SLACK_PROVIDER = 'slack';
const DISCORD_PROVIDER = 'discord';
const SIMPLE_WEBHOOK_PROVIDER = 'webhook';
const ZAPIER_PROVIDER = 'zapier';
public const SLACK_PROVIDER = 'slack';
public const DISCORD_PROVIDER = 'discord';
public const SIMPLE_WEBHOOK_PROVIDER = 'webhook';
public const ZAPIER_PROVIDER = 'zapier';
public static function getProvider(Form $form, array $data, string $provider, ?string $webhookUrl = null)
{
@@ -24,6 +27,7 @@ class WebhookHandlerProvider
if (is_null($webhookUrl)) {
throw new \Exception('Zapier webhook url is required');
}
return new ZapierHandler($form, $data, $webhookUrl);
default:
throw new \Exception('Unknown webhook provider');

View File

@@ -22,6 +22,6 @@ class ZapierHandler extends AbstractWebhookHandler
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl());
return ! is_null($this->getWebhookUrl());
}
}