Add debug message output to call loggers
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallBase.php
index 96fe76f550ba446e8a586079ebb513cf0df4eee4..2a1d3e8a22bc533c401be8143201b7144c53fd4d 100644 (file)
@@ -3,6 +3,69 @@ namespace callnotifier;
 
 abstract class Logger_CallBase implements Logger
 {
+    protected $callTypes;
+    protected $msns;
+
+    public $debug = false;
+
+    /**
+     * 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') {
+            $this->debug('No valid call type (requested: o)');
+            return false;
+        }
+        if ($call->type == CallMonitor_Call::OUTGOING && $this->callTypes == 'i') {
+            $this->debug('No valid call type (requested: 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
+            $this->debug('No valid MSN (requested: ' . $msn . ')');
+            return false;
+        }
+
+        return true;
+    }
+
     protected function addUnsetVars($call)
     {
         static $expectedVars = array(
@@ -41,6 +104,13 @@ abstract class Logger_CallBase implements Logger
         return str_pad($number, 12, ' ', STR_PAD_RIGHT);
     }
 
+    protected function debug($msg)
+    {
+        if (!$this->debug) {
+            return;
+        }
+        echo $msg . "\n";
+    }
 }
 
 ?>