File Coverage

lib/Webservice/OVH/Email/Domain/Domain/Redirection.pm
Criterion Covered Total %
statement 15 109 13.7
branch 0 52 0.0
condition 0 5 0.0
subroutine 5 16 31.2
pod 9 9 100.0
total 29 191 15.1


line stmt bran cond sub pod time code
1              
2             =encoding utf-8
3              
4             =head1 NAME
5              
6             Webservice::OVH::Email::Domain::Domain::Redirection
7              
8             =head1 SYNOPSIS
9              
10             use Webservice::OVH;
11            
12             my $ovh = Webservice::OVH->new_from_json("credentials.json");
13            
14             my $email_domain = $ovh->email->domain->domain('testdomain.de');
15            
16             my $redirection = $email_domain->new_redirection(from => 'test@test.de', to => 'test2@test.de', local_copy => 'false');
17              
18             =head1 DESCRIPTION
19              
20             Provides the ability to create, delete and change redirections for an email-domain.
21              
22             =head1 METHODS
23              
24             =cut
25              
26             use strict;
27 36     36   221 use warnings;
  36         82  
  36         854  
28 36     36   152 use Carp qw{ carp croak };
  36         69  
  36         797  
29 36     36   158 use JSON;
  36         72  
  36         1421  
30 36     36   179  
  36         72  
  36         298  
31             our $VERSION = 0.47;
32              
33             use Webservice::OVH::Helper;
34 36     36   5378  
  36         84  
  36         38870  
35             =head2 _new_existing
36              
37             Internal Method to create a Redirection object.
38             This method should never be called directly.
39              
40             =over
41              
42             =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object, $domain - parent domain Objekt, $redirection_id => api intern id
43              
44             =item * Return: L<Webservice::OVH::Email::Domain::Domain::Redirection>
45              
46             =item * Synopsis: Webservice::OVH::Email::Domain::Domain::Redirection->_new_existing($ovh_api_wrapper, $domain, $redirection_id, $module);
47              
48             =back
49              
50             =cut
51              
52              
53             my ( $class, %params ) = @_;
54              
55 0     0     die "Missing module" unless $params{module};
56             die "Missing wrapper" unless $params{wrapper};
57 0 0         die "Missing id" unless $params{id};
58 0 0         die "Missing domain" unless $params{domain};
59 0 0          
60 0 0         my $module = $params{module};
61             my $api_wrapper = $params{wrapper};
62 0           my $redirection_id = $params{id};
63 0           my $domain = $params{domain};
64 0            
65 0           die "Missing redirection_id" unless $redirection_id;
66             my $domain_name = $domain->name;
67 0 0          
68 0           my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $redirection_id, _properties => undef, _domain => $domain }, $class;
69             return $self;
70 0           }
71 0            
72             =head2 _new
73              
74             Internal Method to create the Redirection object.
75             This method should never be called directly.
76              
77             =over
78              
79             =item * Parameter: $api_wrapper - ovh api wrapper object, $module - root object, $domain - parent domain, %params - key => value
80              
81             =item * Return: L<Webservice::OVH::Email::Domain::Domain::Redirection>
82              
83             =item * Synopsis: Webservice::OVH::Email::Domain::Domain::Redirection->_new($ovh_api_wrapper, $domain, $module, from => 'from@test.com', to => 'to@test.com', local_copy => 'false');
84              
85             =back
86              
87             =cut
88              
89              
90             my ( $class, %params ) = @_;
91              
92             die "Missing module" unless $params{module};
93 0     0     die "Missing wrapper" unless $params{wrapper};
94             die "Missing domain" unless $params{domain};
95 0 0          
96 0 0         my $module = $params{module};
97 0 0         my $api_wrapper = $params{wrapper};
98             my $domain = $params{domain};
99 0            
100 0           my @keys_needed = qw{ from local_copy to };
101 0            
102             if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) {
103 0            
104             croak "Missing parameter: @missing_parameters";
105 0 0         }
  0            
106              
107 0           my $local_copy = $params{local_copy} eq 'true' || $params{local_copy} eq '1' || $params{local_copy} eq 'yes' ? JSON::true : JSON::false;
108              
109             my $domain_name = $domain->name;
110 0 0 0       my $body = {};
111             $body->{from} = Webservice::OVH::Helper->trim($params{from});
112 0           $body->{to} = Webservice::OVH::Helper->trim($params{to});
113 0           $body->{localCopy} = $local_copy;
114 0           my $response = $api_wrapper->rawCall( method => 'post', path => "/email/domain/$domain_name/redirection/", body => $body, noSignature => 0 );
115 0           croak $response->error if $response->error;
116 0            
117 0           my $redirection_id = $response->{id};
118 0 0          
119             my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $redirection_id, _properties => undef, _domain => $domain }, $class;
120 0            
121             return $self;
122 0           }
123              
124 0           =head2 is_valid
125              
126             When this redirection is deleted on the api side, this method returns 0.
127              
128             =over
129              
130             =item * Return: VALUE
131              
132             =item * Synopsis: print "Valid" if $redirection->is_valid;
133              
134             =back
135              
136             =cut
137              
138              
139             my ($self) = @_;
140              
141             $self->properties;
142              
143 0     0 1   return $self->{_valid};
144             }
145 0            
146             =head2 id
147 0            
148             Returns the api id.
149              
150             =over
151              
152             =item * Return: VALUE
153              
154             =item * Synopsis: my $id = $redirection->id;
155              
156             =back
157              
158             =cut
159              
160              
161             my ($self) = @_;
162              
163             return $self->{_id};
164             }
165              
166 0     0 1   =head2 domain
167              
168 0           Returns the email-domain this redirection is attached to.
169              
170             =over
171              
172             =item * Return: L<Webservice::Email::Domain::Domain>
173              
174             =item * Synopsis: my $email_domain = $redirection->domain;
175              
176             =back
177              
178             =cut
179              
180              
181             my ($self) = @_;
182              
183             return $self->{_domain};
184             }
185              
186             =head2 properties
187 0     0 1    
188             Returns the raw properties as a hash.
189 0           This is the original return value of the web-api.
190              
191             =over
192              
193             =item * Return: HASH
194              
195             =item * Synopsis: my $properties = $record->properties;
196              
197             =back
198              
199             =cut
200              
201              
202             my ($self) = @_;
203              
204             return unless $self->{_valid};
205              
206             my $api = $self->{_api_wrapper};
207             my $domain_name = $self->domain->name;
208             my $redirection_id = $self->id;
209 0     0 1   my $response = $api->rawCall( method => 'get', path => "/email/domain/$domain_name/redirection/$redirection_id", noSignature => 0 );
210             carp $response->error if $response->error;
211 0 0          
212             if ( $response->error ) {
213 0            
214 0           $self->{_valid} = 0;
215 0           $self->{_properties} = undef;
216 0           return;
217 0 0          
218             } else {
219 0 0          
220             $self->{_properties} = $response->content;
221 0           return $self->{_properties};
222 0           }
223 0           }
224              
225             =head2 field_type
226              
227 0           Exposed property value.
228 0            
229             =over
230              
231             =item * Return: VALUE
232              
233             =item * Synopsis: my $from = $record->from;
234              
235             =back
236              
237             =cut
238              
239              
240             my ($self) = @_;
241              
242             $self->properties unless $self->{_properties};
243             return unless $self->{_valid};
244              
245             return $self->{_properties}->{from};
246             }
247              
248 0     0 1   =head2 to
249              
250 0 0         Exposed property value.
251 0 0          
252             =over
253 0            
254             =item * Return: VALUE
255              
256             =item * Synopsis: my $to = $record->to;
257              
258             =back
259              
260             =cut
261              
262              
263             my ($self) = @_;
264              
265             $self->properties unless $self->{_properties};
266             return unless $self->{_valid};
267              
268             return $self->{_properties}->{to};
269             }
270              
271             =head2 delete
272 0     0 1    
273             Deletes the redirection api sided and sets this object invalid.
274 0 0          
275 0 0         =over
276              
277 0           =item * Synopsis: $redirection->delete;
278              
279             =back
280              
281             =cut
282              
283              
284             my ($self) = @_;
285              
286             return unless $self->{_valid};
287              
288             my $api = $self->{_api_wrapper};
289             my $domain_name = $self->domain->name;
290             my $redirection_id = $self->id;
291             my $response = $api->rawCall( method => 'delete', path => "/email/domain/$domain_name/redirection/$redirection_id", noSignature => 0 );
292             croak $response->error if $response->error;
293              
294 0     0 1   $self->{_valid} = 0;
295             }
296 0 0          
297             =head2 change
298 0            
299 0           Changes the redirection
300 0            
301 0           =over
302 0 0          
303             =item * Parameter: %params - key => value to
304 0            
305             =item * Synopsis: $redirection->change(to => 'test@test.de');
306              
307             =back
308              
309             =cut
310              
311              
312             my ( $self, $to ) = @_;
313              
314             return unless $self->{_valid};
315              
316             croak "Missing to as parameter" unless $to;
317              
318             my $api = $self->{_api_wrapper};
319             my $domain_name = $self->domain->name;
320             my $redirection_id = $self->id;
321             my $body = {};
322             $body->{to} = Webservice::OVH::Helper->trim($to);
323 0     0 1   my $response = $api->rawCall( method => 'post', path => "/email/domain/$domain_name/redirection/$redirection_id/changeRedirection", body => $body, noSignature => 0 );
324             croak $response->error if $response->error;
325 0 0          
326             my $task_id = $response->content->{id};
327 0 0         my $task = Webservice::OVH::Email::Domain::Domain::Task::Redirection->_new_existing( wrapper => $api, domain => $self->domain, id => $task_id, module => $self->{_module} );
328              
329 0           return $task;
330 0           }
331 0            
332 0           =head2 tasks
333 0            
334 0           Get all associated tasks
335 0 0          
336             =over
337 0            
338 0           =item * Return: HASH
339              
340 0           =item * Synopsis: $mailinglist->tasks;
341              
342             =back
343              
344             =cut
345              
346              
347             my ($self) = @_;
348              
349             return unless $self->{_valid};
350              
351             my $domain_name = $self->domain->name;
352             my $api = $self->{_api_wrapper};
353             my $id = $self->id;
354              
355             my $response = $api->rawCall( method => 'get', path => sprintf( "/email/domain/$domain_name/task/redirection?account=%s", $id ), noSignature => 0 );
356             croak $response->error if $response->error;
357              
358             my $taks = $response->content || [];
359 0     0 1    
360             return unless scalar @$taks;
361 0 0          
362             return $taks;
363 0            
364 0           }
365 0            
366             1;