Add debug message output to call loggers
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallBase.php
1 <?php
2 namespace callnotifier;
3
4 abstract class Logger_CallBase implements Logger
5 {
6     protected $callTypes;
7     protected $msns;
8
9     public $debug = false;
10
11     /**
12      * Create a new call logger.
13      *
14      * @param string $callTypes Which types of call to log:
15      *                          - "i" - incoming calls only
16      *                          - "o" - outgoing calls only
17      *                          - "io" - both incoming and outgoing calls
18      * @param array  $msns      Array of MSN (Multi Subscriber Number) that
19      *                          calls to shall get logged.
20      *                          If the array is empty, calls to all MSNs get
21      *                          logged.
22      */
23     public function __construct($callTypes = 'io', $msns = array())
24     {
25         $this->callTypes = $callTypes;
26         $this->msns      = (array)$msns;
27     }
28
29     /**
30      * Check if the call type (incoming or outgoing) shall be logged.
31      *
32      * @return boolean True if it should be logged, false if not
33      */
34     protected function hasValidType($call)
35     {
36         if ($call->type == CallMonitor_Call::INCOMING && $this->callTypes == 'o') {
37             $this->debug('No valid call type (requested: o)');
38             return false;
39         }
40         if ($call->type == CallMonitor_Call::OUTGOING && $this->callTypes == 'i') {
41             $this->debug('No valid call type (requested: i)');
42             return false;
43         }
44
45         return true;
46     }
47
48     /**
49      * Check if the MSN shall be logged
50      *
51      * @return boolean True if it should be logged, false if not
52      */
53     protected function hasValidMsn($call)
54     {
55         if ($call->type == CallMonitor_Call::INCOMING) {
56             $msn = $call->to;
57         } else {
58             $msn = $call->from;
59         }
60         if (count($this->msns) > 0 && !in_array($msn, $this->msns)) {
61             //msn shall not be logged
62             $this->debug('No valid MSN (requested: ' . $msn . ')');
63             return false;
64         }
65
66         return true;
67     }
68
69     protected function addUnsetVars($call)
70     {
71         static $expectedVars = array(
72             'toName', 'fromName', 'toLocation', 'fromLocation'
73         );
74         foreach ($expectedVars as $varName) {
75             if (!isset($call->$varName)) {
76                 $call->$varName = null;
77             }
78         }
79     }
80
81
82     protected function getNumberString($call, $type)
83     {
84         $varNumber   = $type;
85         $varName     = $type . 'Name';
86         $varLocation = $type . 'Location';
87
88         if ($call->$varName !== null) {
89             return $call->$varName;
90         }
91
92         $str = $this->getNumber($call->$varNumber);
93         if ($call->$varLocation !== null) {
94             $str .= ' aus ' . $call->$varLocation;
95         }
96         return $str;
97     }
98
99     protected function getNumber($number)
100     {
101         if ($number == '') {
102             $number = '*anonym*';
103         }
104         return str_pad($number, 12, ' ', STR_PAD_RIGHT);
105     }
106
107     protected function debug($msg)
108     {
109         if (!$this->debug) {
110             return;
111         }
112         echo $msg . "\n";
113     }
114 }
115
116 ?>