Fritz/Device.pm

filter: Statementstmt 100% 123 / 123 Branchbran 100% 28 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 100% 171 / 171 CC 28 SCAR 14.2
CC
28
Cov
100%
dump
7.0
get_service
5.0
find_service
5.0
CRAP 4.1
« Fritz/Service.pm ↑ Summary Fritz/Action.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 # Copyright (C) 2015 Christian Garbs <mitch@cgarbs.de>
4 # Licensed under GNU GPL v2 or later.
5  
6 package Net::Fritz::Device;
7 # ABSTRACT: represents a TR064 device
8 3 $Net::Fritz::Device::VERSION = 'v0.0.7';
9  
10 3 ▶use Net::Fritz::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Net::Fritz::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Net::Fritz::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 with 'Net::Fritz::IsNoError';
17  
18  
19 3 has fritz => ( is => 'ro' );
20  
21  
22 3 has xmltree => ( is => 'ro' );
23  
24  
25 3 has service_list => ( is => 'lazy', init_arg => undef );
26  
27 sub _build_service_list {
28 28 ▶ my $self = shift;
Subroutine _build_service_list: called CC=3 SCAR=11.0
29 28 my $xml = $self->xmltree;
30 28 my @services;
31  
32 28 ▶ if (exists $xml->{serviceList}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $xml->{"serviceList"}) 26 2 28
33 26 foreach my $service (@{$xml->{serviceList}->[0]->{service}}) {
34 35 push @services, Net::Fritz::Service->new(
35 xmltree => $service,
36 fritz => $self->fritz
37 );
38 }
39 }
40  
41 28 return \@services;
42 }
43  
44  
45 3 has device_list => ( is => 'lazy', init_arg => undef );
46  
47 sub _build_device_list {
48 31 ▶ my $self = shift;
Subroutine _build_device_list: called CC=3 SCAR=11.0
49 31 my $xml = $self->xmltree;
50 31 my @devices;
51  
52 31 ▶ if (exists $xml->{deviceList}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $xml->{"deviceList"}) 19 12 31
53 19 foreach my $device (@{$xml->{deviceList}->[0]->{device}}) {
54 29 push @devices, Net::Fritz::Device->new(
55 xmltree => $device,
56 fritz => $self->fritz
57 );
58 }
59 }
60  
61 31 return \@devices;
62 }
63  
64  
65 3 has attributes => ( is => 'lazy', init_arg => undef );
66  
67 3 use constant ATTRIBUTES => qw(
68 deviceType
69 friendlyName
70 manufacturer
71 manufacturerURL
72 modelDescription
73 modelName
74 modelNumber
75 modelURL
76 UDN
77 presentationURL
78 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 sub _build_attributes {
81 11 ▶ my $self = shift;
Subroutine _build_attributes: called CC=3 SCAR=11.0
82 11 my $xml = $self->xmltree;
83 11 my $attributes = {};
84  
85 11 for my $attr (ATTRIBUTES) {
86 110 ▶ if (exists $xml->{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $xml->{$attr}) 21 89 110
87 21 $attributes->{$attr} = $xml->{$attr}->[0];
88 }
89 }
90  
91 11 return $attributes;
92 }
93  
94  
95 sub get_service {
96 8 ▶ my $self = shift;
Subroutine get_service: called CC=5 SCAR=16.1
Pod documented
97 8 my $type = shift;
98  
99 8 foreach my $service (@{$self->service_list}) {
100 10 ▶ if ($service->serviceType eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service->serviceType eq $type) 2 8 10
101 2 return $service;
102 }
103 }
104  
105 6 foreach my $device (@{$self->device_list}) {
106 5 my $service = $device->get_service($type);
107 5 ▶ if (! $service->error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service->error) 2 3 5
108 2 return $service;
109 }
110 }
111
112 4 return Net::Fritz::Error->new('service not found');
113 }
114  
115  
116 sub find_service {
117 8 ▶ my $self = shift;
Subroutine find_service: called CC=5 SCAR=16.1
Pod documented
118 8 my $type = shift;
119  
120 8 foreach my $service (@{$self->service_list}) {
121 10 ▶ if ($service->serviceType =~ /$type/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service->serviceType =~ /$type/) 2 8 10
122 2 return $service;
123 }
124 }
125  
126 6 foreach my $device (@{$self->device_list}) {
127 5 my $service = $device->find_service($type);
128 5 ▶ if (! $service->error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service->error) 2 3 5
129 2 return $service;
130 }
131 }
132  
133 4 return Net::Fritz::Error->new('service not found');
134 }
135  
136  
137 sub find_service_names {
138 8 ▶ my $self = shift;
Subroutine find_service_names: called CC=4 SCAR=13.9
Pod documented
139 8 my $type = shift;
140  
141 8 my @found = ();
142  
143 8 foreach my $service (@{$self->service_list}) {
144 10 ▶ if ($service->serviceType =~ /$type/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service->serviceType =~ /$type/) 4 6 10
145 4 push @found, $service;
146 }
147 }
148  
149 8 foreach my $device (@{$self->device_list}) {
150 6 my $data = $device->find_service_names($type);
151 6 push @found, @{$data->data};
152 }
153  
154 8 return Net::Fritz::Data->new(\@found);
155 }
156  
157  
158 sub find_device {
159 7 ▶ my $self = shift;
Subroutine find_device: called CC=5 SCAR=16.1
Pod documented
160 7 my $type = shift;
161  
162 7 foreach my $device (@{$self->device_list}) {
163 8 ▶ if ($device->attributes->{deviceType} eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($device->attributes->{"deviceType"} eq $type) 2 6 8
164 2 return $device;
165 }
166 }
167
168 5 foreach my $device (@{$self->device_list}) {
169 4 my $device = $device->find_device($type);
170 4 ▶ if (! $device->error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($device->error) 1 3 4
171 1 return $device;
172 }
173 }
174
175 4 return Net::Fritz::Error->new( 'device not found' );
176 }
177  
178  
179 sub dump {
180 2 ▶ my $self = shift;
Subroutine dump: called CC=7 SCAR=19.5
Pod documented
181  
182 2 my $indent = shift;
183 2 ▶ $indent = '' unless defined $indent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent 1 1 2
184  
185 2 my $text = "${indent}Net::Fritz::Device:\n";
186 2 $indent .= ' ';
187 2 $text .= "${indent}modelName = " . $self->attributes->{modelName} . "\n";
188 2 ▶ $text .= "${indent}presentationURL = " . $self->attributes->{presentationURL} . "\n" if defined $self->attributes->{presentationURL};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->attributes->{"presentationURL"} 1 1 2
189  
190 2 my @service_list = @{$self->service_list};
191 2 ▶ if (@service_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@service_list) 1 1 2
192 1 $text .= "${indent}subservices = {\n";
193 1 foreach my $service (@service_list) {
194 1 $text .= $service->dump($indent . ' ');
195 }
196 1 $text .= "${indent}}\n";
197 }
198  
199 2 my @device_list = @{$self->device_list};
200 2 ▶ if (@device_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@device_list) 1 1 2
201 1 $text .= "${indent}subdevices = {\n";
202 1 foreach my $device (@device_list) {
203 1 $text .= $device->dump($indent . ' ');
204 }
205 1 $text .= "${indent}}\n";
206 }
207  
208 2 return $text;
209 }
210  
211  
212 3 1;
213  
214 __END__
« Fritz/Service.pm ↑ Summary Fritz/Action.pm »