Skip to content

Commit b7e4029

Browse files
committed
Small refactoring
1 parent 11148df commit b7e4029

14 files changed

+31
-30
lines changed

app/Listeners/Forms/NotifyFormSubmission.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Events\Forms\FormSubmitted;
66
use App\Models\Integration\FormIntegration;
7+
use App\Service\Forms\Integrations\AbstractIntegrationHandler;
78
use Illuminate\Contracts\Queue\ShouldQueue;
89
use Illuminate\Queue\InteractsWithQueue;
910

@@ -21,7 +22,7 @@ public function handle(FormSubmitted $event)
2122
{
2223
$formIntegrations = FormIntegration::where([['form_id', $event->form->id], ['status', FormIntegration::STATUS_ACTIVE]])->get();
2324
foreach ($formIntegrations as $formIntegration) {
24-
$this->integrationHandler(
25+
$this->getIntegrationHandler(
2526
$event,
2627
$formIntegration
2728
)->handle();
@@ -37,7 +38,7 @@ public function handle(FormSubmitted $event)
3738
*/
3839
}
3940

40-
public static function integrationHandler(FormSubmitted $event, FormIntegration $formIntegration)
41+
public static function getIntegrationHandler(FormSubmitted $event, FormIntegration $formIntegration): AbstractIntegrationHandler
4142
{
4243
$integration = FormIntegration::getIntegration($formIntegration->integration_id);
4344
if ($integration && isset($integration['file_name']) && class_exists('App\Service\Forms\Webhooks\\' . $integration['file_name'])) {

app/Models/Integration/FormZapierWebhook.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace App\Models\Integration;
44

55
use App\Models\Forms\Form;
6-
use App\Service\Forms\Webhooks\WebhookHandlerProvider;
6+
use App\Service\Forms\Integrations\WebhookHandlerProvider;
77
use Illuminate\Database\Eloquent\Factories\HasFactory;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Eloquent\SoftDeletes;

app/Service/Forms/Webhooks/AbstractIntegrationHandler.php app/Service/Forms/Integrations/AbstractIntegrationHandler.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
use App\Models\Integration\FormIntegration;
66
use App\Events\Forms\FormSubmitted;
@@ -27,7 +27,7 @@ protected function getProviderName(): string
2727
return $this->integration['name'] ?? '';
2828
}
2929

30-
protected function isValidLogic(): bool
30+
protected function logicConditionsMet(): bool
3131
{
3232
if (!$this->formIntegration->logic) {
3333
return true;
@@ -37,7 +37,7 @@ protected function isValidLogic(): bool
3737

3838
protected function shouldRun(): bool
3939
{
40-
return true;
40+
return $this->logicConditionsMet();
4141
}
4242

4343
protected function getWebhookUrl(): ?string
@@ -76,7 +76,7 @@ protected function getWebhookData(): array
7676
*/
7777
public function handle()
7878
{
79-
if (!$this->shouldRun() || !$this->isValidLogic()) {
79+
if (!$this->shouldRun()) {
8080
return;
8181
}
8282

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
use App\Models\Forms\Form;
66
use App\Service\Forms\FormSubmissionFormatter;

app/Service/Forms/Webhooks/DiscordHandler.php app/Service/Forms/Integrations/DiscordHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
use App\Service\Forms\FormSubmissionFormatter;
66
use Illuminate\Support\Arr;

app/Service/Forms/Webhooks/DiscordNotification.php app/Service/Forms/Integrations/DiscordNotification.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
use App\Service\Forms\FormSubmissionFormatter;
66
use Illuminate\Support\Arr;
@@ -18,7 +18,7 @@ protected function shouldRun(): bool
1818
{
1919
return !is_null($this->getWebhookUrl())
2020
&& str_contains($this->getWebhookUrl(), 'https://discord.com/api/webhooks')
21-
&& $this->form->is_pro;
21+
&& $this->form->is_pro && parent::shouldRun();
2222
}
2323

2424
protected function getWebhookData(): array

app/Service/Forms/Webhooks/EmailNotification.php app/Service/Forms/Integrations/EmailNotification.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
use Illuminate\Support\Facades\Log;
66
use Illuminate\Support\Facades\Notification;
@@ -10,20 +10,19 @@ class EmailNotification extends AbstractIntegrationHandler
1010
{
1111
protected function shouldRun(): bool
1212
{
13-
return !(!$this->form->is_pro || !$this->formIntegrationData->notification_emails);
13+
return !(!$this->form->is_pro || !$this->formIntegrationData->notification_emails) && parent::shouldRun();
1414
}
1515

1616
public function handle()
1717
{
18-
if (!$this->shouldRun() || !$this->isValidLogic()) {
18+
if (!$this->shouldRun()) {
1919
return;
2020
}
2121

22-
$subscribers = collect(preg_split("/\r\n|\n|\r/", $this->formIntegrationData->notification_emails))->filter(function (
23-
$email
24-
) {
25-
return filter_var($email, FILTER_VALIDATE_EMAIL);
26-
});
22+
$subscribers = collect(preg_split("/\r\n|\n|\r/", $this->formIntegrationData->notification_emails))
23+
->filter(function ($email) {
24+
return filter_var($email, FILTER_VALIDATE_EMAIL);
25+
});
2726
Log::debug('Sending email notification', [
2827
'recipients' => $subscribers->toArray(),
2928
'form_id' => $this->form->id,

app/Service/Forms/Webhooks/SimpleWebhookHandler.php app/Service/Forms/Integrations/SimpleWebhookHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
class SimpleWebhookHandler extends AbstractWebhookHandler
66
{

app/Service/Forms/Webhooks/SlackHandler.php app/Service/Forms/Integrations/SlackHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
use App\Service\Forms\FormSubmissionFormatter;
66
use Illuminate\Support\Arr;

app/Service/Forms/Webhooks/SlackNotification.php app/Service/Forms/Integrations/SlackNotification.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
use App\Service\Forms\FormSubmissionFormatter;
66
use Illuminate\Support\Arr;
@@ -17,7 +17,8 @@ protected function shouldRun(): bool
1717
{
1818
return !is_null($this->getWebhookUrl())
1919
&& str_contains($this->getWebhookUrl(), 'https://hooks.slack.com/')
20-
&& $this->form->is_pro;
20+
&& $this->form->is_pro
21+
&& parent::shouldRun();
2122
}
2223

2324
protected function getWebhookData(): array

app/Service/Forms/Webhooks/SubmissionConfirmation.php app/Service/Forms/Integrations/SubmissionConfirmation.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
use App\Mail\Forms\SubmissionConfirmationMail;
66
use Illuminate\Support\Facades\Mail;
@@ -15,12 +15,12 @@ class SubmissionConfirmation extends AbstractIntegrationHandler
1515

1616
protected function shouldRun(): bool
1717
{
18-
return !(!$this->form->is_pro);
18+
return !(!$this->form->is_pro) && parent::shouldRun() && !$this->riskLimitReached();
1919
}
2020

2121
public function handle()
2222
{
23-
if (!$this->shouldRun() || !$this->isValidLogic() || $this->riskLimitReached()) {
23+
if (!$this->shouldRun()) {
2424
return;
2525
}
2626

app/Service/Forms/Webhooks/WebhookHandlerProvider.php app/Service/Forms/Integrations/WebhookHandlerProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
use App\Models\Forms\Form;
66

app/Service/Forms/Webhooks/WebhookNotification.php app/Service/Forms/Integrations/WebhookNotification.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
class WebhookNotification extends AbstractIntegrationHandler
66
{
@@ -11,6 +11,6 @@ protected function getWebhookUrl(): ?string
1111

1212
protected function shouldRun(): bool
1313
{
14-
return !is_null($this->getWebhookUrl()) && $this->form->is_pro;
14+
return !is_null($this->getWebhookUrl()) && $this->form->is_pro && parent::shouldRun();
1515
}
1616
}

app/Service/Forms/Webhooks/ZapierHandler.php app/Service/Forms/Integrations/ZapierHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Service\Forms\Webhooks;
3+
namespace App\Service\Forms\Integrations;
44

55
use App\Models\Forms\Form;
66

0 commit comments

Comments
 (0)