Completed
Push — master ( 774eb2...689ba3 )
by Richard
07:00
created

Engine::run()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 7
Bugs 0 Features 1
Metric Value
c 7
b 0
f 1
dl 0
loc 19
ccs 17
cts 17
cp 1
rs 9.4285
cc 3
eloc 14
nc 1
nop 0
crap 3
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
22
/**
23
 * The Engine of the App drives the analysis process.
24
 */
25
class Engine {
26
    /**
27
     * @var Config
28
     */
29
    protected $config;
30
31
    /**
32
     * @var Indexer
33
     */
34
    protected $indexer;
35
36
    /**
37
     * @var Analyzer
38
     */
39
    protected $analyzer;
40
41 3
    public function __construct(Config $config, Indexer $indexer, Analyzer $analyzer) {
42 3
        $this->config = $config;
43 3
        $this->indexer = $indexer;
44 3
        $this->analyzer = $analyzer;
45 3
    }
46
47
    /**
48
     * Run the analysis.
49
     * 
50
     * @return null
51
     */
52 3
    public function run() {
53 3
        $fc = $this->init_flightcontrol();
54 3
        $fc->directory("/")
55 3
            ->recurseOn()
56
            ->filter(function(FSObject $obj) {
57 3
                foreach ($this->config->analysis_ignore() as $pattern) {
58 3
                    if (preg_match("%$pattern%", $obj->path()) !== 0) {
59 3
                        return false;
60
                    }
61 3
                }
62 3
                return true;
63 3
            })
64 3
            ->foldFiles(null, function($_, File $file) {
65 3
                echo "indexing: ".$file->path()."\n";
66 3
                $this->indexer->index_file($file->path());
67 3
            });
68 3
        echo "\n\n\n";
69 3
        $this->analyzer->run();
70 3
    }
71
72
    /**
73
     * Initialize the filesystem abstraction.
74
     *
75
     * @return  Flightcontrol
76
     */
77 3
    public function init_flightcontrol() {
78 3
        $adapter = new Local($this->config->project_root(), LOCK_EX, Local::SKIP_LINKS);
79 3
        $flysystem = new Filesystem($adapter);
80 3
        return new Flightcontrol($flysystem);
81
    }
82
}
83