From 1d4832818ca6220e76304bee80f3e09e11e8b602 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 12 Dec 2016 14:17:44 -0300 Subject: [PATCH] Bug 17755: Introduce Koha::Object::Library::Limit This patch introduces a new class for extending Koha::Object using multiple inheritance. It cannot be used standalone, it needs to be used in Koha::Object implementations like this: use base qw( Koha::Object Koha::Object::Library::Limit ); Its goal is to provide a single way and place to deal with this common pattern in Koha's codebase. As it happened with Koha::Object, that needed to be tested in a real object class, this work was done on top of Koha::Patron::Attribute::Type implementation and it is fully covered by the tests that are introduced for it. --- Koha/Exceptions.pm | 9 ++- Koha/Object/Library/Limit.pm | 171 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 Koha/Object/Library/Limit.pm diff --git a/Koha/Exceptions.pm b/Koha/Exceptions.pm index dd8647b..12540c9 100644 --- a/Koha/Exceptions.pm +++ b/Koha/Exceptions.pm @@ -8,11 +8,14 @@ use Exception::Class ( 'Koha::Exceptions::Exception' => { description => 'Something went wrong!', }, - 'Koha::Exceptions::DuplicateObject' => { isa => 'Koha::Exceptions::Exception', description => 'Same object already exists', }, + 'Koha::Exceptions::ObjectNotFound' => { + isa => 'Koha::Exceptions::Exception', + description => 'The required object doesn\'t exist', + }, 'Koha::Exceptions::CannotDeleteDefault' => { isa => 'Koha::Exceptions::Exception', description => 'The default value cannot be deleted' @@ -21,6 +24,10 @@ use Exception::Class ( isa => 'Koha::Exceptions::Exception', description => 'A required parameter is missing' }, + 'Koha::Exceptions::CannotAddLibraryLimit' => { + isa => 'Koha::Exceptions::Exception', + description => 'General problem adding a library limit' + }, # Virtualshelves exceptions 'Koha::Exceptions::Virtualshelves::DuplicateObject' => { isa => 'Koha::Exceptions::DuplicateObject', diff --git a/Koha/Object/Library/Limit.pm b/Koha/Object/Library/Limit.pm new file mode 100644 index 0000000..7c5cc37 --- /dev/null +++ b/Koha/Object/Library/Limit.pm @@ -0,0 +1,171 @@ +package Koha::Object::Library::Limit; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Koha::Database; +use Koha::Exceptions; + +use Try::Tiny; + +=head1 NAME + +Koha::Object::Library::Limit - Generic library limit handling class + +=head1 SYNOPSIS + + use base qw(Koha::Object Koha::Object::Library::Limit); + my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } ); + +=head1 DESCRIPTION + +This class is provided as a generic way of handling library limits for Koha::Object-based classes +in Koha. + +This class must always be subclassed. + +=head1 API + +=head2 Class Methods + +=cut + +=head3 library_limits + +my $limits = $object->library_limits(); + +$object->library_limits( \@branchcodes ); + +=cut + +sub library_limits { + my ( $self, $branchcodes ) = @_; + + if ($branchcodes) { + return $self->replace_library_limits($branchcodes); + } + else { + return $self->get_library_limits(); + } +} + +=head3 get_library_limits + +my $limits = $object->get_library_limits(); + +=cut + +sub get_library_limits { + my ($self) = @_; + + my @branchcodes + = $self->_library_limit_rs->search( + { $self->_library_limits->{id} => $self->id } ) + ->get_column( $self->_library_limits->{library} )->all(); + + return \@branchcodes; +} + +=head3 add_library_limit + +$object->add_library_limit( $branchcode ); + +=cut + +sub add_library_limit { + my ( $self, $branchcode ) = @_; + + Koha::Exceptions::MissingParameter->throw( + "Required parameter 'branchcode' missing") + unless $branchcode; + + my $limitation; + try { + $limitation = $self->_library_limit_rs->update_or_create( + { $self->_library_limits->{id} => $self->id, + $self->_library_limits->{library} => $branchcode + } + ); + } + catch { + Koha::Exceptions::CannotAddLibraryLimit->throw( $_->{msg} ); + }; + + return $limitation ? 1 : undef; +} + +=head3 del_library_limit + +$object->del_library_limit( $branchcode ); + +=cut + +sub del_library_limit { + my ( $self, $branchcode ) = @_; + + Koha::Exceptions::MissingParameter->throw( + "Required parameter 'branchcode' missing") + unless $branchcode; + + my $limitation = $self->_library_limit_rs->search( + { $self->_library_limits->{id} => $self->id, + $self->_library_limits->{library} => $branchcode + } + ); + + Koha::Exceptions::ObjectNotFound->throw( + "No branch limit for branch $branchcode found for id " + . $self->id + . " to delete!" ) + unless ($limitation->count); + + return $limitation->delete(); +} + +=head3 replace_library_limits + +$object->replace_library_limits( \@branchcodes ); + +=cut + +sub replace_library_limits { + my ( $self, $branchcodes ) = @_; + + $self->_library_limit_rs->search( + { $self->_library_limits->{id} => $self->id } )->delete; + + my @return_values = map { $self->add_library_limit($_) } @$branchcodes; + + return \@return_values; +} + +=head3 Koha::Objects->_library_limit_rs + +Returns the internal resultset for the branch limitation table or creates it if undefined + +=cut + +sub _library_limit_rs { + my ($self) = @_; + + $self->{_library_limit_rs} ||= Koha::Database->new->schema->resultset( + $self->_library_limits->{class} ); + + return $self->{_library_limit_rs}; +} + +1; -- 2.7.4