Completed
Push — master ( 2a510c...5bfa5e )
by Richard
03:56
created

IndexDBFactory::build_index_db()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.9765

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 8
cp 0.375
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2.9765
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 *
5
 * Copyright (c) 2016, 2015 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received
8
 * a copy of the license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\DB;
12
13
use Lechimp\Dicto\Report\ResultDB;
14
15
class IndexDBFactory {
16
    /**
17
     * Create a new database for index at path.
18
     *
19
     * @param   string  $path
20
     * @throws  \RuntimeException   if database already exists.
21
     * @return  IndexDB
22
     */
23 1
    public function build_index_db($path) {
24 1
        if (file_exists($path)) {
25 1
            throw new \RuntimeException("File at '$path' already exists, can't build database.");
26
        }
27
        $connection = $this->build_connection($path);
0 ignored issues
show
Bug introduced by
The method build_connection() does not seem to exist on object<Lechimp\Dicto\DB\IndexDBFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
        $db = new IndexDB($connection);
29
        $db->init_sqlite_regexp();
30
        $db->init_database_schema();
31
        return $db;
32
    }
33
34
    /**
35
     * Check if an index database exists.
36
     *
37
     * @param   string  $path
38
     * @return  bool
39
     */
40 4
    public function index_db_exists($path) {
41 4
        return file_exists($path);
42
    }
43
44
    /**
45
     * Load existing index database. 
46
     *
47
     * @param   string  $path
48
     * @throws  \RuntimeException   if file does not exist
49
     * @return  IndexDB 
50
     */
51 2
    public function load_index_db($path) {
52 2
        if (!$this->index_db_exists($path)) {
53 1
            throw new \RuntimeException("There is no index database at '$path'");
54
        }
55 1
        $connection = DB::sqlite_connection($path);
56 1
        $db = new IndexDB($connection);
57 1
        $db->init_sqlite_regexp();
58 1
        return $db;
59
    }
60
}
61