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