Bug 21395: Fix misc/admin/koha-preferences
[koha.git] / Koha / ExternalContent / RecordedBooks.pm
blob2e8c33bffdb6fcaf21cddd6c2794d3c600657497
1 # Copyright 2016 Catalyst
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 package Koha::ExternalContent::RecordedBooks;
20 use Modern::Perl;
21 use Carp;
23 use base qw(Koha::ExternalContent);
24 use WebService::ILS::RecordedBooks::PartnerPatron;
25 use WebService::ILS::RecordedBooks::Partner;
26 use C4::Context;
28 __PACKAGE__->mk_accessors(qw(domain is_identified));
30 =head1 NAME
32 Koha::ExternalContent::RecordedBooks
34 =head1 SYNOPSIS
36 use Koha::ExternalContent::RecordedBooks;
37 my $rb_client = Koha::ExternalContent::RecordedBooks->new();
38 my $rb_auth_url = $od_client->auth_url();
40 =head1 DESCRIPTION
42 A (very) thin wrapper around C<WebService::ILS::RecordedBooks::Patron>
44 Takes "RecordedBooks*" Koha preferences
46 =cut
48 =head2 Class Methods
50 =cut
52 =head3 new
54 my $rb_client = Koha::ExternalContent::RecordedBooks->new();
56 Create the object for interacting with RecordedBooks
58 =cut
60 sub new {
61 my $class = shift;
62 my $params = shift || {};
64 my $self = $class->SUPER::new($params);
65 unless ($params->{client}) {
66 my $client_secret = C4::Context->preference('RecordedBooksClientSecret')
67 or croak("RecordedBooksClientSecret pref not set");
68 my $library_id = C4::Context->preference('RecordedBooksLibraryID')
69 or croak("RecordedBooksLibraryID pref not set");
70 my $domain = C4::Context->preference('RecordedBooksDomain');
71 my $patron = $params->{koha_session_id} ? $self->koha_patron : undef;
72 my $email;
73 if ($patron) {
74 $email = $patron->email
75 or $self->logger->warn("User with no email, cannot identify with RecordedBooks");
77 my $client;
78 if ($email) {
79 local $@;
80 $client = eval { WebService::ILS::RecordedBooks::PartnerPatron->new(
81 client_secret => $client_secret,
82 library_id => $library_id,
83 domain => $domain,
84 user_id => $email,
85 ) };
86 $self->logger->warn("Invalid RecordedBooks user $email ($@)") if $@;
87 $self->is_identified($client);
89 $client ||= WebService::ILS::RecordedBooks::Partner->new(
90 client_secret => $client_secret,
91 library_id => $library_id,
92 domain => $domain,
94 $self->client( $client );
96 return $self;
99 =head1 METHODS
101 L<WebService::ILS::RecordedBooks::PartnerPatron> methods used without mods:
103 =over 4
105 =item C<error_message()>
107 =back
109 =cut
111 use vars qw{$AUTOLOAD};
112 sub AUTOLOAD {
113 my $self = shift;
114 (my $method = $AUTOLOAD) =~ s/.*:://;
115 return $self->client->$method(@_);
117 sub DESTROY { }