Completed
Push — master ( 1f0278...22c246 )
by
unknown
12s
created

NotificationPreferences::getList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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 string
68
     */
69
    public function getTarget()
70
    {
71
        return $this->data->target;
72
    }
73
74
    /**
75
     * Returns media.
76
     *
77
     * @return string
78
     */
79
    public function getMedia()
80
    {
81
        return $this->data->media;
82
    }
83
84
    /**
85
     * Returns events.
86
     *
87
     * @return array
88
     */
89
    public function getEvents()
90
    {
91
        return $this->data->events;
92
    }
93
94
    /**
95
     * Returns notification id.
96
     *
97
     * @return stdClass
98
     */
99
    public function getId()
100
    {
101
        return $this->data->id;
102
    }
103
104
    /**
105
     * Returns notification token.
106
     *
107
     * @return stdClass
108
     */
109
    public function getToken()
110
    {
111
        return $this->data->token;
112
    }
113
114
    /**
115
     * Create a new notification preference.
116
     *
117
     * @return \stdClass
118
     */
119
    public function create()
120
    {
121
        return $this->createResource($this->generatePath('notifications'));
122
    }
123
124
    /**
125
     * Get a notification preference.
126
     *
127
     * @param string $notification_id Moip notification id.
128
     *
129
     * @return stdClass
130
     */
131
    public function get($notification_id)
132
    {
133
        return $this->getByPath($this->generatePath('notifications', $notification_id));
134
    }
135
136
    /**
137
     * Create a new Notifications List instance.
138
     *
139
     * @return \Moip\Resource\NotificationPreferencesList
140
     */
141
    public function getList()
142
    {
143
        $notificationList = new NotificationPreferencesList($this->moip);
144
145
        return $notificationList->get();
146
    }
147
148
    /**
149
     * Delete.
150
     *
151
     * @param $notification_id
152
     *
153
     * @return mixed
154
     */
155
    public function delete($notification_id)
156
    {
157
        return $this->deleteByPath($this->generatePath('notifications', $notification_id));
158
    }
159
160
    /**
161
     * Mount the notification preference structure.
162
     *
163
     * @param \stdClass $response
164
     *
165
     * @return \Moip\Resource\NotificationPreferences data
166
     */
167
    protected function populate(stdClass $response)
168
    {
169
        $account = clone $this;
170
        $account->data->events = $this->getIfSet('events', $response);
171
        $account->data->target = $this->getIfSet('target', $response);
172
        $account->data->media = $this->getIfSet('media', $response);
173
        $account->data->token = $this->getIfSet('token', $response);
174
        $account->data->id = $this->getIfSet('id', $response);
175
176
        return $account;
177
    }
178
}
179