Completed
Push — master ( 673226...2f9fa2 )
by Freek
01:35
created

StripeWebhookCall::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 0
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
        if ($this->type === '') {
23
            throw WebhookFailed::missingType($this);
24
        }
25
26
        $jobClass = $this->determineJobClass($this->type);
27
28
        event("stripe-webhooks::{$this->type}", $this);
29
30
        if ($jobClass === '') {
31
            return;
32
        }
33
34
        if (! class_exists($jobClass)) {
35
            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...
36
        }
37
38
        dispatch(new $jobClass($this));
39
    }
40
41
    public function saveException(Exception $exception)
42
    {
43
        $this->exception = [
44
            'code' => $exception->getCode(),
45
            'message' => $exception->getMessage(),
46
            'trace' => $exception->getTraceAsString(),
47
        ];
48
49
        $this->save();
50
51
        return $this;
52
    }
53
54
    protected function determineJobClass(string $eventType): string
55
    {
56
        $jobConfigKey = str_replace('.', '_', $eventType);
57
58
        return config("stripe-webhooks.jobs.{$jobConfigKey}", '');
59
    }
60
61
    protected function clearException() {
62
        $this->exception = null;
63
64
        $this->save();
65
66
        return $this;
67
    }
68
}
69