Passed
Push — master ( 3f6c85...9c6499 )
by Jens
02:40
created

TermCount::executeStoreDocumentTermCount()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 5
Ratio 50 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 3
dl 5
loc 10
rs 9.4285
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\Storage;
14
15
class TermCount
16
{
17
	/**
18
	 * @var \PDO
19
	 */
20
	protected $dbHandle;
21
	protected $documents;
22
	protected $filters;
23
	protected $storage;
24
25
	/**
26
	 * TermCount constructor.
27
	 *
28
	 * @param \PDO    $dbHandle
29
	 * @param array   $documents
30
	 * @param array   $filters
31
	 * @param Storage $jsonStorage
32
	 */
33
	public function __construct($dbHandle, $documents, $filters, $jsonStorage)
34
	{
35
		$this->dbHandle = $dbHandle;
36
		$this->documents = $documents;
37
		$this->filters = $filters;
38
		$this->storage = $jsonStorage;
39
	}
40
41
	public function execute()
42
	{
43
		$this->iterateDocumentsAndCreateTermCount($this->documents);
44
	}
45
46 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...
47
	{
48
		foreach ($this->filters as $filterName) {
49
			$filterClassName = '\library\search\filters\\' . $filterName;
50
			$filter = new $filterClassName($tokens);
51
			$tokens = $filter->getFilterResults();
52
		}
53
		return $tokens;
54
	}
55
56
	protected function storeDocumentTermCount($document, $documentTermCount)
57
	{
58
		$db = $this->dbHandle;
59
		$sqlStart = '
60
			INSERT INTO `term_count` (`documentPath`, `term`, `count`, `field`)
61
				 VALUES ';
62
		$sql = $sqlStart;
63
		$values = array();
64
		$quotedDocumentPath = $db->quote($document->path);
65
		$i = 0;
66
		foreach ($documentTermCount as $field => $countArray) {
67
			$quotedField = $db->quote($field);
68
			foreach ($countArray as $term => $count) {
69
				$values[] = $quotedDocumentPath . ', ' . $db->quote($term) . ', ' . $db->quote($count) . ', ' . $quotedField;
70
				$i += 1;
71
				if ($i >= Indexer::SQLITE_MAX_COMPOUND_SELECT) {
72
					$this->executeStoreDocumentTermCount($values, $sql, $db);
73
					$values = array();
74
					$sql = $sqlStart;
75
					$i = 0;
76
				}
77
			}
78
		}
79
		if (count($values) != 0) {
80
			$this->executeStoreDocumentTermCount($values, $sql, $db);
81
		}
82
	}
83
84
	/**
85
	 * @param $values
86
	 * @param $sql
87
	 * @param $db
88
	 *
89
	 * @throws \Exception
90
	 */
91
	protected function executeStoreDocumentTermCount($values, $sql, $db)
92
	{
93
		$sql .= '(' . implode('),' . PHP_EOL . '(', $values) . ');';
94
		$stmt = $db->prepare($sql);
95 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...
96
			$errorInfo = $db->errorInfo();
97
			$errorMsg = $errorInfo[2];
98
			throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
99
		}
100
	}
101
102
	/**
103
	 * @param $document
104
	 */
105
	private function createTermCountForDocument($document)
106
	{
107
		$tokenizer = new DocumentTokenizer($document, $this->storage);
108
		$tokens = $tokenizer->getTokens();
109
		$documentTermCount = $this->applyFilters($tokens);
110
		$this->storeDocumentTermCount($document, $documentTermCount);
111
	}
112
113
	/**
114
	 * @param $documents
115
	 */
116
	private function iterateDocumentsAndCreateTermCount($documents)
117
	{
118
		foreach ($documents as $document) {
119
			if ($document->type === 'folder') {
120
				$this->iterateDocumentsAndCreateTermCount($document->content);
121
			} else {
122
				$this->createTermCountForDocument($document);
123
			}
124
		}
125
	}
126
}