Completed
Push — master ( 9b2a73...93cace )
by Richard
05:06
created

Engine::with_time_measurement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
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\ReportGenerator;
15
use Lechimp\Dicto\Analysis\AnalyzerFactory;
16
use Lechimp\Dicto\Analysis\Index;
17
use Lechimp\Dicto\Indexer\Insert;
18
use Lechimp\Dicto\Graph;
19
use Psr\Log\LoggerInterface as Log;
20
21
/**
22
 * The Engine of the App drives the analysis process.
23
 */
24
class Engine {
25
    /**
26
     * @var Log
27
     */
28
    protected $log;
29
30
    /**
31
     * @var Config
32
     */
33
    protected $config;
34
35
    /**
36
     * @var DBFactory
37
     */
38
    protected $db_factory;
39
40
    /**
41
     * @var IndexerFactory
42
     */
43
    protected $indexer_factory;
44
45
    /**
46
     * @var AnalyzerFactory
47
     */
48
    protected $analyzer_factory;
49
50
    /**
51
     * @var ReportGenerator
52
     */
53
    protected $report_generator;
54
55
    /**
56
     * @var SourceStatus
57
     */
58
    protected $source_status;
59
60 9
    public function __construct( Log $log
61
                               , Config $config
62
                               , DBFactory $db_factory
63
                               , IndexerFactory $indexer_factory
64
                               , AnalyzerFactory $analyzer_factory
65
                               , ReportGenerator $report_generator
66
                               , SourceStatus $source_status
67
                               ) {
68 9
        $this->log = $log;
69 9
        $this->config = $config;
70 9
        $this->db_factory = $db_factory;
71 9
        $this->indexer_factory = $indexer_factory;
72 9
        $this->analyzer_factory = $analyzer_factory;
73 9
        $this->report_generator = $report_generator;
74 9
        $this->source_status = $source_status;
75 9
    }
76
77
    /**
78
     * Run the analysis.
79
     * 
80
     * @return null
81
     */
82 8
    public function run() {
83 8
        $index_db_path = $this->index_database_path();
84 8
        if (!$this->db_factory->index_db_exists($index_db_path)) {
85 7
            $index = $this->build_index();
86 7
            $this->run_indexing($index);
87
88 7
            if ($this->config->analysis_store_index()) {
89 1
                $index_db = $this->db_factory->build_index_db($index_db_path);
90 1
                $this->log->notice("Writing index to database '$index_db_path'...");
91 1
                $this->write_index_to($index, $index_db);
92 1
            }
93 7
        }
94
        else {
95 1
            $index_db = $this->db_factory->load_index_db($index_db_path);
96 1
            $this->log->notice("Reading index from database '$index_db_path'...");
97 1
            $index = $this->read_index_from($index_db);
98
        }
99 8
        $this->run_analysis($index);
100 8
    }
101
102 8
    protected function index_database_path() {
103 8
        $commit_hash = $this->source_status->commit_hash();
104 8
        return $this->config->project_storage()."/$commit_hash.sqlite";
105
    }
106
107 7
    protected function run_indexing(Insert $index) {
108 7
        $this->log->notice("Building index...");
109 7
        $indexer = $this->indexer_factory->build($index);
110 7
        $this->with_time_measurement
111
            ( function ($s) { return "Indexing took $s seconds to run."; }
112
            , function () use ($indexer) {
113 7
                $indexer->index_directory
114 7
                    ( $this->config->project_root()
115 7
                    , $this->config->analysis_ignore()
116 7
                    );
117 7
            });
118 7
    }
119
120 8
    protected function run_analysis(Index $index) {
121 8
        $this->log->notice("Running analysis...");
122 8
        $commit_hash = $this->source_status->commit_hash();
123 8
        $this->report_generator->begin_run($commit_hash);
124 8
        $analyzer = $this->analyzer_factory->build($index, $this->report_generator);
125 8
        $this->with_time_measurement
126
            ( function ($s) { return "Analysis took $s seconds to run."; }
127 8
            , function () use ($analyzer) {
128 8
                $analyzer->run();
129 8
            });
130 8
        $this->report_generator->end_run();
131 8
    }
132
133 7
    protected function build_index() {
134 7
        return new Graph\IndexDB;
135
    }
136
137
    protected function write_index_to(Graph\IndexDB $index, IndexDB $db) {
138
        $db->write_index($index);
139
    }
140
141
    protected function read_index_from(IndexDB $db) {
142
        return $db->read_index();
143
    }
144
145 8
    protected function with_time_measurement(\Closure $message, \Closure $what) {
146 8
        $start_time = microtime(true);
147 8
        $what();
148 8
        $time_elapsed_secs = microtime(true) - $start_time;
149 8
        $this->log->notice($message($time_elapsed_secs));
150 8
    }
151
}
152