From d58c4c22fc783f348922cd8fa860bef2e1c1aa0e Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 21 Feb 2013 11:21:06 -0500
Subject: [PATCH] Inject configuration into File stats driver; throw exception
 if unable to open file.

---
 module/VuFind/config/module.config.php        |  9 ++++++-
 .../src/VuFind/Statistics/Driver/File.php     | 26 ++++++++++++++-----
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 2c081d414a5..0bf3ca6b74e 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -532,9 +532,16 @@ $config = array(
             ),
             'statistics_driver' => array(
                 'abstract_factories' => array('VuFind\Statistics\Driver\PluginFactory'),
+                'factories' => array(
+                    'file' => function ($sm) {
+                        $config = \VuFind\Config\Reader::getConfig();
+                        $folder = isset($config->Statistics->file)
+                            ? $config->Statistics->file : sys_get_temp_dir();
+                        return new \VuFind\Statistics\Driver\File($folder);
+                    },
+                ),
                 'invokables' => array(
                     'db' => 'VuFind\Statistics\Driver\Db',
-                    'file' => 'VuFind\Statistics\Driver\File',
                     'solr' => 'VuFind\Statistics\Driver\Solr',
                 ),
                 'aliases' => array(
diff --git a/module/VuFind/src/VuFind/Statistics/Driver/File.php b/module/VuFind/src/VuFind/Statistics/Driver/File.php
index 28838810bf4..34e90c3c253 100644
--- a/module/VuFind/src/VuFind/Statistics/Driver/File.php
+++ b/module/VuFind/src/VuFind/Statistics/Driver/File.php
@@ -26,7 +26,6 @@
  * @link     http://vufind.org   Main Site
  */
 namespace VuFind\Statistics\Driver;
-use VuFind\Config\Reader as ConfigReader;
 
 /**
  * Writer to put statistics into an XML File
@@ -39,7 +38,22 @@ use VuFind\Config\Reader as ConfigReader;
  */
 class File extends AbstractBase
 {
-    protected $folder = null;
+    /**
+     * Folder where stats will be written.
+     *
+     * @var string
+     */
+    protected $folder;
+
+    /**
+     * Constructor
+     *
+     * @param string $folder Folder where stats will be written.
+     */
+    public function __construct($folder)
+    {
+        $this->folder = $folder;
+    }
 
     /**
      * Get the name of the folder for storing statistics.
@@ -48,10 +62,6 @@ class File extends AbstractBase
      */
     protected function getFolder()
     {
-        if (null === $this->folder) {
-            $configs = ConfigReader::getConfig();
-            $this->folder = $configs->Statistics->file;
-        }
         return $this->folder;
     }
 
@@ -61,6 +71,7 @@ class File extends AbstractBase
      * @param array $data     Data specific to what we're saving
      * @param array $userData Browser, IP, urls, etc
      *
+     * @throws \Exception
      * @return void
      */
     public function write($data, $userData)
@@ -81,6 +92,9 @@ class File extends AbstractBase
             $xml .= "\n</xml>";
         }
         $file = fopen($filename, 'w');
+        if (!$file) {
+            throw new \Exception('Cannot write to log file.');
+        }
         fwrite($file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . $xml);
         fclose($file);
     }
-- 
GitLab