Store notifications events as Horde_Notification_Event objects
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 9 Feb 2010 01:37:46 +0000 (18:37 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 9 Feb 2010 03:52:57 +0000 (20:52 -0700)
14 files changed:
framework/Notification/lib/Horde/Notification/Event.php
framework/Notification/lib/Horde/Notification/Handler/Base.php
framework/Notification/lib/Horde/Notification/Listener.php
framework/Notification/lib/Horde/Notification/Listener/Audio.php
framework/Notification/lib/Horde/Notification/Listener/Javascript.php
framework/Notification/lib/Horde/Notification/Listener/Mobile.php
framework/Notification/lib/Horde/Notification/Listener/Status.php
framework/Notification/lib/Horde/Notification/Storage/Interface.php
framework/Notification/lib/Horde/Notification/Storage/Session.php
framework/Notification/test/Horde/Notification/Class/Notification/EventTest.php
framework/Notification/test/Horde/Notification/Class/Notification/Listener/AudioTest.php
framework/Notification/test/Horde/Notification/Class/Notification/Listener/JavascriptTest.php
framework/Notification/test/Horde/Notification/Class/Notification/Listener/MobileTest.php
framework/Notification/test/Horde/Notification/Class/Notification/Listener/StatusTest.php

index 03bc615..3d83053 100644 (file)
@@ -17,38 +17,78 @@ class Horde_Notification_Event
      *
      * @var string
      */
-    protected $_message = '';
+    public $message = '';
 
     /**
-     * If passed, sets the message for this event.
+     * The flags for this message.
      *
-     * @param string $message  The text message for this event.
+     * @var array
      */
-    public function __construct($message = null)
-    {
-        if (!is_null($message)) {
-            $this->setMessage($message);
-        }
-    }
+    public $flags = array();
 
     /**
-     * Sets the text message for this event.
+     * The message type.
      *
-     * @param string $message  The text message to display.
+     * @var string
      */
-    public function setMessage($message)
+    public $type;
+
+    /**
+     * Constructor.
+     *
+     * @param mixed $data   Message: either a string or an Exception or
+     *                      PEAR_Error object.
+     * @param string $type  The event type.
+     * @param array $flags  The flag array.
+     */
+    public function __construct($data, $type = null, array $flags = array())
     {
-        $this->_message = $message;
+        $this->flags = $flags;
+
+        if ($data instanceof PEAR_Error) {
+            // DEPRECATED
+            if (($userinfo = $ob->getUserInfo()) &&
+                  is_array($userinfo)) {
+                $userinfo_elts = array();
+                foreach ($userinfo as $userinfo_elt) {
+                    if (is_scalar($userinfo_elt)) {
+                        $userinfo_elts[] = $userinfo_elt;
+                    } elseif (is_object($userinfo_elt)) {
+                        if (is_callable(array($userinfo_elt, '__toString'))) {
+                            $userinfo_elts[] = $userinfo_elt->__toString();
+                        } elseif (is_callable(array($userinfo_elt, 'getMessage'))) {
+                            $userinfo_elts[] = $userinfo_elt->getMessage();
+                        }
+                    }
+                }
+
+                $this->message = $data->getMessage() . ' : ' . implode(', ', $userinfo_elts);
+            } else {
+                $this->message = $data->getMessage();
+            }
+
+            if (is_null($type)) {
+                $type = 'horde.error';
+            }
+        } else {
+            // String or Exception
+            $this->message = strval($data);
+            if (is_null($type)) {
+                $type = is_string($data) ? 'horde.message' : 'horde.error';
+            }
+        }
+
+        $this->type = $type;
     }
 
     /**
-     * Gets the text message for this event.
+     * String representation of this object.
      *
-     * @return string  The text message to display.
+     * @return string  String representation.
      */
-    public function getMessage()
+    public function __toString()
     {
-        return $this->_message;
+        return $this->message;
     }
 
 }
index bc512a4..dfb27a0 100644 (file)
@@ -144,33 +144,17 @@ implements Horde_Notification_Handler_Interface
      */
     public function push($event, $type = null, array $flags = array())
     {
-        if (!($event instanceof Horde_Notification_Event) &&
-            !($event instanceof PEAR_Error) &&
-            !($event instanceof Exception)) {
-            /* Transparently create a Horde_Notification_Event object and
-             * set the message attribute. */
-            $event = new Horde_Notification_Event($event);
-        }
-
-        if (is_null($type)) {
-            if ($event instanceof PEAR_Error || $event instanceof Exception) {
-                $type = 'horde.error';
-            } else {
-                $type = 'horde.message';
-            }
+        if ($event instanceof Horde_Notification_Event) {
+            $event->flags = $flags;
+            $event->type = $type;
+        } else {
+            /* Transparently create a Horde_Notification_Event object. */
+            $event = new Horde_Notification_Event($event, $type, $flags);
         }
 
         foreach ($this->_listeners as $listener) {
-            if ($listener->handles($type)) {
-                $this->_storage->push(
-                    $listener->getName(),
-                    array(
-                        'class' => get_class($event),
-                        'event' => serialize($event),
-                        'flags' => serialize($flags),
-                        'type' => $type
-                    )
-                );
+            if ($listener->handles($event->type)) {
+                $this->_storage->push($listener->getName(), $event);
             }
         }
     }
index 66a4c66..17fbfc4 100644 (file)
@@ -61,86 +61,12 @@ abstract class Horde_Notification_Listener
     /**
      * Processes one message from the message stack.
      *
-     * @param array $message  One message hash from the stack.
-     * @param array $options  An array of options.
+     * @param Horde_Notification_Event $event  One event object from the
+     *                                         stack.
+     * @param array $options                   An array of options.
      *
-     * @return mixed  TODO
+     * @return mixed  The formatted message.
      */
-    abstract public function getMessage($message, $options = array());
-
-    /**
-     * Unserialize an event from the message stack, checking to see if the
-     * appropriate class exists and kludging it into a base Notification_Event
-     * object if not.
-     */
-    public function getEvent($message)
-    {
-        $ob = @unserialize($message['event']);
-        if (!is_callable(array($ob, 'getMessage'))) {
-            if (isset($ob->_message)) {
-                $ob = new Horde_Notification_Event($ob->_message);
-            }
-        }
-
-        /* If we've failed to create a valid Notification_Event object
-         * (or subclass object) so far, return a PEAR_Error. */
-        if (!is_callable(array($ob, 'getMessage'))) {
-            $ob = PEAR::raiseError('Unable to decode message event: ' . $message['event']);
-        }
-
-        /* Specially handle PEAR_Error objects and add userinfo if
-         * it's there. */
-        if (is_callable(array($ob, 'getUserInfo'))) {
-            $userinfo = $ob->getUserInfo();
-            if ($userinfo) {
-                if (is_array($userinfo)) {
-                    $userinfo_elts = array();
-                    foreach ($userinfo as $userinfo_elt) {
-                        if (is_scalar($userinfo_elt)) {
-                            $userinfo_elts[] = $userinfo_elt;
-                        } elseif (is_object($userinfo_elt)) {
-                            if (is_callable(array($userinfo_elt, '__toString'))) {
-                                $userinfo_elts[] = $userinfo_elt->__toString();
-                            } elseif (is_callable(array($userinfo_elt, 'getMessage'))) {
-                                $userinfo_elts[] = $userinfo_elt->getMessage();
-                            }
-                        }
-                    }
-                    $userinfo = implode(', ', $userinfo_elts);
-                }
-
-                $ob->_message = $ob->getMessage() . ' : ' . $userinfo;
-            }
-        }
-
-        return $ob;
-    }
-
-    /**
-     * Unserialize an array of event flags from the message stack.  If this
-     * event has no flags, or the flags array could not be unserialized, an
-     * empty array is returned.
-     *
-     * @return array  An array of flags.
-     */
-    public function getFlags($message)
-    {
-        /* If this message doesn't have any flags, return an empty
-         * array. */
-        if (empty($message['flags'])) {
-            return array();
-        }
-
-        /* Unserialize the flags array from the message. */
-        $flags = @unserialize($message['flags']);
-
-        /* If we couldn't unserialize the flags array, return an empty
-         * array. */
-        if (!is_array($flags)) {
-            return array();
-        }
-
-        return $flags;
-    }
+    abstract public function getMessage($event, $options = array());
 
 }
index 364e100..3aebf91 100644 (file)
@@ -41,18 +41,16 @@ class Horde_Notification_Listener_Audio extends Horde_Notification_Listener
     }
 
     /**
-     * Outputs one message.
+     * Processes one message from the message stack.
      *
-     * @param array $message  One message hash from the stack.
-     * @param array $options  An array of options (not used).
+     * @param Horde_Notification_Event $event  An event object.
+     * @param array $options                   An array of options (not used).
      *
-     * @return text  The message representation.
+     * @return mixed  The formatted message.
      */
-    public function getMessage($message, $options = array())
+    public function getMessage($event, $options = array())
     {
-        $event = $this->getEvent($message);
-        return '<embed src="' . htmlspecialchars($event->getMessage()) .
-               '" width="0" height="0" autostart="true" />';
+        return '<embed src="' . htmlspecialchars($event->message) . '" width="0" height="0" autostart="true" />';
     }
 
 }
index 0f36256..edf4a23 100644 (file)
@@ -38,23 +38,32 @@ class Horde_Notification_Listener_Javascript extends Horde_Notification_Listener
             return;
         }
 
-        if (empty($options['noscript'])) {
-            echo '<script type="text/javascript">//<![CDATA[' . "\n";
-        }
-
-        $files = array();
+        $files = $js_text = array();
 
         while ($message = array_shift($messageStack)) {
-            $msg_text = $this->getMessage($message);
-            if ($message['type'] == 'javascript') {
-                echo $msg_text . "\n";
-            } elseif ($message['type'] == 'javascript-file') {
-                $files[] = $msg_text;
+            $event = $this->getMessage($message);
+            switch ($event->type) {
+            case 'javascript':
+                $js_text[] = $event->message . "\n";
+                break;
+
+            case 'javascript-file':
+                $files[] = $event->message;
+                break;
             }
         }
 
+        if (empty($options['noscript']) && !empty($js_text)) {
+            echo '<script type="text/javascript">//<![CDATA[' . "\n";
+        }
+
+        echo implode('', $js_text);
+
         if (empty($options['noscript'])) {
-            echo "//]]></script>\n";
+            if (!empty($js_text)) {
+                echo "//]]></script>\n";
+            }
+
             if (count($files)) {
                 foreach ($files as $file) {
                     echo '<script type="text/javascript" src="' . $file . '"></script>' . "\n";
@@ -64,17 +73,16 @@ class Horde_Notification_Listener_Javascript extends Horde_Notification_Listener
     }
 
     /**
-     * Outputs one message.
+     * Processes one message from the message stack.
      *
-     * @param array $message  One message hash from the stack.
-     * @param array $options  An array of options (not used).
+     * @param Horde_Notification_Event $event  An event object.
+     * @param array $options                   An array of options (not used).
      *
-     * @return string  The message text.
+     * @return mixed  The formatted message.
      */
-    public function getMessage($message, $options = array())
+    public function getMessage($event, $options = array())
     {
-        $event = $this->getEvent($message);
-        return $event->getMessage();
+        return $event->message;
     }
 
 }
index fbb2a25..610dc97 100644 (file)
@@ -66,20 +66,21 @@ class Horde_Notification_Listener_Mobile extends Horde_Notification_Listener_Sta
     }
 
     /**
-     * Outputs one message.
+     * Processes one message from the message stack.
      *
-     * @param array $message  One message hash from the stack.
-     * @param array $options  An array of options (not used).
+     * @param Horde_Notification_Event $event  An event object.
+     * @param array $options                   An array of options (not used).
+     *
+     * @return mixed  The formatted message.
      */
-    public function getMessage($message, $options = array())
+    public function getMessage($event, $options = array())
     {
         if (!$this->_mobile) {
             $p = new Horde_Notification_Listener_Status();
-            return $p->getMessage($message, $options);
+            return $p->getMessage($event, $options);
         }
 
-        $event = $this->getEvent($message);
-        $this->_mobile->add(new Horde_Mobile_text($this->_handles[$message['type']] . ': ' . strip_tags($event->getMessage())));
+        $this->_mobile->add(new Horde_Mobile_text($this->_handles[$event->type] . ': ' . strip_tags($event->message)));
     }
 
 }
index 2f9e0bf..1a974a6 100644 (file)
@@ -76,10 +76,10 @@ class Horde_Notification_Listener_Status extends Horde_Notification_Listener
     }
 
     /**
-     * Returns one message.
+     * Processes one message from the message stack.
      *
-     * @param array $message  One message hash from the stack.
-     * @param array $options  An array of options.
+     * @param Horde_Notification_Event $event  An event object.
+     * @param array $options                   An array of options:
      * <pre>
      * 'data' - (boolean) If false, returns HTML code. If true, returns an
      *                    array of message information. DEFAULT: false
@@ -87,18 +87,15 @@ class Horde_Notification_Listener_Status extends Horde_Notification_Listener
      *
      * @return mixed  TODO
      */
-    public function getMessage($message, $options = array())
+    public function getMessage($event, $options = array())
     {
-        $event = $this->getEvent($message);
-        $flags = $this->getFlags($message);
-        $result = array('type' => $message['type']);
+        $result = array('type' => $event->type);
 
-        if ($event instanceof Horde_Notification_Event &&
-            $message['type'] == 'horde.alarm') {
+        if ($event->type == 'horde.alarm') {
             if (empty($options['data'])) {
-                $text = $this->_getAlarm($flags['alarm']);
+                $text = $this->_getAlarm($event->flags['alarm']);
             } else {
-                $result['alarm'] = $flags['alarm'];
+                $result['alarm'] = $event->flags['alarm'];
                 if (!empty($result['alarm']['params']['notify']['ajax'])) {
                     $result['alarm']['ajax'] = $result['alarm']['params']['notify']['ajax'];
                 } elseif (!empty($result['alarm']['params']['notify']['show'])) {
@@ -108,17 +105,17 @@ class Horde_Notification_Listener_Status extends Horde_Notification_Listener
                       $result['alarm']['methods']);
             }
         } else {
-            $text = $event->getMessage();
+            $text = $event->message;
             if (!empty($options['data'])) {
                 $result['message'] = $text;
             }
-            if (!in_array('content.raw', $this->getFlags($message))) {
+            if (!in_array('content.raw', $event->flags)) {
                 $text = htmlspecialchars($text, ENT_COMPAT, Horde_Nls::getCharset());
             }
         }
 
         return empty($options['data'])
-            ? '<li>' . Horde::img($this->_handles[$message['type']][0], $this->_handles[$message['type']][1], '', '') . $text . '</li>'
+            ? '<li>' . Horde::img($this->_handles[$event->type][0], $this->_handles[$event->type][1], '', '') . $text . '</li>'
             : $result;
     }
 
index 30ef1b2..ffe7e43 100644 (file)
@@ -61,10 +61,10 @@ interface Horde_Notification_Storage_Interface
     /**
      * Store a new event.
      *
-     * @param string $listener The event will be stored for this listener.
-     * @param array  $event    The event to store.
-     *
-     * @return NULL
+     * @param string $listener                 The event will be stored for
+     *                                         this listener.
+     * @param Horde_Notification_Event $event  The event to store.
      */
-    public function push($listener, array $event);
-}
\ No newline at end of file
+    public function push($listener, $event);
+
+}
index fc10d24..4dcdadc 100644 (file)
@@ -96,13 +96,15 @@ implements Horde_Notification_Storage_Interface
     /**
      * Store a new event for the given listener stack.
      *
-     * @param string $listener The event will be stored for this listener.
-     * @param array  $event    The event to store.
-     *
-     * @return NULL
+     * @param string $listener                    The event will be stored for
+     *                                            this listener.
+     * @param Notification_Event_Listener $event  The event to store.
      */
-    public function push($listener, array $event)
+    public function push($listener, $event)
     {
+        /* No need to serialize() ourselves - PHP's session handling does
+         * this automatically. */
         $_SESSION[$this->_stack][$listener][] = $event;
     }
-}
\ No newline at end of file
+
+}
index 43c8741..9d394cd 100644 (file)
@@ -33,26 +33,19 @@ class Horde_Notification_Class_Notification_EventTest extends PHPUnit_Framework_
     public function testMethodConstructHasPostconditionThatTheGivenMessageWasSavedIfItWasNotNull()
     {
         $event = new Horde_Notification_Event('test');
-        $this->assertEquals('test', $event->getMessage());
-    }
-
-    public function testMethodSetmessageHasPostconditionThatTheGivenMessageWasSaved()
-    {
-        $event = new Horde_Notification_Event();
-        $event->setMessage('test');
-        $this->assertEquals('test', $event->getMessage());
+        $this->assertEquals('test', $event->message);
     }
 
     public function testMethodGetmessageHasResultStringTheStoredMessage()
     {
         $event = new Horde_Notification_Event();
-        $event->setMessage('test');
-        $this->assertEquals('test', $event->getMessage());
+        $event->message = test;
+        $this->assertEquals('test', $event->message);
     }
 
     public function testMethodGetmessageHasResultStringEmptyIfNoMessageWasStored()
     {
         $event = new Horde_Notification_Event();
-        $this->assertEquals('', $event->getMessage());
+        $this->assertEquals('', $event->message);
     }
-}
\ No newline at end of file
+}
index b629ad4..d5efc65 100644 (file)
@@ -46,12 +46,7 @@ class Horde_Notification_Class_Notification_Listener_AudioTest extends PHPUnit_E
     {
         $listener = new Horde_Notification_Listener_Audio();
         $event = new Horde_Notification_Event('test');
-        $messages = array(
-            array(
-                'class' => 'Horde_Notification_Event',
-                'event' => serialize($event)
-            )
-        );
+        $messages = array($event);
         $this->expectOutputString(
             '<embed src="test" width="0" height="0" autostart="true" />'
         );
index 0dbaa46..2761bba 100644 (file)
@@ -53,13 +53,7 @@ class Horde_Notification_Class_Notification_Listener_JavascriptTest extends PHPU
     {
         $listener = new Horde_Notification_Listener_Javascript();
         $event = new Horde_Notification_Event('test');
-        $messages = array(
-            array(
-                'class' => 'Horde_Notification_Event',
-                'event' => serialize($event),
-                'type'  => 'javascript'
-            )
-        );
+        $messages = array($event);
         $this->expectOutputString(
             '<script type="text/javascript">//<![CDATA['
             . "\n" . 'test' . "\n" . '//]]></script>' . "\n"
@@ -71,13 +65,7 @@ class Horde_Notification_Class_Notification_Listener_JavascriptTest extends PHPU
     {
         $listener = new Horde_Notification_Listener_Javascript();
         $event = new Horde_Notification_Event('test');
-        $messages = array(
-            array(
-                'class' => 'Horde_Notification_Event',
-                'event' => serialize($event),
-                'type'  => 'javascript'
-            )
-        );
+        $messages = array($event);
         $this->expectOutputString('test' . "\n");
         $listener->notify($messages, array('noscript' => true));
     }
@@ -86,18 +74,12 @@ class Horde_Notification_Class_Notification_Listener_JavascriptTest extends PHPU
     {
         $listener = new Horde_Notification_Listener_Javascript();
         $event = new Horde_Notification_Event('test');
-        $messages = array(
-            array(
-                'class' => 'Horde_Notification_Event',
-                'event' => serialize($event),
-                'type'  => 'javascript-file'
-            )
-        );
+        $event->type = 'javascript-file';
+        $messages = array($event);
         $this->expectOutputString(
-            '<script type="text/javascript">//<![CDATA['
-            . "\n" . '//]]></script>' . "\n" .
             '<script type="text/javascript" src="test"></script>' . "\n"
         );
         $listener->notify($messages);
     }
+
 }
index 951b8cd..ce3a152 100644 (file)
@@ -78,13 +78,7 @@ class Horde_Notification_Class_Notification_Listener_MobileTest extends PHPUnit_
         $this->markTestIncomplete('This is untestable without mocking half of the Horde framework.');
         $listener = new Horde_Notification_Listener_Mobile();
         $event = new Horde_Notification_Event('test');
-        $messages = array(
-            array(
-                'class' => 'Horde_Notification_Event',
-                'event' => serialize($event),
-                'type'  => 'horde.message'
-            )
-        );
+        $messages = array($event);
         $this->expectOutputString(
             '<ul class="notices"><li>test</li></ul>'
         );
@@ -112,13 +106,7 @@ class Horde_Notification_Class_Notification_Listener_MobileTest extends PHPUnit_
         $listener = new Horde_Notification_Listener_Mobile();
         $listener->setMobileObject($this->mobile);
         $event = new Horde_Notification_Event('test');
-        $messages = array(
-            array(
-                'class' => 'Horde_Notification_Event',
-                'event' => serialize($event),
-                'type'  => 'horde.message',
-            )
-        );
+        $messages = array($event);
         $listener->notify($messages);
     }
 
@@ -126,14 +114,9 @@ class Horde_Notification_Class_Notification_Listener_MobileTest extends PHPUnit_
     {
         $listener = new Horde_Notification_Listener_Mobile();
         $event = new Horde_Notification_Event('test');
-        $flags = array('content.raw' => true);
-        $message = array(
-            'class' => 'Horde_Notification_Event',
-            'event' => serialize($event),
-            'type'  => 'horde.message',
-            'flags' => serialize($flags)
-        );
-        $listener->getMessage($message, array('data' => true));
+        $event->flags = array('content.raw' => true);
+        $messages = array($event);
+        $listener->getMessage($messages, array('data' => true));
     }
 
 }
index f609562..53a3b38 100644 (file)
@@ -69,13 +69,7 @@ class Horde_Notification_Class_Notification_Listener_StatusTest extends PHPUnit_
         $this->markTestIncomplete('This is untestable without mocking half of the Horde framework.');
         $listener = new Horde_Notification_Listener_Status();
         $event = new Horde_Notification_Event('test');
-        $messages = array(
-            array(
-                'class' => 'Horde_Notification_Event',
-                'event' => serialize($event),
-                'type'  => 'horde.message'
-            )
-        );
+        $messages = array($event);
         $this->expectOutputString(
             '<ul class="notices"><li>test</li></ul>'
         );
@@ -86,15 +80,8 @@ class Horde_Notification_Class_Notification_Listener_StatusTest extends PHPUnit_
     {
         $listener = new Horde_Notification_Listener_Status();
         $event = new Horde_Notification_Event('test');
-        $flags = array('content.raw' => true);
-        $messages = array(
-            array(
-                'class' => 'Horde_Notification_Event',
-                'event' => serialize($event),
-                'type'  => 'horde.message',
-                'flags' => serialize($flags)
-            )
-        );
+        $event->flags = array('content.raw' => true);
+        $messages = array($event);
         $listener->notify($messages, array('store' => true));
         $this->expectOutputString('');
     }
@@ -103,23 +90,11 @@ class Horde_Notification_Class_Notification_Listener_StatusTest extends PHPUnit_
     {
         $listener = new Horde_Notification_Listener_Status();
         $event = new Horde_Notification_Event('test');
-        $flags = array('content.raw' => true);
-        $messages = array(
-            array(
-                'class' => 'Horde_Notification_Event',
-                'event' => serialize($event),
-                'type'  => 'horde.message',
-                'flags' => serialize($flags)
-            )
-        );
+        $event->flags = array('content.raw' => true);
+        $messages = array($event);
         $listener->notify($messages, array('store' => true));
         $this->assertEquals(
-            array(
-                array(
-                    'message' => 'test',
-                    'type' => 'horde.message'
-                )
-            ),
+            $event,
             $listener->getStack()
         );
     }