Message.pm

filter: Statementstmt 45.1% 28 / 62 Branchbran 33.3% 8 / 24 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 72.7% 8 / 11 Podpod 100% 8 / 8 totaltotal 45.0% 54 / 120 CC 16 SCAR 24.3
CC
16
Cov
40%
commit
33.7
delete
12.0
get_messages
6.0
CRAP 11.4
« Config.pm ↑ Summary Resource/Location/Type.pm »
1 package MyLibrary::Message;
2  
3 1 ▶use MyLibrary::DB;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 MyLibrary::Message
10  
11 =head1 SYNOPSIS
12  
13 use MyLibrary::Message;
14  
15 =head1 DESCRIPTION
16  
17 Use this module to get and set messages from the librarians to a MyLibrary database.
18  
19 =head1 METHODS
20  
21 =head2 new()
22  
23 =head2 message_id()
24  
25 =head2 message_date()
26  
27 =head2 message()
28  
29 =head2 message_global()
30  
31 message_global is a Boolean value. Set it to true ('1') to make this message intended for everybody. Set it to false ('2') to make it not. False is the default. Call message_global without any parameters to get it's value.
32  
33 =head2 commit()
34  
35 =head2 delete()
36  
37 =head2 get_messages()
38  
39 =head1 HISTORY
40  
41 This module is legacy code. I sometimes wonder if it is even needed, and the message_global method feels like a hack.
42  
43 =head1 AUTHOR
44  
45 Eric Lease Morgan
46  
47  
48 =cut
49  
50  
51 1 ▶sub new {
Subroutine new: called CC=3 SCAR=16.3
Pod documented
52  
53 # declare local variables
54 1 my ($class, %opts) = @_;
55 1 my $self = {};
56  
57 # check for an id
58 1 ▶ if ($opts{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"id"}) 0 1 1
59
60 # get a handle
61 0 my $dbh = MyLibrary::DB->dbh();
62
63 # find this record
64 0 my $rv = $dbh->selectrow_hashref('SELECT * FROM messages WHERE message_id = ?', undef, $opts{id});
65
66 # check for success
67 0 ▶ if (ref($rv) eq "HASH") { $self = $rv }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rv eq 'HASH') { } 0 0 0
68 0 else { return }
69
70 }
71
72 # return the object
73 1 return bless $self, $class;
74
75 }
76  
77  
78 0 ▶sub message_id {
Subroutine message_id: not called CC=1 SCAR=6.9
Pod documented
79  
80 0 my $self = shift;
81 0 return $self->{message_id};
82  
83 }
84  
85  
86 2 ▶sub message_date {
Subroutine message_date: called CC=2 SCAR=6.9
Pod documented
87  
88 # declare local variables
89 2 my ($self, $message_date) = @_;
90
91 # check for the existance of a date
92 2 ▶ if ($message_date) { $self->{message_date} = $message_date }
Branch: 1 branch
BranchTrueFalseTotal
if ($message_date) 1 1 2
93
94 # return it
95 2 return $self->{message_date};
96
97 }
98  
99  
100 2 ▶sub message {
Subroutine message: called CC=2 SCAR=6.9
Pod documented
101  
102 # declare local variables
103 2 my ($self, $message) = @_;
104
105 # check for the existance of a message
106 2 ▶ if ($message) { $self->{message} = $message }
Branch: 1 branch
BranchTrueFalseTotal
if ($message) 1 1 2
107
108 # return the date
109 2 return $self->{message};
110
111 }
112  
113  
114 4 ▶sub message_global {
Subroutine message_global: called CC=4 SCAR=14.5
Pod documented
115  
116 # declare local variables
117 4 my ($self, $message_global) = @_;
118
119 # check for the existance of a message
120 4 ▶ if ( ! $message_global ) { }
Branch: 2 branches
BranchTrueFalseTotal
if (not $message_global) { } 2 2 4
elsif (lc $message_global eq '1' or lc $message_global eq '2') { } 2 0 2
121 2 ▶ elsif ( lc( $message_global ) eq '1' || lc( $message_global ) eq '2' ) { $self->{message_global} = $message_global }
Condition66%2/3
lc $message_global eq 1 or lc $message_global eq 2
l!l&&r!l&&!r
110
MC/DC0%0/2
lc $message_global eq 1 or lc $message_global eq 2
lc $message_global eq 1 lc $message_global eq 2
Truth table66%2/3
A or B
A= lc $message_global eq 1B= lc $message_global eq 2
ABresult
1X1
011
000
122 0 else { croak("Invalid value for message_global: $message_global. Valid values are 1 (true) or 2 (false).") }
123
124 # return the global message flag
125 4 return $self->{message_global};
126
127 }
128  
129  
130 1 ▶sub commit {
Subroutine commit: called CC=6 SCAR=35.2
Pod documented
131  
132 # get myself, :-)
133 1 my $self = shift;
134
135 # get a database handle
136 1 my $dbh = MyLibrary::DB->dbh();
137
138 # see if the object has an id
139 0 ▶ if ($self->message_id()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->message_id) { } 0 0 0
140
141 # update the record with this id
142 0 my $return = $dbh->do('UPDATE messages SET message = ?, message_date = ?, message_global = ? WHERE message_id = ?', undef, $self->message(), $self->message_date(), $self->message_global(), $self->message_id());
143 0 ▶ if ($return > 1 || ! $return) { croak "Message update in commit() failed. $return records were updated." }
Branch: 1 branch
BranchTrueFalseTotal
if ($return > 1 or not $return) 0 0 0
Condition0%0/3
$return > 1 or not $return
l!l&&r!l&&!r
000
MC/DC0%0/2
$return > 1 or not $return
$return > 1 not $return
Truth table0%0/3
A or not(B)
A= $return > 1B= not $return
ABresult
1X1
011
000
144
145 }
146
147 else {
148
149 # get a new sequence
150 0 my $id = MyLibrary::DB->nextID();
151
152 # create a new record
153 0 my $return = $dbh->do('INSERT INTO messages (message_id, message, message_date, message_global) VALUES (?, ?, ?, ?)', undef, $id, $self->message(), $self->message_date(), $self->message_global());
154 0 ▶ if ($return > 1 || ! $return) { croak 'Message commit() failed.'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($return > 1 or not $return) 0 0 0
Condition0%0/3
$return > 1 or not $return
l!l&&r!l&&!r
000
MC/DC0%0/2
$return > 1 or not $return
$return > 1 not $return
Truth table0%0/3
A or not(B)
A= $return > 1B= not $return
ABresult
1X1
011
000
155 0 $self->{message_id} = $id;
156
157 }
158
159 # done
160 0 return 1;
161
162 }
163  
164  
165 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
166  
167 0 my $self = shift;
168  
169 0 ▶ if ($self->{message_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"message_id"}) 0 0 0
170  
171 0 my $dbh = MyLibrary::DB->dbh();
172 0 my $rv = $dbh->do('DELETE FROM messages WHERE message_id = ?', undef, $self->{message_id});
173 0 ▶ if ($rv != 1) {croak ("Deleted $rv records. I'll bet this isn't what you wanted.");}
Branch: 1 branch
BranchTrueFalseTotal
if ($rv != 1) 0 0 0
174 0 return 1;
175  
176 }
177  
178 0 return 0;
179  
180 }
181  
182  
183 0 ▶sub get_messages {
Subroutine get_messages: not called CC=2 SCAR=17.9
Pod documented
184  
185 0 my $self = shift;
186 0 my @rv = ();
187
188 # create and execute a query
189 0 my $dbh = MyLibrary::DB->dbh();
190 0 my $rows = $dbh->prepare('SELECT * FROM messages ORDER BY message_date');
191 0 $rows->execute;
192
193 # process each found row
194 0 ▶ while (my $row = $rows->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $rows->fetchrow_hashref) 0 0 0
195
196 0 push (@rv, bless ($row, 'MyLibrary::Message'));
197
198 }
199
200 0 return @rv;
201
202 }
203  
204  
205 # return true, or else
206 1 1;
« Config.pm ↑ Summary Resource/Location/Type.pm »