Completed
Push — master ( 2655e6...365d3e )
by Freek
01:46
created

StripeWebhookCall::clearException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace App\Models;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Model;
7
8
class StripeWebhookCall extends Model
9
{
10
    public $guarded = [];
11
12
    protected $casts = [
13
        'payload' => 'array',
14
        'exception' => 'array',
15
    ];
16
17
    public function process()
18
    {
19
        $this->clearException();
20
21
        $jobClass = $this->determineJobClass($this->type);
22
23
        if ($jobClass === '') {
24
            return;
25
        }
26
27
        dispatch(new $jobClass($this));
28
    }
29
30
    protected function determineJobClass(string $eventType): string
31
    {
32
        $jobConfigKey = str_replace('.', '_', $eventType);
33
34
        return config("stripe-webhooks.jobs.{$jobConfigKey}", '');
35
    }
36
37
    protected function clearException() {
38
        $this->exception = null;
39
40
        $this->save();
41
42
        return $this;
43
    }
44
45
    protected function saveException(Exception $exception)
46
    {
47
        $this->exception = [
48
            'code' => $exception->getCode(),
49
            'message' => $exception->getMessage(),
50
            'exception' => $exception->getTraceAsString(),
51
        ];
52
53
        $this->save();
54
55
        return $this;
56
    }
57
}
58