|
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
|
|
|
* Create a new notification preference. |
|
86
|
|
|
* |
|
87
|
|
|
* @return \stdClass |
|
88
|
|
|
*/ |
|
89
|
|
|
public function create() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->createResource($this->generatePath('notifications')); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get a notification preference. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $notification_id Moip notification id. |
|
98
|
|
|
* |
|
99
|
|
|
* @return stdClass |
|
100
|
|
|
*/ |
|
101
|
|
|
public function get($notification_id) |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->getByPath($this->generatePath('notifications', $notification_id)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Delete. |
|
108
|
|
|
* |
|
109
|
|
|
* @param $notification_id |
|
110
|
|
|
* |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
|
|
public function delete($notification_id) |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->deleteByPath($this->generatePath('notifications', $notification_id)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Generate URL to request. |
|
120
|
|
|
* |
|
121
|
|
|
* @param $method |
|
122
|
|
|
* @param $id |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function generatePath($method, $id = null) |
|
127
|
|
|
{ |
|
128
|
|
|
if (!is_null($id)) { |
|
129
|
|
|
return sprintf('%s/%s/%s/%s', MoipResource::VERSION, self::PATH, $method, $id); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return sprintf('%s/%s/%s', MoipResource::VERSION, self::PATH, $method); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Mount the notification preference structure. |
|
137
|
|
|
* |
|
138
|
|
|
* @param \stdClass $response |
|
139
|
|
|
* |
|
140
|
|
|
* @return \Moip\Resource\NotificationPreferences data |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function populate(stdClass $response) |
|
143
|
|
|
{ |
|
144
|
|
|
$account = clone $this; |
|
145
|
|
|
$account->data->events = $this->getIfSet('events', $response); |
|
146
|
|
|
$account->data->target = $this->getIfSet('target', $response); |
|
147
|
|
|
$account->data->media = $this->getIfSet('media', $response); |
|
148
|
|
|
$account->data->token = $this->getIfSet('token', $response); |
|
149
|
|
|
$account->data->id = $this->getIfSet('id', $response); |
|
150
|
|
|
|
|
151
|
|
|
return $account; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|