Completed
Push — master ( 69f641...8fff44 )
by Richard
05:35
created

Engine::read_index_from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 * 
5
 * Copyright (c) 2016 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 Lechimp\Dicto\Indexer\IndexerFactory;
14
use Lechimp\Dicto\Analysis\AnalyzerFactory;
15
use Lechimp\Dicto\Analysis\Index;
16
use Lechimp\Dicto\Indexer\Insert;
17
use Lechimp\Dicto\Graph;
18
use Psr\Log\LoggerInterface as Log;
19
20
/**
21
 * The Engine of the App drives the analysis process.
22
 */
23
class Engine {
24
    /**
25
     * @var Log
26
     */
27
    protected $log;
28
29
    /**
30
     * @var Config
31
     */
32
    protected $config;
33
34
    /**
35
     * @var DBFactory
36
     */
37
    protected $db_factory;
38
39
    /**
40
     * @var IndexerFactory
41
     */
42
    protected $indexer_factory;
43
44
    /**
45
     * @var AnalyzerFactory
46
     */
47
    protected $analyzer_factory;
48
49
    /**
50
     * @var SourceStatus
51
     */
52
    protected $source_status;
53
54 5
    public function __construct( Log $log
55
                               , Config $config
56
                               , DBFactory $db_factory
57
                               , IndexerFactory $indexer_factory
58
                               , AnalyzerFactory $analyzer_factory
59
                               , SourceStatus $source_status
60
                               ) {
61 5
        $this->log = $log;
62 5
        $this->config = $config;
63 5
        $this->db_factory = $db_factory;
64 5
        $this->indexer_factory = $indexer_factory;
65 5
        $this->analyzer_factory = $analyzer_factory;
66 5
        $this->source_status = $source_status;
67 5
    }
68
69
    /**
70
     * Run the analysis.
71
     * 
72
     * @return null
73
     */
74 4
    public function run() {
75 4
        $index_db_path = $this->index_database_path();
76 4
        if (!$this->db_factory->index_db_exists($index_db_path)) {
77 3
            $index = $this->build_index();
78 3
            $this->run_indexing($index);
79 3
            $index_db = $this->db_factory->build_index_db($index_db_path);
80 3
            $this->log->notice("Writing index to database '$index_db_path'...");
81 3
            $this->write_index_to($index, $index_db);
82 3
        }
83
        else {
84 1
            $index_db = $this->db_factory->load_index_db($index_db_path);
85 1
            $this->log->notice("Reading index from database '$index_db_path'...");
86 1
            $index = $this->read_index_from($index_db);
87
        }
88 4
        $this->run_analysis($index);
89 4
    }
90
91 4
    protected function index_database_path() {
92 4
        $commit_hash = $this->source_status->commit_hash();
93 4
        return $this->config->project_storage()."/$commit_hash.sqlite";
94
    }
95
96 4
    protected function result_database_path() {
97 4
        return $this->config->project_storage()."/results.sqlite";
98
    }
99
100 3
    protected function run_indexing(Insert $index) {
101 3
        $this->log->notice("Starting to build index...");
102 3
        $indexer = $this->indexer_factory->build($index);
103 3
        $indexer->index_directory
104 3
            ( $this->config->project_root()
105 3
            , $this->config->analysis_ignore()
106 3
            );
107 3
    }
108
109 4
    protected function run_analysis(Index $index) {
110 4
        $this->log->notice("Running analysis...");
111 4
        $commit_hash = $this->source_status->commit_hash();
112 4
        $result_db = $this->db_factory->get_result_db($this->result_database_path());
113 4
        $result_db->begin_new_run($commit_hash);
114 4
        $analyzer = $this->analyzer_factory->build($index, $result_db);
115 4
        $analyzer->run();
116 4
    }
117
118 3
    protected function build_index() {
119 3
        return new Graph\IndexDB;
120
    }
121
122
    protected function write_index_to(Graph\IndexDB $index, IndexDB $db) {
123
        $db->write_index($index);
124
    }
125
126
    protected function read_index_from(IndexDB $db) {
127
        return $db->read_index();
128
    }
129
}
130