From 6ff58f6f280f8c8474968e66c96cefd25bff6a75 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Fri, 8 Jan 2021 10:29:37 +0200
Subject: [PATCH] KohaRest: Avoid hard dependency on SafeMoneyFormat.

This allows the driver to work in console utilities as well.
---
 .../VuFind/src/VuFind/ILS/Driver/KohaRest.php | 27 ++++++++++++++-----
 .../src/VuFind/ILS/Driver/KohaRestFactory.php |  9 +++++--
 2 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/module/VuFind/src/VuFind/ILS/Driver/KohaRest.php b/module/VuFind/src/VuFind/ILS/Driver/KohaRest.php
index 484ba5acccf..71601c33c52 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/KohaRest.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/KohaRest.php
@@ -77,7 +77,7 @@ class KohaRest extends \VuFind\ILS\Driver\AbstractBase implements
     /**
      * Factory function for constructing the SessionContainer.
      *
-     * @var Callable
+     * @var callable
      */
     protected $sessionFactory;
 
@@ -220,10 +220,10 @@ class KohaRest extends \VuFind\ILS\Driver\AbstractBase implements
      * @param \VuFind\Date\Converter $dateConverter   Date converter object
      * @param callable               $sessionFactory  Factory function returning
      * SessionContainer object
-     * @param SafeMoneyFormat        $safeMoneyFormat Money formatting view helper
+     * @param ?SafeMoneyFormat       $safeMoneyFormat Money formatting view helper
      */
     public function __construct(\VuFind\Date\Converter $dateConverter,
-        $sessionFactory, SafeMoneyFormat $safeMoneyFormat
+        $sessionFactory, ?SafeMoneyFormat $safeMoneyFormat
     ) {
         $this->dateConverter = $dateConverter;
         $this->sessionFactory = $sessionFactory;
@@ -2422,11 +2422,9 @@ class KohaRest extends \VuFind\ILS\Driver\AbstractBase implements
         case 'Patron::Debt':
         case 'Patron::DebtGuarantees':
             $count = isset($details['current_outstanding'])
-                ? $this->safeMoneyFormat->__invoke($details['current_outstanding'])
-                : '-';
+                ? $this->formatMoney($details['current_outstanding']) : '-';
             $limit = isset($details['max_outstanding'])
-                ? $this->safeMoneyFormat->__invoke($details['max_outstanding'])
-                : '-';
+                ? $this->formatMoney($details['max_outstanding']) : '-';
             $params = [
                 '%%blockCount%%' => $count,
                 '%%blockLimit%%' => $limit,
@@ -2435,4 +2433,19 @@ class KohaRest extends \VuFind\ILS\Driver\AbstractBase implements
         }
         return $this->translate($this->patronStatusMappings[$reason] ?? '', $params);
     }
+
+    /**
+     * Helper function for formatting currency
+     *
+     * @param $amount Number to format
+     *
+     * @return string
+     */
+    protected function formatMoney($amount)
+    {
+        if (null === $this->safeMoneyFormat) {
+            throw new \Exception('SafeMoneyFormat helper not available');
+        }
+        return ($this->safeMoneyFormat)($amount);
+    }
 }
diff --git a/module/VuFind/src/VuFind/ILS/Driver/KohaRestFactory.php b/module/VuFind/src/VuFind/ILS/Driver/KohaRestFactory.php
index 5875cad6551..e1ed6bc4f44 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/KohaRestFactory.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/KohaRestFactory.php
@@ -67,9 +67,14 @@ class KohaRestFactory extends \VuFind\ILS\Driver\DriverWithDateConverterFactory
             $manager = $container->get(\Laminas\Session\SessionManager::class);
             return new \Laminas\Session\Container("KohaRest_$namespace", $manager);
         };
-        $helper = $container->get('ViewHelperManager')->get('safeMoneyFormat');
+        // Create safeMoneyFormat helper conditionally to avoid hard dependency on
+        // themes (which otherwise could cause problems for command line tools that
+        // use the ILS driver when the theme system is not active).
+        $helperManager = $container->get('ViewHelperManager');
+        $safeMoneyFormat = $helperManager->has('safeMoneyFormat')
+            ? $helperManager->get('safeMoneyFormat') : null;
         return parent::__invoke(
-            $container, $requestedName, [$sessionFactory, $helper]
+            $container, $requestedName, [$sessionFactory, $safeMoneyFormat]
         );
     }
 }
-- 
GitLab