Passed
Push — master ( b886e0...d77012 )
by Nils
02:35
created

NumberOfLinesCheck::getCheckIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Leankoala\HealthFoundation\Check\Files\Content;
4
5
use Leankoala\HealthFoundation\Check\BasicCheck;
6
use Leankoala\HealthFoundation\Check\Result;
7
8
class NumberOfLinesCheck extends BasicCheck
9
{
10
    protected $identifier = 'base:files:content:numberOfLines';
11
12
    private $file;
13
14
    private $relation;
15
16
    private $limit;
17
18
    const RELATION_MAX = 'max';
19
    const RELATION_MIN = 'min';
20
21
    private $pattern;
22
23
    public function run()
24
    {
25
        if (!file_exists($this->file)) {
26
            return new Result(Result::STATUS_FAIL, 'Unable to get document length because file does not exist.');
27
        }
28
29
        $grep = '';
30
        if ($this->pattern) {
31
            foreach ($this->pattern as $pattern) {
32
                $grep .= ' | grep "' . $pattern . '"';
33
            }
34
        }
35
36
        $command = 'cat ' . $this->file . $grep . ' | wc -l';
37
38
        exec($command, $output, $return);
39
40
        $numberLines = (int)$output[0];
41
42
        if ($this->relation === self::RELATION_MAX) {
43
            if ($numberLines > $this->limit) {
44
                return new Result(Result::STATUS_FAIL, 'The document contains too many lines (' . $numberLines . '). Expected where ' . $this->limit . ' at the most.');
45
            } else {
46
                return new Result(Result::STATUS_PASS, 'The document contains ' . $numberLines . ' lines. Expected where ' . $this->limit . ' at the most.');
47
            }
48
        } else {
49
            if ($numberLines < $this->limit) {
50
                return new Result(Result::STATUS_FAIL, 'The document contains too few lines (' . $numberLines . '). Expected where ' . $this->limit . ' at least.');
51
            } else {
52
                return new Result(Result::STATUS_PASS, 'The document contains ' . $numberLines . ' lines. Expected where ' . $this->limit . ' at least.');
53
            }
54
        }
55
    }
56
57
    public function init($file, $limit, $relation = self::RELATION_MAX, $pattern = null)
58
    {
59
        $this->file = $file;
60
        $this->pattern = (array)$pattern;
61
        $this->relation = $relation;
62
        $this->limit = $limit;
63
    }
64
65
    protected function getCheckIdentifier()
66
    {
67
        return $this->identifier . '.' . md5($this->file . serialize($this->pattern));
68
    }
69
}
70