Passed
Push — develop ( 169afe...f2bd80 )
by Jens
02:39
created

TermCount::storeDocumentTermCount()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 8
nop 2
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 1-3-2017
5
 * Time: 10:22
6
 */
7
8
namespace library\search\indexer;
9
10
11
use library\search\DocumentTokenizer;
12
use library\search\Indexer;
13
use library\storage\JsonStorage;
14
15
class TermCount
16
{
17
	protected $dbHandle;
18
	protected $documents;
19
	protected $filters;
20
	protected $storage;
21
22
	/**
23
	 * TermCount constructor.
24
	 *
25
	 * @param resource $dbHandle
26
	 * @param array $documents
27
	 * @param array $filters
28
	 * @param JsonStorage $jsonStorage
29
	 */
30
	public function __construct($dbHandle, $documents, $filters, $jsonStorage)
31
	{
32
		$this->dbHandle = $dbHandle;
33
		$this->documents = $documents;
34
		$this->filters = $filters;
35
		$this->storage = $jsonStorage;
36
	}
37
38
	public function execute()
39
	{
40
		foreach ($this->documents as $document) {
41
			$tokenizer = new DocumentTokenizer($document, $this->storage);
42
			$tokens = $tokenizer->getTokens();
43
			$documentTermCount = $this->applyFilters($tokens);
44
			$this->storeDocumentTermCount($document, $documentTermCount);
45
		}
46
	}
47
48 View Code Duplication
	protected function applyFilters($tokens)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
	{
50
		foreach ($this->filters as $filterName) {
51
			$filterClassName = '\library\search\filters\\' . $filterName;
52
			$filter = new $filterClassName($tokens);
53
			$tokens = $filter->getFilterResults();
54
		}
55
		return $tokens;
56
	}
57
58
	protected function storeDocumentTermCount($document, $documentTermCount)
59
	{
60
		$db = $this->dbHandle;
61
		$sqlStart = '
62
			INSERT INTO `term_count` (`documentPath`, `term`, `count`, `field`)
63
				 VALUES ';
64
		$sql = $sqlStart;
65
		$values = array();
66
		$quotedDocumentPath = $db->quote($document->path);
0 ignored issues
show
Bug introduced by
The method quote cannot be called on $db (of type resource).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
67
		$i = 0;
68
		foreach ($documentTermCount as $field => $countArray) {
69
			$quotedField = $db->quote($field);
0 ignored issues
show
Bug introduced by
The method quote cannot be called on $db (of type resource).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
70
			foreach ($countArray as $term => $count) {
71
				$values[] = $quotedDocumentPath . ', ' . $db->quote($term) . ', ' . $db->quote($count) . ', ' . $quotedField;
0 ignored issues
show
Bug introduced by
The method quote cannot be called on $db (of type resource).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
72
				$i += 1;
73
				if ($i >= Indexer::SQLITE_MAX_COMPOUND_SELECT) {
74
					$this->executeStoreDocumentTermCount($values, $sql, $db);
75
					$values = array();
76
					$sql = $sqlStart;
77
					$i = 0;
78
				}
79
			}
80
		}
81
		if (count($values) != 0) {
82
			$this->executeStoreDocumentTermCount($values, $sql, $db);
83
		}
84
	}
85
86
	/**
87
	 * @param $values
88
	 * @param $sql
89
	 * @param $db
90
	 *
91
	 * @throws \Exception
92
	 */
93
	protected function executeStoreDocumentTermCount($values, $sql, $db)
94
	{
95
		$sql .= '(' . implode('),' . PHP_EOL . '(', $values) . ');';
96
97
		$stmt = $db->prepare($sql);
98 View Code Duplication
		if ($stmt === false || !$stmt->execute()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
			$errorInfo = $db->errorInfo();
100
			$errorMsg = $errorInfo[2];
101
			throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
102
		}
103
	}
104
}