|
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
|
|
|
$numberLines = $this->getNumberOfLines(); |
|
30
|
|
|
|
|
31
|
|
|
return $this->processData($numberLines); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return int |
|
36
|
|
|
*/ |
|
37
|
|
|
private function getNumberOfLines() |
|
38
|
|
|
{ |
|
39
|
|
|
$grep = ''; |
|
40
|
|
|
if ($this->pattern) { |
|
41
|
|
|
foreach ($this->pattern as $pattern) { |
|
42
|
|
|
$grep .= ' | grep -a "' . $pattern . '"'; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$command = 'cat ' . $this->file . $grep . ' | wc -l'; |
|
47
|
|
|
|
|
48
|
|
|
exec($command, $output, $return); |
|
49
|
|
|
|
|
50
|
|
|
return (int)$output[0]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $numberLines |
|
55
|
|
|
* @return Result |
|
56
|
|
|
*/ |
|
57
|
|
|
private function processData($numberLines) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->relation === self::RELATION_MAX) { |
|
60
|
|
|
if ($numberLines > $this->limit) { |
|
61
|
|
|
return new Result(Result::STATUS_FAIL, 'The document contains too many lines (' . $numberLines . '). Expected where ' . $this->limit . ' at the most.'); |
|
62
|
|
|
} else { |
|
63
|
|
|
return new Result(Result::STATUS_PASS, 'The document contains ' . $numberLines . ' lines. Expected where ' . $this->limit . ' at the most.'); |
|
64
|
|
|
} |
|
65
|
|
|
} else { |
|
66
|
|
|
if ($numberLines < $this->limit) { |
|
67
|
|
|
return new Result(Result::STATUS_FAIL, 'The document contains too few lines (' . $numberLines . '). Expected where ' . $this->limit . ' at least.'); |
|
68
|
|
|
} else { |
|
69
|
|
|
return new Result(Result::STATUS_PASS, 'The document contains ' . $numberLines . ' lines. Expected where ' . $this->limit . ' at least.'); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function init($file, $limit, $relation = self::RELATION_MAX, $pattern = null) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->file = $file; |
|
77
|
|
|
$this->pattern = (array)$pattern; |
|
78
|
|
|
$this->relation = $relation; |
|
79
|
|
|
$this->limit = $limit; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function getCheckIdentifier() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->identifier . '.' . md5($this->file . serialize($this->pattern)); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|