SearchDbConnected   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeDb() 0 4 2
A configureDatabase() 0 6 1
A __construct() 0 5 1
A getSearchDbHandle() 0 7 2
A isDatabaseConfigured() 0 6 1
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 21-2-2017
5
 * Time: 17:05
6
 */
7
8
namespace CloudControl\Cms\search;
9
10
11
use CloudControl\Cms\storage\Storage;
12
13
/**
14
 * Abstract Class SearchDbConnected
15
 * Handles connection with the search index database
16
 * @package CloudControl\Cms\search
17
 */
18
abstract class SearchDbConnected
19
{
20
    /**
21
     * @var \PDO
22
     */
23
    protected $searchDbHandle;
24
25
    /**
26
     * @var \CloudControl\Cms\storage\Storage
27
     */
28
    protected $storage;
29
    /**
30
     * @var string
31
     */
32
    protected $storageDir;
33
34
    /**
35
     * Indexer constructor.
36
     *
37
     * @param \CloudControl\Cms\storage\Storage $storage
38
     */
39
    public function __construct(Storage $storage)
40
    {
41
        $this->storageDir = $storage->getStorageDir();
42
        $this->storage = $storage;
43
        $this->initializeDb();
44
    }
45
46
    protected function initializeDb()
47
    {
48
        if (!$this->isDatabaseConfigured()) {
49
            $this->configureDatabase();
50
        }
51
    }
52
53
    protected function configureDatabase()
54
    {
55
        $db = $this->getSearchDbHandle();
56
        $sqlPath = __DIR__ . DIRECTORY_SEPARATOR . '../cc/install/search.sql';
57
        $searchSql = file_get_contents($sqlPath);
58
        $db->exec($searchSql);
59
    }
60
61
    protected function isDatabaseConfigured()
62
    {
63
        $db = $this->getSearchDbHandle();
64
        $stmt = $db->query('SELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'term_count\';');
65
        $result = $stmt->fetchAll();
66
        return !empty($result);
67
    }
68
69
    protected function getSearchDbHandle()
70
    {
71
        if ($this->searchDbHandle === null) {
72
            $path = $this->storageDir . DIRECTORY_SEPARATOR;
73
            $this->searchDbHandle = new \PDO('sqlite:' . $path . 'search.db');
74
        }
75
        return $this->searchDbHandle;
76
    }
77
}