Completed
Push — master ( 1e9ef5...3740bc )
by Richard
05:53 queued 29s
created

Engine   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 90.2%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 11
dl 0
loc 113
ccs 46
cts 51
cp 0.902
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A run() 0 19 3
A index_database_path() 0 4 1
A run_indexing() 0 8 1
A run_analysis() 0 8 1
A build_index() 0 3 1
A write_index_to() 0 3 1
A read_index_from() 0 3 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("Starting to build index...");
109 7
        $indexer = $this->indexer_factory->build($index);
110 7
        $indexer->index_directory
111 7
            ( $this->config->project_root()
112 7
            , $this->config->analysis_ignore()
113 7
            );
114 7
    }
115
116 8
    protected function run_analysis(Index $index) {
117 8
        $this->log->notice("Running analysis...");
118 8
        $commit_hash = $this->source_status->commit_hash();
119 8
        $this->report_generator->begin_run($commit_hash);
120 8
        $analyzer = $this->analyzer_factory->build($index, $this->report_generator);
121 8
        $analyzer->run();
122 8
        $this->report_generator->end_run();
123 8
    }
124
125 7
    protected function build_index() {
126 7
        return new Graph\IndexDB;
127
    }
128
129
    protected function write_index_to(Graph\IndexDB $index, IndexDB $db) {
130
        $db->write_index($index);
131
    }
132
133
    protected function read_index_from(IndexDB $db) {
134
        return $db->read_index();
135
    }
136
}
137