File Coverage

blib/lib/EWS/Contacts/Role/Reader.pm
Criterion Covered Total %
statement 13 36 36.1
branch 0 14 0.0
condition 0 8 0.0
subroutine 5 10 50.0
pod 0 1 0.0
total 18 69 26.0


line stmt bran cond sub pod time code
1             package EWS::Contacts::Role::Reader;
2             BEGIN {
3 1     1   806 $EWS::Contacts::Role::Reader::VERSION = '1.143070';
4             }
5 1     1   454 use Moose::Role;
  1         4529  
  1         9  
6              
7 1     1   6388 use EWS::Contacts::ResultSet;
  1         3  
  1         40  
8 1     1   7 use Carp;
  1         1  
  1         411  
9              
10             sub _list_messages {
11 0     0     my ($self, $kind, $response) = @_;
12 0           return @{ $response->{"${kind}Result"}
  0            
13             ->{ResponseMessages}
14             ->{cho_CreateItemResponseMessage} };
15             }
16              
17             sub _check_for_errors {
18 0     0     my ($self, $kind, $response) = @_;
19              
20 0           foreach my $msg ( $self->_list_messages($kind, $response) ) {
21 0   0       my $code = $msg->{"${kind}ResponseMessage"}->{ResponseCode} || '';
22 0 0         croak "Fault returned from Exchange Server: $code\n"
23             if $code ne 'NoError';
24             }
25             }
26              
27             sub _list_contactitems {
28 0     0     my ($self, $kind, $response) = @_;
29              
30 0 0         return map { $_->{Contact} }
  0            
31 0 0         grep { defined $_->{'Contact'}->{'DisplayName'} and length $_->{'Contact'}->{'DisplayName'} }
32 0 0         map { @{ $_->{Items}->{cho_Item} || [] } }
  0            
33 0           map { exists $_->{RootFolder} ? $_->{RootFolder} : $_ }
34 0           map { $_->{"${kind}ResponseMessage"} }
35             $self->_list_messages($kind, $response);
36             }
37              
38             sub _get_contacts {
39 0     0     my ($self, $opts) = @_;
40              
41 0 0         return scalar $self->client->FindItem->(
    0          
42             (exists $opts->{impersonate} ? (
43             Impersonation => {
44             ConnectingSID => {
45             PrimarySmtpAddress => $opts->{impersonate},
46             }
47             },
48             ) : ()),
49             RequestVersion => {
50             Version => $self->client->server_version,
51             },
52             ItemShape => { BaseShape => 'AllProperties' },
53             ParentFolderIds => {
54             cho_FolderId => [
55             { DistinguishedFolderId =>
56             {
57             Id => 'contacts',
58             (exists $opts->{email} ? (
59             Mailbox => {
60             EmailAddress => $opts->{email},
61             },
62             ) : ()), # optional
63             }
64             }
65             ]
66             },
67             Traversal => 'Shallow',
68             );
69             }
70              
71             # find primarysmtp if passed an account id.
72             # then find contacts in the account.
73             sub retrieve {
74 0     0 0   my ($self, $opts) = @_;
75              
76 0           my $get_response = $self->_get_contacts($opts);
77              
78 0 0 0       if (exists $get_response->{'ResponseCode'} and defined $get_response->{'ResponseCode'}
      0        
79             and $get_response->{'ResponseCode'} eq 'ErrorNonPrimarySmtpAddress') {
80              
81 0           $self->retrieve({
82             %$opts,
83             email => $get_response->{'MessageXml'}->{'Value'}->{'_'},
84             });
85             }
86              
87 0           $self->_check_for_errors('FindItem', $get_response);
88              
89 0           return EWS::Contacts::ResultSet->new({
90             items => [ $self->_list_contactitems('FindItem', $get_response) ]
91             });
92             }
93              
94 1     1   4 no Moose::Role;
  1         2  
  1         10  
95             1;