QueryCounter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Elgg\Database;
3
4
/**
5
 * Count queries performed
6
 *
7
 * Do not use directly. Use _elgg_db_get_query_counter().
8
 *
9
 * @access private
10
 *
11
 * @package    Elgg.Core
12
 * @subpackage Database
13
 * @since      1.9.0
14
 */
15
class QueryCounter {
16
17
	/**
18
	 * @var int
19
	 */
20
	protected $initial;
21
22
	/**
23
	 * @var \Elgg\Database
24
	 */
25
	protected $db;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @param \Elgg\Database $db Elgg's database
31
	 */
32
	public function __construct(\Elgg\Database $db) {
33
		$this->db = $db;
34
		$this->initial = $db->getQueryCount();
35
	}
36
37
	/**
38
	 * Get the number of queries performed since the object was constructed
39
	 *
40
	 * @return int # of queries
41
	 */
42
	public function getDelta() {
43
		return $this->db->getQueryCount() - $this->initial;
44
	}
45
46
	/**
47
	 * Create header X-ElggQueryDelta-* with the delta
48
	 *
49
	 * @see getDelta()
50
	 *
51
	 * @param string $key Key to add to HTTP header name
52
	 *
53
	 * @return void
54
	 */
55
	public function setDeltaHeader($key = 'Default') {
56
		$delta = $this->getDelta();
57
58
		header("X-ElggQueryDelta-$key: $delta", true);
59
	}
60
61
	/**
62
	 * Get SCRIPT element which sends the delta to console.log
63
	 *
64
	 * @see getDelta()
65
	 *
66
	 * @param string $key Key to display in console log
67
	 *
68
	 * @return string markup of SCRIPT element
69
	 */
70
	public function getDeltaScript($key = 'Default') {
71
		$delta = $this->getDelta();
72
73
		$msg = json_encode("ElggQueryDelta-$key: $delta");
74
		return "<script>console.log($msg)</script>";
75
	}
76
}
77
78