1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2017-03-30 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\ApacheServerStatusParser\Service\Content\Parser; |
8
|
|
|
|
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use JonasRudolph\PHPComponents\StringUtility\Implementation\StringUtility; |
11
|
|
|
use Net\Bazzline\Component\ApacheServerStatusParser\DomainModel\Information; |
12
|
|
|
|
13
|
|
|
class InformationListOfLineParser implements ListOfLineParserInterface |
14
|
|
|
{ |
15
|
|
|
/** @var StringUtility */ |
16
|
|
|
private $stringUtility; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* InformationListOfLineParser constructor. |
22
|
|
|
* |
23
|
|
|
* @param StringUtility $stringUtility |
24
|
|
|
*/ |
25
|
|
|
public function __construct( |
26
|
|
|
StringUtility $stringUtility |
27
|
|
|
) { |
28
|
|
|
$this->stringUtility = $stringUtility; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string[] $listOfLine |
33
|
|
|
* |
34
|
|
|
* @return Information |
35
|
|
|
* @throws InvalidArgumentException |
36
|
|
|
* @todo make it beautiful to look at |
37
|
|
|
*/ |
38
|
|
|
public function parse(array $listOfLine) |
39
|
|
|
{ |
40
|
|
|
//begin of dependencies |
41
|
|
|
$stringUtility = $this->stringUtility; |
42
|
|
|
//end of dependencies |
43
|
|
|
|
44
|
|
|
//begin of business logic |
45
|
|
|
$listOfLineHasMinimalSize = (count($listOfLine) > 3); |
46
|
|
|
|
47
|
|
|
if ($listOfLineHasMinimalSize) { |
48
|
|
|
$listOfMandatoryPropertyNameToStartsWithPrefix = [ |
49
|
|
|
'date_of_built' => 'Server Built: ', |
50
|
|
|
'identifier' => 'Apache Server Status for ', |
51
|
|
|
'mode_of_mpm' => 'Server MPM: ', |
52
|
|
|
'version' => 'Server Version: ' |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$listOMandatoryProperties = [ |
56
|
|
|
'date_of_built' => null, |
57
|
|
|
'identifier' => null, |
58
|
|
|
'mode_of_mpm' => null, |
59
|
|
|
'version' => null |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
foreach ($listOfLine as $line) { |
63
|
|
|
//stop repeating yourself, us the $listOfMandatoryPropertyNameToStartsWithPrefix |
64
|
|
|
//take a look to the StatisticListOfLineParser |
65
|
|
|
if ($stringUtility->startsWith($line, 'Apache Server Status for ')) { |
66
|
|
|
$listOMandatoryProperties['identifier'] = substr($line, 25); //always use numbers if you are dealing with static strings |
67
|
|
|
} else if ($stringUtility->startsWith($line, 'Server Version: ')) { |
68
|
|
|
$listOMandatoryProperties['version'] = substr($line, 16); |
69
|
|
|
} else if ($stringUtility->startsWith($line, 'Server MPM: ')) { |
70
|
|
|
$listOMandatoryProperties['mode_of_mpm'] = substr($line, 12); |
71
|
|
|
} else if ($stringUtility->startsWith($line, 'Server Built: ')) { |
72
|
|
|
$listOMandatoryProperties['date_of_built'] = substr($line, 14); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
foreach ($listOMandatoryProperties as $name => $mandatoryProperty) { |
|
|
|
|
77
|
|
|
if (is_null($mandatoryProperty)) { |
78
|
|
|
throw new InvalidArgumentException( |
79
|
|
|
'could not find mandatory property "' |
80
|
|
|
. $listOfMandatoryPropertyNameToStartsWithPrefix[$name] |
81
|
|
|
. '"' |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$information = new Information( |
87
|
|
|
$listOMandatoryProperties['date_of_built'], |
88
|
|
|
$listOMandatoryProperties['identifier'], |
89
|
|
|
$listOMandatoryProperties['mode_of_mpm'], |
90
|
|
|
$listOMandatoryProperties['version'] |
91
|
|
|
); |
92
|
|
|
} else { |
93
|
|
|
throw new InvalidArgumentException( |
94
|
|
|
self::class . ' can not parse given list of line "' |
95
|
|
|
. implode(',', $listOfLine) . '"' |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $information; |
100
|
|
|
//end of business logic |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.