Completed
Push — master ( 29c7fd...0622b2 )
by Dev
53:37 queued 52:34
created

LogsAnalyzer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
eloc 27
dl 0
loc 60
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 4
B parse() 0 20 8
A filter() 0 7 2
A checkFile() 0 3 2
A setFilter() 0 3 1
1
<?php
2
3
namespace PiedWeb\LogsAnalyzer;
4
5
class LogsAnalyzer
6
{
7
    protected $parser;
8
9
    protected $filter;
10
11
    public $logs;
12
13
    public function __construct(string $type, ?string $domain = null)
14
    {
15
        $this->parser = new LogParser();
16
        $this->parser->setFormat(class_exists($type) ? $type::FORMAT : $type);
17
        if ($domain !== null) {
18
            $this->parser->setDomain($domain);
19
        }
20
        $this->parser->setType(class_exists($type) ? $type : LogLine::class);
21
    }
22
23
    /**
24
     * The function must return TRUE for line we want to keep / FALSE for line to ignore
25
     */
26
    public function setFilter($filter)
27
    {
28
        $this->filter = $filter;
29
    }
30
31
    protected function checkFile(string $filename): bool
32
    {
33
        return !file_exists($filename) || !is_readable($filename);
34
    }
35
36
    protected function filter($line)
37
    {
38
        if ($this->filter) {
39
            return call_user_func($this->filter, $line);
40
        }
41
42
        return true;
43
    }
44
45
    public function parse(string $filename)
46
    {
47
        if ($this->checkFile($filename)
48
            || $handle = fopen(('.gz' == substr($filename, -3) ? 'compress.zlib://' : '').$filename, 'r') === false) {
49
            throw new \Exception('A problem occured with file `'.$filename.'`');
50
        }
51
52
        $logs = [];
53
        $lineCounter = 1;
54
        while (!feof($handle)) {
0 ignored issues
show
Bug introduced by
$handle of type false is incompatible with the type resource expected by parameter $handle of feof(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        while (!feof(/** @scrutinizer ignore-type */ $handle)) {
Loading history...
55
            $line = fgets($handle);
0 ignored issues
show
Bug introduced by
$handle of type false is incompatible with the type resource expected by parameter $handle of fgets(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            $line = fgets(/** @scrutinizer ignore-type */ $handle);
Loading history...
56
            $line = $line ? $this->parser->parse($line) : null;
57
            if ($line && $this->filter($line)) {
58
                $logs[$lineCounter] = $line;
59
            }
60
            ++$lineCounter;
61
        }
62
        fclose($handle);
0 ignored issues
show
Bug introduced by
$handle of type false is incompatible with the type resource expected by parameter $handle of fclose(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
63
64
        return $logs;
65
    }
66
}
67