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 96fe76f550ba446e8a586079ebb513cf0df4eee4..31ad0bc7ce8408af85115d75081e0b24939d82f0 100644 (file)
@@ -3,6 +3,65 @@ namespace callnotifier;
 
 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(
index b45577430a39e49425adb874ac87fdb36348bb54..e2cdc6fd37946daae4e2310217b572e99b5a47c0 100644 (file)
@@ -17,8 +17,11 @@ class Logger_CallDb extends Logger_CallBase
      * @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(
@@ -60,6 +63,13 @@ class Logger_CallDb extends Logger_CallBase
         }
 
         $call = $arData['call'];
+        if (!$this->hasValidType($call)) {
+            return;
+        }
+        if (!$this->hasValidMsn($call)) {
+            return;
+        }
+
         $this->addUnsetVars($call);
 
         $ret = $this->stmt->execute(
index 4720c4c920bf4f3dba081171db314ed89d879853..d617f1d30193323b3422a71cb34a199d9a1f883b 100644 (file)
@@ -5,18 +5,26 @@ class Logger_CallDreambox extends Logger_CallBase
 {
     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)
     {
-        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 ad13406dc6eedcd6e3e8a872a2d366ab4f63d5c9..70fad4a4d99c7c788ee25045b7b92c336a179289 100644 (file)
@@ -7,12 +7,23 @@ class Logger_CallEcho extends Logger_CallBase
     {
         switch ($type) {
         case 'startingCall':
-            $this->displayStart($arData['call']);
+            $displayMethod = 'displayStart';
             break;
         case 'finishedCall':
-            $this->displayFinished($arData['call']);
+            $displayMethod = 'displayFinished';
             break;
+        default:
+            return;
         }
+
+        $call = $arData['call'];
+        if (!$this->hasValidType($call)) {
+            return;
+        }
+        if (!$this->hasValidMsn($call)) {
+            return;
+        }
+        $this->$displayMethod($arData['call']);
     }
 
 
index 850e840f0c072c160a8dc3a64cd744d53fad50e0..12a5b08f2548eb128a3638a719a4a7b4b0f84188 100644 (file)
@@ -5,8 +5,6 @@ class Logger_CallFile extends Logger_CallBase
 {
     protected $file;
     protected $fileHdl;
-    protected $callTypes;
-    protected $msns;
 
     /**
      * 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'];
-
-        //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;
         }
-
-        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;
         }
 
index 477a253e281f6765eb2927f1a19adf5e5b1024c3..74c930e0ef6f1b1b4c9604ab6a801c8ef0f84fb8 100644 (file)
@@ -7,12 +7,23 @@ class Logger_CallNotifySend extends Logger_CallBase
     {
         switch ($type) {
         case 'startingCall':
-            $this->displayStart($arData['call']);
+            $displayMethod = 'displayStart';
             break;
         case 'finishedCall':
-            $this->displayFinished($arData['call']);
+            $displayMethod = 'displayFinished';
             break;
+        default:
+            return;
         }
+
+        $call = $arData['call'];
+        if (!$this->hasValidType($call)) {
+            return;
+        }
+        if (!$this->hasValidMsn($call)) {
+            return;
+        }
+        $this->$displayMethod($arData['call']);
     }