InverseDocumentFrequency::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 1-3-2017
5
 * Time: 11:41
6
 */
7
8
namespace CloudControl\Cms\search\indexer;
9
10
11
class InverseDocumentFrequency
12
{
13
    /**
14
     * @var \PDO
15
     */
16
    protected $dbHandle;
17
    protected $documentCount;
18
19
    /**
20
     * InverseDocumentFrequency constructor.
21
     *
22
     * @param \PDO $dbHandle
23
     * @param int $documentCount
24
     */
25
    public function __construct($dbHandle, $documentCount)
26
    {
27
        $this->dbHandle = $dbHandle;
28
        $this->documentCount = $documentCount;
29
    }
30
31
    /**
32
     * Formula to calculate:
33
     *        idf(t) = 1 + log ( totalDocuments / (documentsThatContainTheTerm + 1))
34
     * @throws \Exception
35
     */
36
    public function execute()
37
    {
38
        $db = $this->dbHandle;
39
        $db->/** @scrutinizer ignore-call */
40
        sqliteCreateFunction('log', 'log', 1);
41
        $sql = '
42
		INSERT INTO inverse_document_frequency (term, inverseDocumentFrequency)
43
		SELECT DISTINCT term, (1+(log(:documentCount / COUNT(documentPath) + 1))) AS inverseDocumentFrequency
44
					  FROM term_count
45
				  GROUP BY term
46
		';
47
48
        if (!$stmt = $db->prepare($sql)) {
49
            $errorInfo = $db->errorInfo();
50
            $errorMsg = $errorInfo[2];
51
            throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
52
        }
53
        $stmt->bindValue(':documentCount', $this->documentCount);
54
        $result = $stmt->execute();
55
        if ($result === false) {
56
            $errorInfo = $db->errorInfo();
57
            $errorMsg = $errorInfo[2];
58
            throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
59
        }
60
    }
61
}