Patron/Links.pm

filter: Statementstmt 13.3% 10 / 75 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 27.2% 3 / 11 Podpod 100% 8 / 8 totaltotal 15.6% 21 / 134 CC 18 SCAR 28.7
CC
18
Cov
9%
commit
42.0
get_links
20.0
new
12.0
CRAP 17.6
« Auth.pm ↑ Summary Stylesheet.pm »
1 package MyLibrary::Patron::Links;
2  
3 3 ▶use MyLibrary::DB;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 MyLibrary::Patron::Links;
10  
11 =head1 SYNOPSIS
12  
13 # require the necessary module
14 use MyLibrary::Patron::Links;
15  
16 # create a new patron link
17 my $patron_link = MyLibrary::Patron::Links->new();
18  
19 # get a link id
20 my $link_id = $patron_link->link_id();
21  
22 # set the attributes of the link
23 $patron_link->link_name('CNN');
24 $patron_link->link_url('http://my.site.com');
25 $patron_link->patron_id(23);
26  
27 # save link to database
28 $patron_link->commit();
29  
30 # get all links for a patron
31 my @patron_links = MyLibrary::Patron::Links->get_links(patron_id => $patron_id);
32  
33 # delete a link from the database
34 $patron_link->delete();
35  
36 =head1 DESCRIPTION
37  
38 This is a sub module for creating and manipulating personal patron links. Every link has a name, which is the URL display text, and the URL href itself. Every link must be associated with a particular patron. The module also allows for the retrieval of the complete list of links associated with a patron. The list will be alphabetized according to link name.
39  
40 =head1 METHODS
41  
42 =head2 new()
43  
44 This class method is the constructor for this package. The method is responsible for initializing all attributes associated with a
45 given Patron::Link object. The method can also be used to create a Patron object using a patron link id.
46  
47 =head2 link_id()
48  
49 This method is used exclusively to retrieve an exising patron link database id, if the patron link has been committed to the database. This method may not be used to set the link id in the database.
50  
51 # get a link id
52 my $link_id = $patron_link->link_id();
53  
54 =head2 link_name()
55  
56 This method may be used to either get or set a link name. This is a required attribute, meaning that the object cannot be commited to the database if this attribute is left null.
57  
58 # set the link name
59 $patron_link->link_name('CNN');
60  
61 # get the link name
62 my $link_name = $patron_link->link_name();
63  
64 =head2 link_url()
65  
66 This attribute method should be used to either set or get the url associated with this patron link.
67  
68 # set the link URL
69 $patron_link->link_URL('http://my.favoriteplace.com');
70  
71 # get the link URL
72 my $link_URL = $patron_link->link_URL();
73  
74 =head2 patron_id();
75  
76 This method is used to set or get the patron id associated with a patron link. The patron must exist in the database or this method will throw an exception. The required attribute is the patron id, if setting the id. Otherwise, this method will always return the numeric patron id associated with the link.
77  
78 # set the patron id
79 $patron_link->patron_id(23);
80  
81 # get the patron id
82 my $patron_id = $patron_link->patron_id();
83  
84 =head2 commit()
85  
86 This method commits the patron link to the database. This method requires no arguments.
87  
88 # commit the patron link
89 $patron_link->commit();
90  
91 =head2 delete()
92  
93 Use this method to delete a particular patron link from the database.
94  
95 # delete the patron link
96 $patron_link->delete();
97  
98 =head2 get_links
99  
100 This is a class method that will return a list of patron link ids in link name order. The only required argument is the patron id. This method will return undef if no links exist corresponding to the patron id submitted.
101  
102 # get a list of patron link ids in name order
103 my @patron_links = MyLibrary::Patron::Links->get_links(patron_id => $patron_id);
104  
105 =head1 AUTHORS
106  
107 Robert Fox <rfox2@nd.edu>
108  
109 =cut
110  
111 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
112  
113 # declare a few variables
114 0 my ($class, %opts) = @_;
115 0 my $self = {};
116  
117 # check for an id
118 0 ▶ if ($opts{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"id"}) 0 0 0
119
120 # find this record
121 0 my $dbh = MyLibrary::DB->dbh();
122 0 my $rv = $dbh->selectrow_hashref('SELECT * FROM personallinks where link_id = ?', undef, $opts{id});
123 0 ▶ if (ref($rv) eq "HASH") { $self = $rv }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rv eq 'HASH') { } 0 0 0
124 0 else { return }
125 }
126  
127 # return the object
128 0 return bless $self, $class;
129  
130 }
131  
132 0 ▶sub link_name {
Subroutine link_name: not called CC=2 SCAR=17.9
Pod documented
133  
134 0 my ($self, $name) = @_;
135 0 ▶ if ($name) {$self->{link_name} = $name }
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 0 0 0
136 0 else { return $self->{link_name} }
137  
138 }
139  
140 0 ▶sub link_url {
Subroutine link_url: not called CC=3 SCAR=24.8
Pod documented
141  
142 0 my ($self, $url) = @_;
143 0 ▶ if ($url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url) { } 0 0 0
144 0 ▶ unless ($url =~ /^http:.+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ /^http:.+/) 0 0 0
145 0 croak ( 'Patron link URL does not begin with http.' );
146 }
147 0 $self->{link_url} = $url;
148 0 return $self->{link_url};
149 } else {
150 0 return $self->{link_url};
151 }
152  
153 }
154  
155 0 ▶sub patron_id {
Subroutine patron_id: not called CC=3 SCAR=24.8
Pod documented
156  
157 0 my ($self, $patron_id) = @_;
158 0 my $dbh = MyLibrary::DB->dbh();
159 0 ▶ if ($patron_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($patron_id) { } 0 0 0
160 0 my @patron_array = $dbh->selectrow_array('SELECT * FROM patrons WHERE patron_id = ?', undef, $patron_id);
161 0 ▶ unless (scalar(@patron_array)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @patron_array) 0 0 0
162 0 croak ("Submitted patron id in patron_id(), $patron_id, does not correspond to an exising patron." );
163 }
164 0 $self->{patron_id} = $patron_id;
165 0 return $self->{patron_id};
166  
167 } else {
168  
169 0 return $self->{patron_id};
170  
171 }
172  
173 }
174  
175 0 ▶sub link_id {
Subroutine link_id: not called CC=1 SCAR=6.9
Pod documented
176
177 0 my $self = shift;
178 0 return $self->{link_id};
179  
180 }
181  
182 0 ▶sub commit {
Subroutine commit: not called CC=6 SCAR=37.4
Pod documented
183  
184 0 my $self = shift;
185 0 my $dbh = MyLibrary::DB->dbh();
186  
187 0 ▶ if ($self->link_id()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->link_id) { } 0 0 0
188  
189 0 my $return = $dbh->do('UPDATE personallinks SET link_name = ?, link_url = ?, patron_id = ? WHERE link_id = ?', undef, $self->link_name(), $self->link_url(), $self->patron_id(), $self->link_id());
190  
191 0 ▶ if ($return > 1 || ! $return) { croak "Patron link 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
192  
193 } else {
194  
195 0 my $id = MyLibrary::DB->nextID();
196 0 my $return = $dbh->do('INSERT INTO personallinks (link_id, patron_id, link_name, link_url) VALUES (?, ?, ?, ?)', undef, $id, $self->patron_id(), $self->link_name(), $self->link_url());
197 0 ▶ if ($return > 1 || ! $return) { croak 'Patron link 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
198 0 $self->{link_id} = $id;
199  
200 }
201  
202 0 return 1;
203  
204 }
205  
206 0 ▶sub get_links {
Subroutine get_links: not called CC=4 SCAR=30.0
Pod documented
207  
208 0 my $class = shift;
209 0 my %opts = @_;
210 0 ▶ unless ($opts{patron_id} =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"patron_id"} =~ /^\d+$/) 0 0 0
211  
212 0 croak "Patron id not submitted. A valid numeric patron id must be submitted to the get_links() method.";
213  
214 }
215  
216 0 my $dbh = MyLibrary::DB->dbh();
217 0 my @patron_array = $dbh->selectrow_array('SELECT * FROM patrons WHERE patron_id = ?', undef, $opts{patron_id});
218 0 ▶ unless (scalar(@patron_array)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @patron_array) 0 0 0
219 0 croak "Valid patron id not submitted. No patron record exists corresponding to id number $opts{patron_id}.";
220 }
221  
222 0 my $link_ids = $dbh->selectcol_arrayref("SELECT link_id FROM personallinks WHERE patron_id = ? ORDER BY link_name", undef, $opts{patron_id});
223  
224 0 ▶ unless (scalar(@{$link_ids})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$link_ids;}) 0 0 0
225 0 return;
226 }
227  
228 0 return @{$link_ids};
229  
230 }
231  
232 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
233  
234 0 my $self = shift;
235
236 0 ▶ if ($self->link_id()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->link_id) 0 0 0
237  
238 0 my $dbh = MyLibrary::DB->dbh();
239 0 my $rv = $dbh->do('DELETE FROM personallinks WHERE link_id = ?', undef, $self->{link_id});
240 0 ▶ if ($rv != 1) {croak ("Deleted $rv records. Please check the personallinks table for errors.");}
Branch: 1 branch
BranchTrueFalseTotal
if ($rv != 1) 0 0 0
241 0 return 1;
242  
243 }
244  
245 0 return 0;
246  
247 }
248  
249 3 1;
« Auth.pm ↑ Summary Stylesheet.pm »