default logging types are io, not only i
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallDb.php
1 <?php
2 namespace callnotifier;
3
4 /**
5  * Logs finished calls into a SQL database.
6  *
7  * To use this, setup the database table using the script
8  * in docs/create-call-log.sql
9  */
10 class Logger_CallDb extends Logger_CallBase
11 {
12     protected $db;
13     protected $dsn;
14     protected $username;
15     protected $password;
16
17     /**
18      * Create new detailler object
19      *
20      * @param string $dsn      PDO connection string, for example
21      *                         'mysql:host=dojo;dbname=opengeodb'
22      * @param string $username Database username
23      * @param string $password Database password
24      */
25     public function __construct(
26         $dsn, $username, $password, $callTypes = 'io', $msns = array()
27     ) {
28         parent::__construct($callTypes, $msns);
29
30         $this->dsn      = $dsn;
31         $this->username = $username;
32         $this->password = $password;
33         //check if the credentials are correct
34         $this->connect();
35     }
36
37     /**
38      * Connect to the SQL server.
39      * SQL servers close the connection automatically after some hours,
40      * and since calls often don't come in every minute, we will have
41      * disconnects in between.
42      * Thus, we will reconnect on every location load.
43      *
44      * @return void
45      */
46     protected function connect()
47     {
48         $this->db = new \PDO(
49             $this->dsn, $this->username, $this->password,
50             array(
51                 \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
52                 \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
53                 \PDO::ATTR_PERSISTENT => true
54             )
55         );
56     }
57
58     public function log($type, $arData)
59     {
60         if ($type != 'finishedCall') {
61             return;
62         }
63
64         $call = $arData['call'];
65         if (!$this->hasValidType($call)) {
66             return;
67         }
68         if (!$this->hasValidMsn($call)) {
69             return;
70         }
71
72         $this->addUnsetVars($call);
73
74         $this->connect();
75         $stmt = $this->prepareDbStatement();
76         $ret  = $stmt->execute(
77             array(
78                 'call_start'         => date('Y-m-d H:i:s', $call->start),
79                 'call_end'           => date('Y-m-d H:i:s', $call->end),
80                 'call_type'          => $call->type,
81                 'call_from'          => $call->from,
82                 'call_from_name'     => $call->fromName,
83                 'call_from_location' => $call->fromLocation,
84                 'call_to'            => $call->to,
85                 'call_to_name'       => $call->toName,
86                 'call_to_location'   => $call->toLocation,
87                 'call_length'        => $call->end - $call->start
88             )
89         );
90         if ($ret === false) {
91             throw new \Exception(
92                 'Error logging call to database: '
93                 . implode(' / ', $stmt->errorInfo())
94             );
95         }
96     }
97
98     protected function prepareDbStatement()
99     {
100         return $this->db->prepare(
101             'INSERT INTO finished ('
102             . '  call_start'
103             . ', call_end'
104             . ', call_type'
105             . ', call_from'
106             . ', call_from_name'
107             . ', call_from_location'
108             . ', call_to'
109             . ', call_to_name'
110             . ', call_to_location'
111             . ', call_length'
112             . ') VALUES ('
113             . '  :call_start'
114             . ', :call_end'
115             . ', :call_type'
116             . ', :call_from'
117             . ', :call_from_name'
118             . ', :call_from_location'
119             . ', :call_to'
120             . ', :call_to_name'
121             . ', :call_to_location'
122             . ', :call_length'
123             . ')'
124         );
125     }
126
127 }
128
129 ?>