FileLinesMetric::getLineCount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Startwind\Inventorio\Metrics\Collector\Metric;
4
5
use Startwind\Inventorio\Exec\File;
6
use Startwind\Inventorio\Exec\Runner;
7
use Startwind\Inventorio\Metrics\Memory\Memory;
8
9
abstract class FileLinesMetric implements Metric
10
{
11
    protected string $filename = '';
12
    protected string $name = '';
13
14
    public function getName(): string
15
    {
16
        return $this->name;
17
    }
18
19
    public function isApplicable(): bool
20
    {
21
        return File::getInstance()->fileExists($this->filename);
22
    }
23
24
    public function getValue(float $lastValue): float
25
    {
26
        $lastLineCount = Memory::getInstance()->getLastData($this->getName() . '_absolute', -1);
27
        $lineCount = $this->getLineCount($this->filename);
28
        Memory::getInstance()->addData($this->getName() . '_absolute', $lineCount);
29
30
        // this is the first run we return 0 to skip this value
31
        if ($lastLineCount < 0) {
32
            return 0;
33
        }
34
35
        // we assume that there was a log rotation done here
36
        if ($lineCount < $lastLineCount) {
37
            return $lineCount;
38
        }
39
40
        return $lineCount - $lastLineCount;
41
    }
42
43
    protected function getLineCount($path): int
44
    {
45
        $count = Runner::getInstance()->run('wc -l ' . $path)->getOutput();
46
        return (int)$count;
47
    }
48
}