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

StripeWebhookCall   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 50
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 12 2
A determineJobClass() 0 6 1
A clearException() 0 7 1
A saveException() 0 12 1
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