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

SearchDbConnected   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 8
loc 60
rs 10
c 1
b 0
f 0
wmc 7
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A initializeDb() 0 6 2
A configureDatabase() 0 7 1
A isDatabaseConfigured() 0 7 1
A getSearchDbHandle() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
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...
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
}