aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2012-08-07 15:07:05 +0200
committerChristian Weiske <cweiske@cweiske.de>2012-08-07 15:07:05 +0200
commit67eb8026d25dd261358ef5de1c8a5a6111a8e282 (patch)
tree16758caaa50b7e60f49757ebcef85e00375f2ef4 /src
parentf4bd5ef0c711aa9a3d0f6b468370a8c14c0e247a (diff)
downloadauerswald-callnotifier-67eb8026d25dd261358ef5de1c8a5a6111a8e282.tar.gz
auerswald-callnotifier-67eb8026d25dd261358ef5de1c8a5a6111a8e282.zip
call logging to database
Diffstat (limited to 'src')
-rw-r--r--src/callnotifier/CLI.php8
-rw-r--r--src/callnotifier/Logger/CallBase.php19
-rw-r--r--src/callnotifier/Logger/CallDb.php83
-rw-r--r--src/callnotifier/Logger/CallFile.php14
4 files changed, 111 insertions, 13 deletions
diff --git a/src/callnotifier/CLI.php b/src/callnotifier/CLI.php
index 6165f15..7109e73 100644
--- a/src/callnotifier/CLI.php
+++ b/src/callnotifier/CLI.php
@@ -64,6 +64,14 @@ class CLI
new Logger_CallFile('all.log'),
array('finishedCall')
);
+ $log->addLogger(
+ new Logger_CallDb(
+ 'mysql:host=localhost;dbname=callnotifier',
+ 'callnotifier',
+ 'callnotifier'
+ ),
+ array('finishedCall')
+ );
$handler = new MessageHandler($this->config, $log, $callMonitor);
diff --git a/src/callnotifier/Logger/CallBase.php b/src/callnotifier/Logger/CallBase.php
new file mode 100644
index 0000000..8bd143c
--- /dev/null
+++ b/src/callnotifier/Logger/CallBase.php
@@ -0,0 +1,19 @@
+<?php
+namespace callnotifier;
+
+abstract class Logger_CallBase implements Logger
+{
+ protected function addUnsetVars($call)
+ {
+ static $expectedVars = array(
+ 'toName', 'fromName', 'toLocation', 'fromLocation'
+ );
+ foreach ($expectedVars as $varName) {
+ if (!isset($call->$varName)) {
+ $call->$varName = null;
+ }
+ }
+ }
+}
+
+?>
diff --git a/src/callnotifier/Logger/CallDb.php b/src/callnotifier/Logger/CallDb.php
new file mode 100644
index 0000000..110e963
--- /dev/null
+++ b/src/callnotifier/Logger/CallDb.php
@@ -0,0 +1,83 @@
+<?php
+namespace callnotifier;
+
+class Logger_CallDb extends Logger_CallBase
+{
+ /**
+ * Create new detailler object
+ *
+ * @param string $dsn PDO connection string, for example
+ * 'mysql:host=dojo;dbname=opengeodb'
+ * @param string $username Database username
+ * @param string $password Database password
+ */
+ public function __construct($dsn, $username, $password)
+ {
+ $this->db = new \PDO(
+ $dsn, $username, $password,
+ array(
+ \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
+ \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
+ )
+ );
+ $this->stmt = $this->db->prepare(
+ 'INSERT INTO finished ('
+ . ' call_start'
+ . ', call_end'
+ . ', call_type'
+ . ', call_from'
+ . ', call_from_name'
+ . ', call_from_location'
+ . ', call_to'
+ . ', call_to_name'
+ . ', call_to_location'
+ . ', call_length'
+ . ') VALUES ('
+ . ' :call_start'
+ . ', :call_end'
+ . ', :call_type'
+ . ', :call_from'
+ . ', :call_from_name'
+ . ', :call_from_location'
+ . ', :call_to'
+ . ', :call_to_name'
+ . ', :call_to_location'
+ . ', :call_length'
+ . ')'
+ );
+ }
+
+ public function log($type, $arData)
+ {
+ if ($type != 'finishedCall') {
+ return;
+ }
+
+ $call = $arData['call'];
+ $this->addUnsetVars($call);
+
+ $ret = $this->stmt->execute(
+ array(
+ 'call_start' => date('Y-m-d H:i:s', $call->start),
+ 'call_end' => date('Y-m-d H:i:s', $call->end),
+ 'call_type' => $call->type,
+ 'call_from' => $call->from,
+ 'call_from_name' => $call->fromName,
+ 'call_from_location' => $call->fromLocation,
+ 'call_to' => $call->to,
+ 'call_to_name' => $call->toName,
+ 'call_to_location' => $call->toLocation,
+ 'call_length' => $call->end - $call->start
+ )
+ );
+ if ($ret === false) {
+ throw new \Exception(
+ 'Error logging call to database: '
+ . implode(' / ', $this->stmt->errorInfo())
+ );
+ }
+ }
+
+}
+
+?>
diff --git a/src/callnotifier/Logger/CallFile.php b/src/callnotifier/Logger/CallFile.php
index 85bf287..ba45137 100644
--- a/src/callnotifier/Logger/CallFile.php
+++ b/src/callnotifier/Logger/CallFile.php
@@ -1,7 +1,7 @@
<?php
namespace callnotifier;
-class Logger_CallFile implements Logger
+class Logger_CallFile extends Logger_CallBase
{
protected $file;
protected $fileHdl;
@@ -101,18 +101,6 @@ class Logger_CallFile implements Logger
return str_pad($number, 12, ' ', STR_PAD_RIGHT);
}
- protected function addUnsetVars($call)
- {
- static $expectedVars = array(
- 'toName', 'fromName', 'toLocation', 'fromLocation'
- );
- foreach ($expectedVars as $varName) {
- if (!isset($call->$varName)) {
- $call->$varName = null;
- }
- }
- }
-
}
?>