all loggers support call type and MSN filtering now
authorChristian Weiske <cweiske@cweiske.de>
Fri, 10 Aug 2012 20:32:08 +0000 (22:32 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 10 Aug 2012 20:32:08 +0000 (22:32 +0200)
src/callnotifier/Logger/CallBase.php
src/callnotifier/Logger/CallDb.php
src/callnotifier/Logger/CallDreambox.php
src/callnotifier/Logger/CallEcho.php
src/callnotifier/Logger/CallFile.php
src/callnotifier/Logger/CallNotifySend.php

index 96fe76f..31ad0bc 100644 (file)
@@ -3,6 +3,65 @@ namespace callnotifier;
 
 abstract class Logger_CallBase implements Logger
 {
 
 abstract class Logger_CallBase implements Logger
 {
+    protected $callTypes;
+    protected $msns;
+
+
+    /**
+     * Create a new call logger.
+     *
+     * @param string $callTypes Which types of call to log:
+     *                          - "i" - incoming calls only
+     *                          - "o" - outgoing calls only
+     *                          - "io" - both incoming and outgoing calls
+     * @param array  $msns      Array of MSN (Multi Subscriber Number) that
+     *                          calls to shall get logged.
+     *                          If the array is empty, calls to all MSNs get
+     *                          logged.
+     */
+    public function __construct($callTypes = 'io', $msns = array())
+    {
+        $this->callTypes = $callTypes;
+        $this->msns      = (array)$msns;
+    }
+
+    /**
+     * Check if the call type (incoming or outgoing) shall be logged.
+     *
+     * @return boolean True if it should be logged, false if not
+     */
+    protected function hasValidType($call)
+    {
+        if ($call->type == CallMonitor_Call::INCOMING && $this->callTypes == 'o') {
+            return false;
+        }
+        if ($call->type == CallMonitor_Call::OUTGOING && $this->callTypes == 'i') {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Check if the MSN shall be logged
+     *
+     * @return boolean True if it should be logged, false if not
+     */
+    protected function hasValidMsn($call)
+    {
+        if ($call->type == CallMonitor_Call::INCOMING) {
+            $msn = $call->to;
+        } else {
+            $msn = $call->from;
+        }
+        if (count($this->msns) > 0 && !in_array($msn, $this->msns)) {
+            //msn shall not be logged
+            return false;
+        }
+
+        return true;
+    }
+
     protected function addUnsetVars($call)
     {
         static $expectedVars = array(
     protected function addUnsetVars($call)
     {
         static $expectedVars = array(
index b455774..e2cdc6f 100644 (file)
@@ -17,8 +17,11 @@ class Logger_CallDb extends Logger_CallBase
      * @param string $username Database username
      * @param string $password Database password
      */
      * @param string $username Database username
      * @param string $password Database password
      */
-    public function __construct($dsn, $username, $password)
-    {
+    public function __construct(
+        $dsn, $username, $password,
+        $callTypes = 'i', $msns = array()
+    ) {
+        parent::__construct($callTypes, $msns);
         $this->db = new \PDO(
             $dsn, $username, $password,
             array(
         $this->db = new \PDO(
             $dsn, $username, $password,
             array(
@@ -60,6 +63,13 @@ class Logger_CallDb extends Logger_CallBase
         }
 
         $call = $arData['call'];
         }
 
         $call = $arData['call'];
+        if (!$this->hasValidType($call)) {
+            return;
+        }
+        if (!$this->hasValidMsn($call)) {
+            return;
+        }
+
         $this->addUnsetVars($call);
 
         $ret = $this->stmt->execute(
         $this->addUnsetVars($call);
 
         $ret = $this->stmt->execute(
index 4720c4c..d617f1d 100644 (file)
@@ -5,18 +5,26 @@ class Logger_CallDreambox extends Logger_CallBase
 {
     protected $host;
 
 {
     protected $host;
 
-    public function __construct($host)
+    public function __construct($host, $callTypes = 'i', $msns = array())
     {
     {
+        parent::__construct($callTypes, $msns);
         $this->host = $host;
     }
 
     public function log($type, $arData)
     {
         $this->host = $host;
     }
 
     public function log($type, $arData)
     {
-        switch ($type) {
-        case 'startingCall':
-            $this->displayStart($arData['call']);
-            break;
+        if ($type != 'startingCall') {
+            return;
+        }
+
+        $call = $arData['call'];
+        if (!$this->hasValidType($call)) {
+            return;
+        }
+        if (!$this->hasValidMsn($call)) {
+            return;
         }
         }
+        $this->displayStart($call);
     }
 
 
     }
 
 
index ad13406..70fad4a 100644 (file)
@@ -7,12 +7,23 @@ class Logger_CallEcho extends Logger_CallBase
     {
         switch ($type) {
         case 'startingCall':
     {
         switch ($type) {
         case 'startingCall':
-            $this->displayStart($arData['call']);
+            $displayMethod = 'displayStart';
             break;
         case 'finishedCall':
             break;
         case 'finishedCall':
-            $this->displayFinished($arData['call']);
+            $displayMethod = 'displayFinished';
             break;
             break;
+        default:
+            return;
         }
         }
+
+        $call = $arData['call'];
+        if (!$this->hasValidType($call)) {
+            return;
+        }
+        if (!$this->hasValidMsn($call)) {
+            return;
+        }
+        $this->$displayMethod($arData['call']);
     }
 
 
     }
 
 
index 850e840..12a5b08 100644 (file)
@@ -5,8 +5,6 @@ class Logger_CallFile extends Logger_CallBase
 {
     protected $file;
     protected $fileHdl;
 {
     protected $file;
     protected $fileHdl;
-    protected $callTypes;
-    protected $msns;
 
     /**
      * Create a new file call logger. It logs finished calls into a file.
 
     /**
      * Create a new file call logger. It logs finished calls into a file.
@@ -45,22 +43,10 @@ class Logger_CallFile extends Logger_CallBase
         }
 
         $call = $arData['call'];
         }
 
         $call = $arData['call'];
-
-        //check if call type matches
-        if ($call->type == CallMonitor_Call::INCOMING && $this->callTypes == 'o') {
-            return;
-        }
-        if ($call->type == CallMonitor_Call::OUTGOING && $this->callTypes == 'i') {
+        if (!$this->hasValidType($call)) {
             return;
         }
             return;
         }
-
-        if ($call->type == CallMonitor_Call::INCOMING) {
-            $msn = $call->to;
-        } else {
-            $msn = $call->from;
-        }
-        if (count($this->msns) > 0 && !in_array($msn, $this->msns)) {
-            //msn shall not be logged
+        if (!$this->hasValidMsn($call)) {
             return;
         }
 
             return;
         }
 
index 477a253..74c930e 100644 (file)
@@ -7,12 +7,23 @@ class Logger_CallNotifySend extends Logger_CallBase
     {
         switch ($type) {
         case 'startingCall':
     {
         switch ($type) {
         case 'startingCall':
-            $this->displayStart($arData['call']);
+            $displayMethod = 'displayStart';
             break;
         case 'finishedCall':
             break;
         case 'finishedCall':
-            $this->displayFinished($arData['call']);
+            $displayMethod = 'displayFinished';
             break;
             break;
+        default:
+            return;
         }
         }
+
+        $call = $arData['call'];
+        if (!$this->hasValidType($call)) {
+            return;
+        }
+        if (!$this->hasValidMsn($call)) {
+            return;
+        }
+        $this->$displayMethod($arData['call']);
     }
 
 
     }