Passed
Push — master ( 8c743d...154964 )
by Eudald
10:11
created

GocardlessWebhookCall   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A clearException() 0 7 1
A determineJobClass() 0 8 1
A process() 0 25 5
A saveException() 0 11 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: eudaldarranztresserra
5
 * Date: 2019-03-01
6
 * Time: 13:08
7
 */
8
9
namespace Nestednet\Gocardless;
10
11
use Exception;
12
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Nestednet\Gocardless\Exceptions\WebhookFailed;
14
15
class GocardlessWebhookCall extends Model
16
{
17
    public $guarded = [];
18
19
    protected $casts = [
20
        'payload' => 'array',
21
        'exception' => 'array',
22
    ];
23
24
    public function process()
25
    {
26
        $this->clearException();
27
28
        if ($this->resource_type === '') {
29
            throw WebhookFailed::missingResource($this);
30
        }
31
32
        if ($this->action === '') {
33
            throw WebhookFailed::missingAction($this);
34
        }
35
36
        event("gocardless-webhooks::{$this->resource_type}_{$this->action}", $this);
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        /** @scrutinizer ignore-call */ 
37
        event("gocardless-webhooks::{$this->resource_type}_{$this->action}", $this);
Loading history...
37
38
        $jobClass = $this->determineJobClass($this->resource_type, $this->action);
39
40
        if ($jobClass === "") {
41
            return;
42
        }
43
44
        if (! class_exists($jobClass)) {
45
            throw WebhookFailed::jobClassDoesNotExist($jobClass, $this);
46
        }
47
48
        dispatch(new $jobClass($this));
0 ignored issues
show
Bug introduced by
The function dispatch was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        /** @scrutinizer ignore-call */ 
49
        dispatch(new $jobClass($this));
Loading history...
49
    }
50
51
    public function saveException(Exception $exception)
52
    {
53
        $this->exception = [
0 ignored issues
show
Bug Best Practice introduced by
The property exception does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
            'code' => $exception->getCode(),
55
            'message' => $exception->getMessage(),
56
            'trace' => $exception->getTraceAsString(),
57
        ];
58
59
        $this->save();
60
61
        return $this;
62
    }
63
64
    protected function determineJobClass(string $resourceType, string $action) : string
65
    {
66
        $formattedResourceType = str_replace('.', '_', $resourceType);
67
        $formattedAction = str_replace('.', '_', $action);
68
69
        $jobClassKey = "{$formattedResourceType}_{$formattedAction}";
70
71
        return config("gocardless.webhooks.jobs.{$jobClassKey}", "");
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        return /** @scrutinizer ignore-call */ config("gocardless.webhooks.jobs.{$jobClassKey}", "");
Loading history...
72
    }
73
74
    protected function clearException()
75
    {
76
        $this->exception = null;
0 ignored issues
show
Bug Best Practice introduced by
The property exception does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
78
        $this->save();
79
80
        return $this;
81
    }
82
83
}