1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: jensk |
4
|
|
|
* Date: 21-2-2017 |
5
|
|
|
* Time: 17:05 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace library\search; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use library\storage\Storage; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Abstract Class SearchDbConnected |
15
|
|
|
* Handles connection with the search index database |
16
|
|
|
* @package library\search |
17
|
|
|
*/ |
18
|
|
|
abstract class SearchDbConnected |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \PDO |
22
|
|
|
*/ |
23
|
|
|
protected $searchDbHandle; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \library\storage\Storage |
27
|
|
|
*/ |
28
|
|
|
protected $storage; |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $storageDir; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Indexer constructor. |
36
|
|
|
* |
37
|
|
|
* @param \library\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
|
|
View Code Duplication |
protected function getSearchDbHandle() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
if ($this->searchDbHandle === null) { |
72
|
|
|
$path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $this->storageDir . DIRECTORY_SEPARATOR; |
73
|
|
|
$this->searchDbHandle = new \PDO('sqlite:' . $path . 'search.db'); |
74
|
|
|
} |
75
|
|
|
return $this->searchDbHandle; |
76
|
|
|
} |
77
|
|
|
} |
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.