Completed
Push — master ( a94ac3...322d21 )
by Richard
05:34
created

Engine::run_indexing()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 22
ccs 17
cts 17
cp 1
rs 8.9197
cc 4
eloc 15
nc 1
nop 0
crap 4
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 licence along with the code.
9
 */
10
11
namespace Lechimp\Dicto\App;
12
13
use Lechimp\Dicto\Indexer\Indexer;
14
use Lechimp\Dicto\Analysis\Analyzer;
15
use League\Flysystem\Adapter\Local;
16
use League\Flysystem\Filesystem;
17
use Lechimp\Flightcontrol\Flightcontrol;
18
use Lechimp\Flightcontrol\File;
19
use Lechimp\Flightcontrol\FSObject;
20
use Doctrine\DBAL\DriverManager;
21
use Psr\Log\LoggerInterface as Log;
22
23
/**
24
 * The Engine of the App drives the analysis process.
25
 */
26
class Engine {
27
    /**
28
     * @var Log
29
     */
30
    protected $log;
31
32
    /**
33
     * @var Config
34
     */
35
    protected $config;
36
37
    /**
38
     * @var Indexer
39
     */
40
    protected $indexer;
41
42
    /**
43
     * @var Analyzer
44
     */
45
    protected $analyzer;
46
47 5
    public function __construct(Log $log, Config $config, Indexer $indexer, Analyzer $analyzer) {
48 5
        $this->log = $log;
49 5
        $this->config = $config;
50 5
        $this->indexer = $indexer;
51 5
        $this->analyzer = $analyzer;
52 5
    }
53
54
    /**
55
     * Run the analysis.
56
     * 
57
     * @return null
58
     */
59 5
    public function run() {
60 5
        $this->run_indexing();
61 5
        $this->run_analysis();
62 5
    }
63
64 5
    protected function run_indexing() {
65 5
        $fc = $this->init_flightcontrol();
66 5
        $fc->directory("/")
67 5
            ->recurseOn()
68
            ->filter(function(FSObject $obj) {
69 5
                foreach ($this->config->analysis_ignore() as $pattern) {
70 5
                    if (preg_match("%$pattern%", $obj->path()) !== 0) {
71 5
                        return false;
72
                    }
73 5
                }
74 5
                return true;
75 5
            })
76 5
            ->foldFiles(null, function($_, File $file) {
77 5
                $this->log->info("indexing: ".$file->path());
78
                try {            
79 5
                    $this->indexer->index_file($file->path());
80
                }
81 5
                catch (\PhpParser\Error $e) {
82 1
                    $this->log->error("in ".$file->path().": ".$e->getMessage());
83
                }
84 5
            });
85 5
    }
86
87 5
    protected function run_analysis() {
88 5
        $this->analyzer->run();
89 5
    }
90
91
    /**
92
     * Initialize the filesystem abstraction.
93
     *
94
     * @return  Flightcontrol
95
     */
96 5
    public function init_flightcontrol() {
97 5
        $adapter = new Local($this->config->project_root(), LOCK_EX, Local::SKIP_LINKS);
98 5
        $flysystem = new Filesystem($adapter);
99 5
        return new Flightcontrol($flysystem);
100
    }
101
}
102