Passed
Push — master ( 0e59e1...fddcd5 )
by Nils
02:37
created

FileLinesMetric   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 31
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLineCount() 0 4 1
A getName() 0 3 1
A isApplicable() 0 3 1
A getValue() 0 10 2
1
<?php
2
3
namespace Startwind\Inventorio\Metrics\Collector\Metric;
4
5
use Startwind\Inventorio\Exec\File;
6
use Startwind\Inventorio\Exec\Runner;
7
8
abstract class FileLinesMetric implements Metric
9
{
10
    protected string $filename = '';
11
    protected string $name = '';
12
13
    public function getName(): string
14
    {
15
        return $this->name;
16
    }
17
18
    public function isApplicable(): bool
19
    {
20
        return File::getInstance()->fileExists($this->filename);
21
    }
22
23
    public function getValue(float $lastValue): float
24
    {
25
        $lineCount = $this->getLineCount($this->filename);
26
27
        // we assume that there was a log rotation done here
28
        if ($lineCount < $lastValue) {
29
            return $lineCount;
30
        }
31
32
        return $lineCount - $lastValue;
33
    }
34
35
    protected function getLineCount($path): int
36
    {
37
        $count = Runner::getInstance()->run('wc -l ' . $path);
38
        return (int)$count;
39
    }
40
}