Completed
Push — master ( 7b285b...480624 )
by Rigel Kent
04:14
created

Webhook::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
    public $id;
12
    public $type;
13
    public $secret_key;
14
    public $status;
15
    public $url;
16
    public $events;
17
    public $updated_at;
18
    public $created_at;
19
20
    public function setData($data)
21
    {
22
        if (is_array($data) && isset($data['id']))
23
        {
24
            return $this->convertToObject($data);
25
        }
26
        $webhooks = collect();
27
28
        foreach ($data as $item)
29
        {
30
            $webhooks->push($this->convertToObject($item));
31
        }
32
        return $webhooks;
33
    }
34
35
    protected function convertToObject($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
        {
48
            $events->push($event);
49
        }
50
51
        $this->events = $events;
52
53
        return $this;
54
    }
55
56
    public function enable()
57
    {
58
        return (new Paymongo)->webhook()->enable($this);
59
    }
60
61
    public function disable()
62
    {
63
        return (new Paymongo)->webhook()->disable($this);
64
    }
65
66
    public function update($payload)
67
    {
68
        return (new Paymongo)->webhook()->update($this, $payload);
69
    }
70
}
71