1 | <?php |
||
25 | class Engine { |
||
26 | /** |
||
27 | * @var Log |
||
28 | */ |
||
29 | protected $log; |
||
30 | |||
31 | /** |
||
32 | * @var Config |
||
33 | */ |
||
34 | protected $config; |
||
35 | |||
36 | /** |
||
37 | * @var DBFactory |
||
38 | */ |
||
39 | protected $db_factory; |
||
40 | |||
41 | /** |
||
42 | * @var IndexerFactory |
||
43 | */ |
||
44 | protected $indexer_factory; |
||
45 | |||
46 | /** |
||
47 | * @var AnalyzerFactory |
||
48 | */ |
||
49 | protected $analyzer_factory; |
||
50 | |||
51 | /** |
||
52 | * @var ReportGenerator |
||
53 | */ |
||
54 | protected $report_generator; |
||
55 | |||
56 | /** |
||
57 | * @var SourceStatus |
||
58 | */ |
||
59 | protected $source_status; |
||
60 | |||
61 | 9 | public function __construct( Log $log |
|
62 | , Config $config |
||
63 | , DBFactory $db_factory |
||
64 | , IndexerFactory $indexer_factory |
||
65 | , AnalyzerFactory $analyzer_factory |
||
66 | , ReportGenerator $report_generator |
||
67 | , SourceStatus $source_status |
||
68 | ) { |
||
69 | 9 | $this->log = $log; |
|
70 | 9 | $this->config = $config; |
|
71 | 9 | $this->db_factory = $db_factory; |
|
72 | 9 | $this->indexer_factory = $indexer_factory; |
|
73 | 9 | $this->analyzer_factory = $analyzer_factory; |
|
74 | 9 | $this->report_generator = $report_generator; |
|
75 | 9 | $this->source_status = $source_status; |
|
76 | 9 | } |
|
77 | |||
78 | /** |
||
79 | * Run the analysis. |
||
80 | * |
||
81 | * @return null |
||
82 | */ |
||
83 | 8 | public function run() { |
|
84 | 8 | $index_db_path = $this->index_database_path(); |
|
85 | 8 | if (!$this->db_factory->index_db_exists($index_db_path)) { |
|
86 | 7 | $index = $this->build_index(); |
|
87 | 7 | $this->run_indexing($index); |
|
88 | 7 | if ($index instanceof InsertTwice) { |
|
89 | 1 | $index_db = $this->force_app_index_db($index->second()); |
|
90 | 1 | $index_db->write_cached_inserts(); |
|
91 | 1 | $index = $this->force_graph_index_db($index->first()); |
|
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() { |
|
106 | |||
107 | 7 | protected function run_indexing(Insert $index) { |
|
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 | , 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 | if ($this->config->analysis_store_index()) { |
|
135 | 1 | $index_db_path = $this->index_database_path(); |
|
136 | 1 | $index_db = $this->db_factory->build_index_db($index_db_path); |
|
137 | 1 | $this->log->notice("Writing index to database '$index_db_path'..."); |
|
138 | 1 | return new InsertTwice(new Graph\IndexDB, $index_db); |
|
139 | } |
||
140 | |||
141 | 6 | return new Graph\IndexDB; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return Graph\IndexDB |
||
146 | */ |
||
147 | protected function read_index_from(IndexDB $db) { |
||
156 | |||
157 | 8 | protected function with_time_measurement(\Closure $message, \Closure $what) { |
|
163 | |||
164 | /** |
||
165 | * @param mixed $index |
||
166 | * @return Graph\IndexDB |
||
167 | */ |
||
168 | 1 | protected function force_graph_index_db($index) { |
|
178 | |||
179 | /** |
||
180 | * @param mixed $index |
||
181 | * @return IndexDB |
||
182 | */ |
||
183 | 1 | protected function force_app_index_db($index) { |
|
193 | } |
||
194 |