echo logger
[auerswald-callnotifier.git] / src / callnotifier / MessageHandler.php
index b4ed6455706a882129527b05d4d1b8fd94dcae50..89d07e72c73464a51ee0346b4ed4cd338a2b900d 100644 (file)
@@ -5,6 +5,18 @@ class MessageHandler
 {
     protected $dumpHdl;
 
+    /**
+     * Array of logger object arrays.
+     * Key is the notification type, value is an array of logger objects
+     * that want to get notified about the type.
+     *
+     * @var array
+     */
+    protected $logger = array(
+        'msgData' => array(),
+        'incomingCall' => array()
+    );
+
 
     public function __construct($config)
     {
@@ -12,6 +24,30 @@ class MessageHandler
         $this->prepareDump();
     }
 
+    /**
+     * Add a logger
+     *
+     * @param Logger       $logger Logger object to register
+     * @param array|string $types  Single notification type or array of such
+     *                             types. "*" means "register for all types".
+     *
+     * @return self
+     */
+    public function addLogger(Logger $logger, $types)
+    {
+        if ($types == '*') {
+            $types = array_keys($this->logger);
+        }
+        $types = (array)$types;
+
+        foreach ($types as $type) {
+            if (!isset($this->logger[$type])) {
+                throw new \Exception('Unknown log type: ' . $type);
+            }
+            $this->logger[$type][] = $logger;
+        }
+    }
+
     public function handle($msg)
     {
         if ($this->config->dumpFile !== null) {
@@ -27,17 +63,37 @@ class MessageHandler
             return false;
         }
         list(, $type, $someid, $details) = $matches;
+        $this->log(
+            'msgData',
+            array(
+                'type' => $type,
+                'id' => $someid,
+                'details' => $details
+            )
+        );
 
         if ($type != 'Info') {
             //we only want info messages
-            var_dump($type . ': ' . $details);
             return;
         }
         //Vegw/Ets-Cref:[0xffef]/[0x64] - VEGW_SETUP from upper layer to internal destination: CGPN[**22]->CDPN[41], 
-        var_dump($details);
         $regex = '#CGPN\\[([^\\]]+)\\]->CDPN\\[([^\\]]+)\\]#';
         if (preg_match($regex, $details, $matches)) {
-            var_dump('a call!', $matches);
+            list(, $from, $to) = $matches;
+            $this->log('incomingCall', array('from' => $from, 'to' => $to));
+        }
+    }
+
+    protected function log($type, $arData)
+    {
+        if (!isset($this->logger[$type])) {
+            throw new \Exception('Unknown log type: ' . $type);
+        }
+        
+        if (count($this->logger[$type])) {
+            foreach ($this->logger[$type] as $logger) {
+                $logger->log($type, $arData);
+            }
         }
     }