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
|
|
|
|