Completed
Push — master ( 365d3e...673226 )
by Freek
01:30
created

StripeWebhookCall   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 54
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 16 3
A saveException() 0 12 1
A determineJobClass() 0 6 1
A clearException() 0 7 1
1
<?php
2
3
namespace Spatie\StripeWebhooks;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\StripeWebhooks\Exceptions\WebhookFailed;
8
9
class StripeWebhookCall extends Model
10
{
11
    public $guarded = [];
12
13
    protected $casts = [
14
        'payload' => 'array',
15
        'exception' => 'array',
16
    ];
17
18
    public function process()
19
    {
20
        $this->clearException();
21
22
        $jobClass = $this->determineJobClass($this->type);
23
24
        if ($jobClass === '') {
25
            return;
26
        }
27
28
        if (! class_exists($jobClass)) {
29
            throw WebhookFailed::jobClassDoesNotExist($this);
0 ignored issues
show
Bug introduced by
The call to jobClassDoesNotExist() misses a required argument $webhookCall.

This check looks for function calls that miss required arguments.

Loading history...
30
        }
31
32
        dispatch(new $jobClass($this));
33
    }
34
35
    public function saveException(Exception $exception)
36
    {
37
        $this->exception = [
38
            'code' => $exception->getCode(),
39
            'message' => $exception->getMessage(),
40
            'trace' => $exception->getTraceAsString(),
41
        ];
42
43
        $this->save();
44
45
        return $this;
46
    }
47
48
    protected function determineJobClass(string $eventType): string
49
    {
50
        $jobConfigKey = str_replace('.', '_', $eventType);
51
52
        return config("stripe-webhooks.jobs.{$jobConfigKey}", '');
53
    }
54
55
    protected function clearException() {
56
        $this->exception = null;
57
58
        $this->save();
59
60
        return $this;
61
    }
62
}
63