Webhook::setSingleData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 29
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Luigel\Paymongo\Models;
4
5
use Luigel\Paymongo\Paymongo;
6
7
class Webhook
8
{
9
    public const SOURCE_CHARGEABLE = 'source.chargeable';
10
11
    protected $id;
12
    protected $type;
13
    protected $secret_key;
14
    protected $status;
15
    protected $url;
16
    protected $events;
17
    protected $updated_at;
18
    protected $created_at;
19
    protected $data;
20
21
    public function setData($data)
22
    {
23
        if (is_array($data) && isset($data['id'])) {
24
            return $this->setSingleData($data);
25
        }
26
        $webhooks = collect();
27
28
        foreach ($data as $item) {
29
            $webhooks->push($this->setSingleData($item));
30
        }
31
32
        return $webhooks;
33
    }
34
35
    protected function setSingleData($data)
36
    {
37
        $this->id = $data['id'];
38
        $this->type = $data['type'];
39
        $this->secret_key = $data['attributes']['secret_key'];
40
        $this->status = $data['attributes']['status'];
41
        $this->url = $data['attributes']['url'];
42
        $this->updated_at = $data['attributes']['updated_at'];
43
        $this->created_at = $data['attributes']['created_at'];
44
45
        $events = collect();
46
        foreach ($data['attributes']['events'] as $event) {
47
            $events->push($event);
48
        }
49
50
        $this->events = $events;
51
52
        $this->data = [
53
            'id' => $this->id,
54
            'type' => $this->type,
55
            'secret_key' => $this->secret_key,
56
            'status' => $this->status,
57
            'url' => $this->url,
58
            'events' => $this->events,
59
            'created_at' => $this->created_at,
60
            'updated_at' => $this->updated_at,
61
        ];
62
63
        return $this;
64
    }
65
66
    public function getId()
67
    {
68
        return $this->id;
69
    }
70
71
    public function getType()
72
    {
73
        return $this->type;
74
    }
75
76
    public function getSecretKey()
77
    {
78
        return $this->secret_key;
79
    }
80
81
    public function getStatus()
82
    {
83
        return $this->status;
84
    }
85
86
    public function getUrl()
87
    {
88
        return $this->url;
89
    }
90
91
    public function getEvents($i = null)
92
    {
93
        if (is_int($i) && $i !== null) {
94
            return $this->events[$i];
95
        }
96
97
        return $this->events;
98
    }
99
100
    public function getCreatedAt()
101
    {
102
        return $this->created_at;
103
    }
104
105
    public function getUpdatedAt()
106
    {
107
        return $this->updated_at;
108
    }
109
110
    public function enable()
111
    {
112
        return (new Paymongo)->webhook()->enable($this);
113
    }
114
115
    public function disable()
116
    {
117
        return (new Paymongo)->webhook()->disable($this);
118
    }
119
120
    public function update($payload)
121
    {
122
        return (new Paymongo)->webhook()->update($this, $payload);
123
    }
124
}
125