Completed
Pull Request — master (#172)
by
unknown
01:59
created

NotificationPreferences::populate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace Moip\Resource;
4
5
use stdClass;
6
7
/**
8
 * Description of NotificationPreferences.
9
 */
10
class NotificationPreferences extends MoipResource
11
{
12
    /**
13
     * Path accounts API.
14
     *
15
     * @const string
16
     */
17
    const PATH = 'preferences';
18
19
    /**
20
     * Notification media.
21
     *
22
     * @var string
23
     */
24
    const NOTIFICATION_MEDIA = 'WEBHOOK';
25
26
    /**
27
     * Initialize a new instance.
28
     */
29
    public function initialize()
30
    {
31
        $this->data = new stdClass();
32
        $this->data->events = [];
33
        $this->data->media = self::NOTIFICATION_MEDIA;
34
    }
35
36
    /**
37
     * Add a new address to the account.
38
     *
39
     * @param string $event Webhook.
40
     *
41
     * @return $this
42
     */
43
    public function addEvent($event)
44
    {
45
        $this->data->events[] = $event;
46
47
        return $this;
48
    }
49
50
    /**
51
     * Set target to notification.
52
     *
53
     * @param string $target Notification URL.
54
     *
55
     * @return $this
56
     */
57
    public function setTarget($target)
58
    {
59
        $this->data->target = $target;
60
61
        return $this;
62
    }
63
64
    /**
65
     * Returns target.
66
     *
67
     * @return stdClass
68
     */
69
    public function getTarget()
70
    {
71
        return $this->data->target;
72
    }
73
74
    /**
75
     * Returns notification id.
76
     *
77
     * @return stdClass
78
     */
79
    public function getId()
80
    {
81
        return $this->data->id;
82
    }
83
84
    /**
85
    * Returns notification token.
86
    *
87
    * @return stdClass
88
    */
89
    public function getToken()
90
    {
91
        return $this->data->token;
92
    }
93
94
    /**
95
     * Create a new notification preference.
96
     *
97
     * @return \stdClass
98
     */
99
    public function create()
100
    {
101
        return $this->createResource($this->generatePath('notifications'));
102
    }
103
104
    /**
105
     * Get a notification preference.
106
     *
107
     * @param string $notification_id Moip notification id.
108
     *
109
     * @return stdClass
110
     */
111
    public function get($notification_id)
112
    {
113
        return $this->getByPath($this->generatePath('notifications', $notification_id));
114
    }
115
116
    /**
117
     * Delete.
118
     *
119
     * @param $notification_id
120
     *
121
     * @return mixed
122
     */
123
    public function delete($notification_id)
124
    {
125
        return $this->deleteByPath($this->generatePath('notifications', $notification_id));
126
    }
127
128
    /**
129
     * Mount the notification preference structure.
130
     *
131
     * @param \stdClass $response
132
     *
133
     * @return \Moip\Resource\NotificationPreferences data
134
     */
135
    protected function populate(stdClass $response)
136
    {
137
        $account = clone $this;
138
        $account->data->events = $this->getIfSet('events', $response);
139
        $account->data->target = $this->getIfSet('target', $response);
140
        $account->data->media = $this->getIfSet('media', $response);
141
        $account->data->token = $this->getIfSet('token', $response);
142
        $account->data->id = $this->getIfSet('id', $response);
143
144
        return $account;
145
    }
146
}
147