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

SearchDbConnected::initializeDb()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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\JsonStorage;
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 resource
22
	 */
23
	protected $searchDbHandle;
24
25
	/**
26
	 * @var \library\storage\JsonStorage
27
	 */
28
	protected $storage;
29
30
	/**
31
	 * Indexer constructor.
32
	 *
33
	 * @param \library\storage\JsonStorage $storage
34
	 */
35
	public function __construct(JsonStorage $storage)
36
	{
37
		$this->storageDir = $storage->getStorageDir();
0 ignored issues
show
Bug introduced by
The property storageDir does not seem to exist. Did you mean storage?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
38
		$this->storage = $storage;
39
		$this->initializeDb();
40
	}
41
42
	protected function initializeDb()
43
	{
44
		if (!$this->isDatabaseConfigured()) {
45
			$this->configureDatabase();
46
		}
47
	}
48
49
	protected function configureDatabase()
50
	{
51
		$db = $this->getSearchDbHandle();
52
		$sqlPath = __DIR__ . DIRECTORY_SEPARATOR . '../cc/install/search.sql';
53
		$searchSql = file_get_contents($sqlPath);
54
		$db->exec($searchSql);
55
	}
56
57
	protected function isDatabaseConfigured()
58
	{
59
		$db = $this->getSearchDbHandle();
60
		$stmt = $db->query('SELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'term_count\';');
61
		$result = $stmt->fetchAll();
62
		return !empty($result);
63
	}
64
65 View Code Duplication
	protected function getSearchDbHandle()
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...
66
	{
67
		if ($this->searchDbHandle === null) {
68
			$path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $this->storageDir . DIRECTORY_SEPARATOR;
0 ignored issues
show
Bug introduced by
The property storageDir does not seem to exist. Did you mean storage?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
69
			$this->searchDbHandle = new \PDO('sqlite:' . $path . 'search.db');
0 ignored issues
show
Documentation Bug introduced by
It seems like new \PDO('sqlite:' . $path . 'search.db') of type object<PDO> is incompatible with the declared type resource of property $searchDbHandle.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
		}
71
		return $this->searchDbHandle;
72
	}
73
}