all loggers support call type and MSN filtering now
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallFile.php
1 <?php
2 namespace callnotifier;
3
4 class Logger_CallFile extends Logger_CallBase
5 {
6     protected $file;
7     protected $fileHdl;
8
9     /**
10      * Create a new file call logger. It logs finished calls into a file.
11      *
12      * @param string $file      Path to the file to log the calls in.
13      * @param string $callTypes Which types of call to log:
14      *                          - "i" - incoming calls only
15      *                          - "o" - outgoing calls only
16      *                          - "io" - both incoming and outgoing calls
17      * @param array  $msns      Array of MSN (Multi Subscriber Number) that
18      *                          calls to shall get logged.
19      *                          If the array is empty, calls to all MSNs get
20      *                          logged.
21      */
22     public function __construct(
23         $file,
24         $callTypes = 'io',
25         $msns = array()
26     ) {
27         $this->file      = $file;
28         $this->callTypes = $callTypes;
29         $this->msns      = (array)$msns;
30
31         $this->fileHdl = fopen($this->file, 'a');
32         if (!$this->fileHdl) {
33             throw new \Exception(
34                 'Cannot open call log file for writing: ' . $this->file
35             );
36         }
37     }
38
39     public function log($type, $arData)
40     {
41         if ($type != 'finishedCall') {
42             return;
43         }
44
45         $call = $arData['call'];
46         if (!$this->hasValidType($call)) {
47             return;
48         }
49         if (!$this->hasValidMsn($call)) {
50             return;
51         }
52
53         fwrite($this->fileHdl, $this->createLogEntry($call));
54     }
55
56
57     protected function createLogEntry(CallMonitor_Call $call)
58     {
59         $this->addUnsetVars($call);
60         $str = date('Y-m-d H:i:s', $call->start);
61         if ($call->type == CallMonitor_Call::INCOMING) {
62             $str .= ' ' . $call->to
63                 . ' von  ' . $this->getNumberString($call, 'from');
64         } else {
65             $str .= ' ' . $call->from
66                 . ' nach ' . $this->getNumberString($call, 'to');
67         }
68
69         $str .= ', Dauer ' . date('H:i:s', $call->end - $call->start - 3600);
70
71         return $str . "\n";
72     }
73
74 }
75
76 ?>