WebhookController   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 31
ccs 10
cts 10
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 14 1
A verify() 0 3 1
1
<?php
2
3
namespace Sebdesign\VivaPayments\Http\Controllers;
4
5
use Illuminate\Http\JsonResponse;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Controller;
8
use Sebdesign\VivaPayments\Events\TransactionFailed;
9
use Sebdesign\VivaPayments\Events\TransactionPaymentCreated;
10
use Sebdesign\VivaPayments\Events\WebhookEvent;
11
use Sebdesign\VivaPayments\Webhook;
12
13
class WebhookController extends Controller
14
{
15
    /**
16
     * Verify a webhook.
17
     *
18
     * @see https://developer.vivawallet.com/webhooks-for-payments/#generate-a-webhook-verification-key
19
     */
20 3
    public function verify(Webhook $webhook): JsonResponse
21
    {
22 3
        return response()->json($webhook->getVerificationKey());
23
    }
24
25
    /**
26
     * Handle requests from Viva Wallet.
27
     *
28
     * @see https://developer.vivawallet.com/webhooks-for-payments/#handle-requests-from-viva-wallet
29
     */
30 9
    public function handle(Request $request): JsonResponse
31
    {
32
        /** @phpstan-ignore-next-line */
33 9
        $event = WebhookEvent::create($request->json()->all());
34
35 9
        event($event);
36
37 9
        match ($event->EventData::class) {
38 3
            TransactionPaymentCreated::class => event($event->EventData),
39 3
            TransactionFailed::class => event($event->EventData),
40 1
            default => null,
41
        };
42
43 9
        return response()->json();
44
    }
45
}
46