diff --git a/lib/AlmaClient/AlmaClient.class.php b/lib/AlmaClient/AlmaClient.class.php index 6fff6e6..f552f47 100644 --- a/lib/AlmaClient/AlmaClient.class.php +++ b/lib/AlmaClient/AlmaClient.class.php @@ -26,7 +26,7 @@ class AlmaClient { /** * Constructor, checking if we have a sensible value for $base_url. */ - function __construct($base_url) { + public function __construct($base_url) { if (stripos($base_url, 'http') === 0 && filter_var($base_url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)) { $this->base_url = $base_url; } @@ -46,28 +46,30 @@ function __construct($base_url) { * are required for all request related to library patrons. * @param array $params * Query string parameters in the form of key => value. - * @param boolean $check_status + * @param bool $check_status * Check the status element, and throw an exception if it is not ok. + * * @return DOMDocument * A DOMDocument object with the response. */ public function request($method, $params = array(), $check_status = TRUE) { - $startTime = explode(' ', microtime()); + $start_time = explode(' ', microtime()); // For use with a non-Drupal-system, we should have a way to swap // the HTTP client out. $request = drupal_http_request(url($this->base_url . $method, array('query' => $params)), array('secure_socket_transport' => 'sslv3')); - $stopTime = explode(' ', microtime()); + $stop_time = explode(' ', microtime()); // For use with a non-Drupal-system, we should have a way to swap // logging and logging preferences out. if (variable_get('alma_enable_logging', FALSE)) { - $seconds = floatval(($stopTime[1] + $stopTime[0]) - ($startTime[1] + $startTime[0])); + $seconds = floatval(($stop_time[1] + $stop_time[0]) - ($start_time[1] + $start_time[0])); // Filter params to avoid logging sensitive data. - // This can be disabled by setting alma_logging_filter_params = 0. There is no UI for setting this variable + // This can be disabled by setting alma_logging_filter_params = 0. There + // is no UI for setting this variable // It is intended for settings.php in development environments only. $params = (variable_get('alma_logging_filter_params', 1)) ? self::filter_request_params($params) : $params; - // Log the request + // Log the request. watchdog('alma', 'Sent request: @url (@seconds s)', array('@url' => url($this->base_url . $method, array('query' => $params)), '@seconds' => $seconds), WATCHDOG_DEBUG); } @@ -86,10 +88,10 @@ public function request($method, $params = array(), $check_status = TRUE) { case '': case 'borrCardNotFound': throw new AlmaClientBorrCardNotFound('Invalid borrower credentials'); - break; + case 'reservationNotFound': throw new AlmaClientReservationNotFound('Reservation not found'); - break; + default: throw new AlmaClientCommunicationError('Status is not okay: ' . $message); } @@ -101,23 +103,31 @@ public function request($method, $params = array(), $check_status = TRUE) { } /** - * Filters sensitive information in request parameters allowing the values to be logged + * Filters sensitive information in request parameters allowing the values to + * be logged. * - * @param array $params An array of request information + * @param array $params + * An array of request information * * @return array - * An array of filtered request information + * An array of filtered request information */ private static function filter_request_params($params) { // Scramble sensitive information - $sensitive = array('borrCard', 'pinCode', 'pinCodeChange', 'address', 'emailAddress'); + $sensitive = array( + 'borrCard', + 'pinCode', + 'pinCodeChange', + 'address', + 'emailAddress', + ); $log_params = array(); foreach ($params as $key => $value) { if (in_array($key, $sensitive)) { // Replace the value with a scrambled version generated using md5() and // the static salt. This way all requests generated by the same page - // load can be grouped + // load can be grouped. $value = substr(md5($value . self::$salt), 0, strlen($value)); } $log_params[$key] = $value; @@ -132,7 +142,7 @@ private static function filter_request_params($params) { * Formats the list of branches in an array usable for form API selects. * * @return array - * List of branches, keyed by branch_id + * List of branches, keyed by branch_id. */ public function get_branches() { $branches = array(); @@ -151,7 +161,7 @@ public function get_branches() { * Formats the list of branches in an array usable for form API selects. * * @return array - * List of branches, keyed by branch_id + * List of branches, keyed by branch_id */ public function get_reservation_branches() { $branches = array(); @@ -169,7 +179,7 @@ public function get_reservation_branches() { * Formats the list of branches in an array usable for form API selects. * * @return array - * List of departments, keyed by department id. + * List of departments, keyed by department id. */ public function get_departments() { $departments = array(); @@ -188,7 +198,7 @@ public function get_departments() { * Formats the list of branches in an array usable for form API selects. * * @return array - * List of locations, keyed by location id. + * List of locations, keyed by location id. */ public function get_locations() { $locations = array(); @@ -202,12 +212,12 @@ public function get_locations() { } /** - * Get sublocation names from Alma. + * Get sub location names from Alma. * * Formats the list of branches in an array usable for form API selects. * * @return array - * List of sublocations, keyed by sublocation id. + * List of sub locations, keyed by sub location id. */ public function get_sublocations() { $sublocations = array(); @@ -226,7 +236,7 @@ public function get_sublocations() { * Formats the list of branches in an array usable for form API selects. * * @return array - * List of collections, keyed by collection id. + * List of collections, keyed by collection id. */ public function get_collections() { $collections = array(); @@ -520,8 +530,8 @@ public function renew_loan($borr_card, $pin_code, $loan_ids) { $doc = $this->request('patron/loans/renew', $params); - //Built return array as specified by Ding loan provider. - //See ding_provider_example_loan_renew_loans(). + // Built return array as specified by Ding loan provider. + // See ding_provider_example_loan_renew_loans(). $reservations = array(); foreach ($doc->getElementsByTagName('loan') as $loan) { $id = $loan->getAttribute('id'); @@ -529,9 +539,10 @@ public function renew_loan($borr_card, $pin_code, $loan_ids) { if ($renewable = $loan->getElementsByTagName('loanIsRenewable')->item(0)) { $message = $renewable->getAttribute('message'); $renewable = $renewable->getAttribute('value'); - //If message is "isRenewedToday" we assumme that the renewal is successful. - //Even if this is not the case any error in the current renewal is irrelevant - //as the loan has previously been renewed so don't report it as such + // If message is "isRenewedToday" we assume that the renewal is + // successful. Even if this is not the case any error in the current + // renewal is irrelevant as the loan has previously been renewed so + // don't report it as such. if ($message == 'isRenewedToday' || $renewable == 'yes') { $reservations[$id] = TRUE; } @@ -823,12 +834,12 @@ public function add_payment($debt_ids, $order_id = NULL) { * Change user’s preferred branch. * * @param string $borr_card - * Library patron's borrowing card number. Either just an arbitrary - * number printed on their library card or their CPR-code. + * Library patron's borrowing card number. Either just an arbitrary + * number printed on their library card or their CPR-code. * @param string $pin_code - * Library patron's current four digit PIN code. + * Library patron's current four digit PIN code. * @param string $branch_code - * New preferred branch. + * New preferred branch. */ public function change_patron_preferences($borr_card, $pin_code, $branch_code) { $params = array( @@ -845,14 +856,14 @@ public function change_patron_preferences($borr_card, $pin_code, $branch_code) { * Add an Alma absent period. * * @param string $borr_card - * Library patron's borrowing card number. Either just an arbitrary - * number printed on their library card or their CPR-code. + * Library patron's borrowing card number. Either just an arbitrary + * number printed on their library card or their CPR-code. * @param string $pin_code - * Library patron's current four digit PIN code. + * Library patron's current four digit PIN code. * @param string $from_date - * Absent period start date. + * Absent period start date. * @param string $to_date - * Absent period start date. + * Absent period start date. */ public function add_absent_period($borr_card, $pin_code, $from_date, $to_date) { $params = array( @@ -870,16 +881,16 @@ public function add_absent_period($borr_card, $pin_code, $from_date, $to_date) { * Change existing absent period. * * @param string $borr_card - * Library patron's borrowing card number. Either just an arbitrary - * number printed on their library card or their CPR-code. + * Library patron's borrowing card number. Either just an arbitrary + * number printed on their library card or their CPR-code. * @param string $pin_code - * Library patron's current four digit PIN code. + * Library patron's current four digit PIN code. * @param string $absent_id - * ID for existing period. + * ID for existing period. * @param string $from_date - * Absent period start date. + * Absent period start date. * @param string $to_date - * Absent period start date. + * Absent period start date. */ public function change_absent_period($borr_card, $pin_code, $absent_id, $from_date, $to_date) { $params = array( @@ -898,12 +909,12 @@ public function change_absent_period($borr_card, $pin_code, $absent_id, $from_da * Remove existing absent period. * * @param string $borr_card - * Library patron's borrowing card number. Either just an arbitrary - * number printed on their library card or their CPR-code. + * Library patron's borrowing card number. Either just an arbitrary + * number printed on their library card or their CPR-code. * @param string $pin_code - * Library patron's current four digit PIN code. + * Library patron's current four digit PIN code. * @param string $absent_id - * ID for existing period. + * ID for existing period. */ public function remove_absent_period($borr_card, $pin_code, $absent_id) { $params = array( @@ -920,14 +931,14 @@ public function remove_absent_period($borr_card, $pin_code, $absent_id) { * Add a messaging service. * * @param string $borr_card - * Library patron's borrowing card number. Either just an arbitrary - * number printed on their library card or their CPR-code. + * Library patron's borrowing card number. Either just an arbitrary + * number printed on their library card or their CPR-code. * @param string $pin_code - * Library patron's current four digit PIN code. + * Library patron's current four digit PIN code. * @param $method - * The method for sending messages e.g. SMS + * The method for sending messages e.g. SMS * @param $type - * The message type e.g. due date alerts + * The message type e.g. due date alerts */ public function add_message_service($borr_card, $pin_code, $method, $type) { $params = array( @@ -945,14 +956,14 @@ public function add_message_service($borr_card, $pin_code, $method, $type) { * Removes a messaging service. * * @param string $borr_card - * Library patron's borrowing card number. Either just an arbitrary - * number printed on their library card or their CPR-code. + * Library patron's borrowing card number. Either just an arbitrary + * number printed on their library card or their CPR-code. * @param string $pin_code - * Library patron's current four digit PIN code. + * Library patron's current four digit PIN code. * @param $method - * The method for sending messages e.g. SMS + * The method for sending messages e.g. SMS * @param $type - * The message type e.g. due date alerts + * The message type e.g. due date alerts */ public function remove_message_service($borr_card, $pin_code, $method, $type) { $params = array(