blib/lib/Net/DAV/Lock.pm

filter: Statementstmt 100% 68 / 68 Branchbran 97.3% 37 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 12 totaltotal 90.0% 118 / 131 CC 20 SCAR 23.7
CC
20
Cov
99%
new
18.0
timeout
2.0
renew
2.0
CRAP 10.7
« blib/lib/Net/DAV/Server.pm ↑ Summary blib/lib/Net/DAV/LockManager.pm »
1 package Net::DAV::Lock;
2  
3 31 ▶use Net::DAV::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 31 our $MAX_LOCK_TIMEOUT = 15 * 60;
6 31 our $DEFAULT_LOCK_TIMEOUT = $MAX_LOCK_TIMEOUT;
7 31 our $DEFAULT_DEPTH = 'infinity'; # as per RFC 4918, section 9.10.3, paragraph 5
8 31 our $DEFAULT_SCOPE = 'exclusive';
9  
10 31 our $VERSION = '1.304';
11 31 $VERSION = eval $VERSION;
12  
13 sub new {
14 110 ▶ my ($class, $hash) = @_;
Subroutine new: called CC=18 SCAR=28.9
Pod undocumented
15 110 my $obj = {};
16  
17 110 my $now = time();
18  
19 110 ▶ die('Missing path value') unless defined $hash->{'path'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash->{"path"} 2 108 110
20 108 $obj->{'path'} = $hash->{'path'};
21  
22 108 ▶ die('Missing creator value') unless defined $hash->{'creator'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash->{"creator"} 1 107 108
23 107 ▶ die('Owner contains invalid characters') unless $hash->{'creator'} =~ /^[a-z_.][-a-z0-9_.]*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $hash->{"creator"} =~ /^[a-z_.][-a-z0-9_.]*$/ 1 106 107
24 106 $obj->{'creator'} = $hash->{'creator'};
25 106 ▶ die('Missing owner value') unless defined $hash->{'owner'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash->{"owner"} 1 105 106
26 105 $obj->{'owner'} = $hash->{'owner'};
27  
28 105 ▶ if (defined $hash->{'expiry'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $hash->{'expiry'}) { } 28 77 105
elsif (defined $hash->{'timeout'}) { } 2 75 77
29 28 ▶ die('Lock expiry is a date in the past') if $hash->{'expiry'} < $now;
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"expiry"} < $now 1 27 28
30 27 ▶ if ($hash->{'expiry'} - $now > $MAX_LOCK_TIMEOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{'expiry'} - $now > $MAX_LOCK_TIMEOUT) { } 9 18 27
31 9 $obj->{'expiry'} = $now + $MAX_LOCK_TIMEOUT;
32 }
33 else {
34 18 $obj->{'expiry'} = $hash->{'expiry'};
35 }
36 } elsif (defined $hash->{'timeout'}) {
37 2 ▶ if ($hash->{'timeout'} > $MAX_LOCK_TIMEOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{'timeout'} > $MAX_LOCK_TIMEOUT) { } 1 1 2
38 1 $obj->{'expiry'} = $now + $MAX_LOCK_TIMEOUT;
39 }
40 else {
41 1 $obj->{'expiry'} = $now + $hash->{'timeout'};
42 }
43 } else {
44 75 $obj->{'expiry'} = $now + $DEFAULT_LOCK_TIMEOUT;
45 }
46  
47 104 ▶ if (defined $hash->{'depth'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash->{'depth'}) { } 52 52 104
48 52 ▶ die('Depth is a non-RFC 4918 value') unless $hash->{'depth'} =~ /^(?:0|infinity)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $hash->{"depth"} =~ /^(?:0|infinity)$/ 1 51 52
49 51 $obj->{'depth'} = $hash->{'depth'};
50 } else {
51 52 $obj->{'depth'} = $DEFAULT_DEPTH;
52 }
53  
54 103 ▶ if (defined $hash->{'scope'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash->{'scope'}) { } 47 56 103
55 47 ▶ die('Scope is an unsupported value') unless $hash->{'scope'} eq 'exclusive';
Branch: 1 branch
BranchTrueFalseTotal
unless $hash->{"scope"} eq "exclusive" 1 46 47
56 46 $obj->{'scope'} = $hash->{'scope'};
57 } else {
58 56 $obj->{'scope'} = $DEFAULT_SCOPE;
59 }
60  
61 102 $obj->{'uri'} = $hash->{'uri'};
62  
63 #
64 # Calculate and store a new UUID based on the path and owner
65 # specified, if none is present.
66 #
67 102 ▶ if ($hash->{'uuid'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hash->{'uuid'}) { } 2 100 102
elsif ($hash->{'token'}) { } 3 97 100
68 2 ▶ unless ($hash->{'uuid'} =~ /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash->{"uuid"} =~ /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/) 1 1 2
69 1 die('UUID is of an invalid format');
70 }
71  
72 1 $obj->{'uuid'} = $hash->{'uuid'};
73 } elsif ($hash->{'token'}) {
74 3 ▶ unless ($hash->{'token'} =~ /^opaquelocktoken:[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash->{"token"} =~ /^opaquelocktoken:[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/) 2 1 3
75 2 die('Token is not a UUID prefixed with the opaquelocktoken: URI namespace');
76 }
77  
78 1 my $uuid = $hash->{'token'};
79 1 $uuid =~ s/^opaquelocktoken://;
80  
81 1 $obj->{'uuid'} = $uuid;
82 } else {
83 97 $obj->{'uuid'} = Net::DAV::UUID::generate(@{$hash}{qw/path owner/});
84 }
85  
86 99 return bless $obj, $class;
87 }
88  
89 #
90 # Provide a separate constructor for reanimating values from the database,
91 # especially when the validation within the normal constructor would be
92 # considered undesired behavior.
93 #
94 # Results in a simple copy of the database row into a blessed object.
95 #
96 sub reanimate {
97 10 ▶ my ($class, $row) = @_;
Subroutine reanimate: called CC=1 SCAR=0.0
Pod undocumented
98  
99 10 bless { %$row }, $class;
100 }
101  
102 125 ▶sub expiry { shift->{'expiry'} };
Subroutine expiry: called CC=1 SCAR=0.0
Pod undocumented
103 61 ▶sub creator { shift->{'creator'} };
Subroutine creator: called CC=1 SCAR=0.0
Pod undocumented
104 13 ▶sub owner { shift->{'owner'} };
Subroutine owner: called CC=1 SCAR=0.0
Pod undocumented
105 88 ▶sub depth { shift->{'depth'} };
Subroutine depth: called CC=1 SCAR=0.0
Pod undocumented
106 14 ▶sub scope { shift->{'scope'} };
Subroutine scope: called CC=1 SCAR=0.0
Pod undocumented
107 501 ▶sub path { shift->{'path'} };
Subroutine path: called CC=1 SCAR=0.0
Pod undocumented
108 137 ▶sub uuid { shift->{'uuid'} };
Subroutine uuid: called CC=1 SCAR=0.0
Pod undocumented
109  
110 #
111 # Return the number of seconds remaining for which this lock is
112 # valid, relative to the current system time.
113 #
114 sub timeout {
115 37 ▶ my ($self) = @_;
Subroutine timeout: called CC=2 SCAR=7.1
Pod undocumented
116  
117 37 my $left = $self->{'expiry'} - time();
118  
119 37 ▶ return $left >= 0? $left: 0;
Branch: 1 branch
BranchTrueFalseTotal
$left >= 0 ? : 37 0 37
120 }
121  
122 #
123 # Provide a wrapper method to return a token URI based on the UUID
124 # stored in the current object.
125 #
126 sub token {
127 125 ▶ my ($self) = @_;
Subroutine token: called CC=1 SCAR=0.0
Pod undocumented
128  
129 125 return 'opaquelocktoken:' . $self->uuid;
130 }
131  
132 #
133 # Update the expiration date of this lock. Throw an error if the update
134 # is not for any time in the future.
135 #
136 # The rationale for providing this method as a means of setting a new
137 # value for the lock expiration date is that without it, the immutable
138 # nature of this class forces the creation of a new lock object, which
139 # would be undesirable as the existing UUID should be preserved.
140 #
141 sub renew {
142 7 ▶ my ($self, $expiry) = @_;
Subroutine renew: called CC=2 SCAR=6.9
Pod undocumented
143  
144 7 ▶ die('New lock expiration date is not in the future') unless $expiry > time();
Branch: 1 branch
BranchTrueFalseTotal
unless $expiry > time 1 6 7
145  
146 6 $self->{'expiry'} = $expiry;
147  
148 6 return $self;
149 }
150  
151 31 1;
152  
153 __END__
« blib/lib/Net/DAV/Server.pm ↑ Summary blib/lib/Net/DAV/LockManager.pm »