Passed
Push — master ( 98972e...fbf8ae )
by Darko
05:51
created

ProcessBtcPayWebhook   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 12
c 1
b 0
f 0
dl 0
loc 20
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 13 3
1
<?php
2
3
namespace App\Jobs;
4
5
use App\Models\User;
6
use Illuminate\Bus\Queueable;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Foundation\Bus\Dispatchable;
9
use Illuminate\Queue\InteractsWithQueue;
10
use Illuminate\Queue\SerializesModels;
11
use Illuminate\Support\Facades\Log;
12
use Spatie\WebhookClient\Jobs\ProcessWebhookJob;
13
14
class ProcessBtcPayWebhook extends ProcessWebhookJob implements ShouldQueue
15
{
16
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by App\Jobs\ProcessBtcPayWebhook: $collectionClass, $id, $relations, $class, $keyBy
Loading history...
17
18
    /**
19
     * Execute the job.
20
     */
21
    public function handle(): void
22
    {
23
        $payload = $this->webhookCall->payload;
24
        // We have received a payment for an invoice and user should be upgraded to a paid plan based on order
25
        if ($payload['type'] === 'InvoiceReceivedPayment') {
26
            preg_match('/(?P<role>\w+(\+\+)?)[ ](?P<addYears>\d+)/i', $payload['metadata']['itemDesc'], $matches);
27
            $user = User::query()->where('email', '=', $payload['metadata']['buyerEmail'])->first();
28
            if ($user) {
29
                User::updateUserRole($user->id, $matches['role']);
30
                User::updateUserRoleChangeDate($user->id, null, $matches['addYears']);
31
                Log::info('User upgraded to '.$matches['role'].' for BTCPay webhook: '.$payload['metadata']['buyerEmail']);
32
            } else {
33
                Log::error('User not found for BTCPay webhook: '.$payload['metadata']['buyerEmail']);
34
35
            }
36
        }
37
38
    }
39
}
40