QueryCounter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDelta() 0 3 1
A setDeltaHeader() 0 5 1
A getDeltaScript() 0 6 1
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