Highway to PSR2
[openemr.git] / common / database / Auditor.php
blob36970a98b1f4704c905cb021971ec9bade905294
1 <?php
2 /**
3 * This class is responsible for shuttling SQL queries to the audit table. All
4 * work is delegated to the existing SQL auditing function `auditSQLEvent` in
5 * `library/log.inc`.
7 * Copyright (C) 2016 Matthew Vita <matthewvita48@gmail.com>
9 * LICENSE: This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>.
20 * @package OpenEMR
21 * @author Matthew Vita <matthewvita48@gmail.com>
22 * @link http://www.open-emr.org
25 namespace common\database;
27 use \Doctrine\DBAL\Logging\SQLLogger;
29 final class Auditor implements SQLLogger
31 /**
32 * Executed SQL queries with the following keys in each inner object:
34 * `params`: The optional param values for the current SQL query.
35 * `sql`: The raw SQL for the current SQL query (shows `?` for params if given).
37 * @note a query will be removed from the dictionary once complete.
39 public $queries = array();
41 /**
42 * Index of the current query in the $queries dictionary.
44 public $currentQueryIndex = 0;
46 /**
47 * Logger for noting sql query information.
49 private $logger;
51 /**
52 * Default constructor. This is here for completeness, it is
53 * essentially a no-op.
55 public function __construct()
57 $this->logger = new \common\logging\Logger("\common\database\Auditor");
60 /**
61 * Intercepts the SQL query to be performed by the ORM.
63 * @param $sql the raw SQL (shows `?` for params if given).
64 * @param $params the optional param values for the SQL.
65 * @param $types the optional param types.
67 public function startQuery($sql, array $params = null, array $types = null)
69 $this->queries[++$this->currentQueryIndex] = array('sql' => $sql, 'params' => $params);
70 $this->logger->trace("sql: " . $sql);
73 /**
74 * Triggers when an SQL query has been performed and sends
75 * the information to the audit table.
77 * @note this is only called if the query succeeded.
78 * @note the query is from the dictionary once complete at this point.
80 public function stopQuery()
82 $sql = $this->queries[$this->currentQueryIndex]['sql'];
83 $params = $this->queries[$this->currentQueryIndex]['params'];
84 auditSQLEvent($sql, true, $params);
85 unset($this->queries[$this->currentQueryIndex]);