ConstantDataCollector   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 77
ccs 29
cts 29
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatistics() 0 8 1
A collectData() 0 33 4
A deleteData() 0 6 1
1
<?php
2
/**
3
 * This file is part of the Code-Insight library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/code-insight
9
 */
10
11
namespace ConsoleHelpers\CodeInsight\KnowledgeBase\DataCollector;
12
13
14
use Go\ParserReflection\ReflectionFileNamespace;
15
16
class ConstantDataCollector extends AbstractDataCollector
17
{
18
19
	/**
20
	 * Collect data from a namespace.
21
	 *
22
	 * @param integer                 $file_id   File id.
23
	 * @param ReflectionFileNamespace $namespace Namespace.
24
	 *
25
	 * @return void
26
	 */
27 3
	public function collectData($file_id, ReflectionFileNamespace $namespace)
28
	{
29 3
		$constants = $namespace->getConstants(true);
30
31 3
		$sql = 'SELECT Name
32
				FROM Constants
33
				WHERE FileId = :file_id';
34 3
		$old_constants = $this->db->fetchCol($sql, array(
35 3
			'file_id' => $file_id,
36
		));
37
38 3
		$insert_sql = 'INSERT INTO Constants (FileId, Name, Value) VALUES (:file_id, :name, :value)';
39 3
		$update_sql = 'UPDATE Constants SET Value = :value WHERE FileId = :file_id AND Name = :name';
40
41 3
		foreach ( $constants as $constant_name => $constant_value ) {
42 3
			$this->db->perform(
43 3
				in_array($constant_name, $old_constants) ? $update_sql : $insert_sql,
44
				array(
45 3
					'file_id' => $file_id,
46 3
					'name' => $constant_name,
47 3
					'value' => json_encode($constant_value),
48
				)
49
			);
50
		}
51
52 3
		$delete_constants = array_diff($old_constants, array_keys($constants));
53
54 3
		if ( $delete_constants ) {
55 1
			$sql = 'DELETE FROM Constants
56
					WHERE FileId = :file_id AND Name IN (:names)';
57 1
			$this->db->perform($sql, array(
58 1
				'file_id' => $file_id,
59 1
				'names' => $delete_constants,
60
			));
61
		}
62 3
	}
63
64
	/**
65
	 * Delete previously collected data for a files.
66
	 *
67
	 * @param array $file_ids File IDs.
68
	 *
69
	 * @return void
70
	 */
71 1
	public function deleteData(array $file_ids)
72
	{
73 1
		$sql = 'DELETE FROM Constants
74
				WHERE FileId IN (:file_ids)';
75 1
		$this->db->perform($sql, array(
76 1
			'file_ids' => $file_ids,
77
		));
78 1
	}
79
80
	/**
81
	 * Returns statistics about the code.
82
	 *
83
	 * @return array
84
	 */
85 1
	public function getStatistics()
86
	{
87 1
		$sql = 'SELECT COUNT(*)
88
				FROM Constants';
89 1
		$constant_count = $this->db->fetchValue($sql);
90
91
		return array(
92 1
			'Constants' => $constant_count,
93
		);
94
	}
95
96
}
97