Completed
Push — master ( f7852e...e666ea )
by Richard
06:08
created

DBFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 41.38%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 70
ccs 12
cts 29
cp 0.4138
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A build_index_db() 0 10 2
A index_db_exists() 0 3 1
A load_index_db() 0 9 2
A get_result_db() 0 6 1
A build_connection() 0 10 1
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\App;
12
13
use Doctrine\DBAL\DriverManager;
14
15
class DBFactory {
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
    public function build_index_db($path) {
24
        if (file_exists($path)) {
25
            throw new \RuntimeException("File at '$path' already exists, can't build database.");
26
        }
27
        $connection = $this->build_connection($path);
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
    public function index_db_exists($path) {
41
        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
    public function load_index_db($path) {
52
        if (!$this->index_db_exists($path)) {
53
            throw new \RuntimeException("There is no index database at '$path'");
54
        }
55
        $connection = $this->build_connection($path);
56
        $db = new IndexDB($connection);
57
        $db->init_sqlite_regexp();
58
        return $db;
59
    }
60
61
    /**
62
     * Get a database for results.
63
     *
64
     * @param   string  $path
65
     * @return  ResultDB
66
     */
67 4
    public function get_result_db($path) {
68 4
        $connection = $this->build_connection($path);
69 4
        $db = new ResultDB($connection);
70 4
        $db->maybe_init_database_schema();
71 4
        return $db;
72
    }
73
74 4
    protected function build_connection($path) {
75 4
        assert('is_string($path)');
76
        return DriverManager::getConnection
77 4
            ( array
78
                ( "driver" => "pdo_sqlite"
79 4
                , "memory" => false
80 4
                , "path" => $path
81 4
                )
82 4
            );
83
    }
84
}
85