|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author stev leibelt <[email protected]> |
|
4
|
|
|
* @since 2017-04-11 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\ApacheServerStatusParser\Service\Builder; |
|
8
|
|
|
|
|
9
|
|
|
use JonasRudolph\PHPComponents\StringUtility\Implementation\StringUtility; |
|
10
|
|
|
use Net\Bazzline\Component\ApacheServerStatusParser\Service\Content\Fetcher\FetcherInterface; |
|
11
|
|
|
use Net\Bazzline\Component\ApacheServerStatusParser\Service\Content\Processor\Processor; |
|
12
|
|
|
use Net\Bazzline\Component\ApacheServerStatusParser\Service\Content\Storage\DetailOnlyStorage; |
|
13
|
|
|
use Net\Bazzline\Component\ApacheServerStatusParser\Service\Content\Storage\FullStorage; |
|
14
|
|
|
use Net\Bazzline\Component\ApacheServerStatusParser\Service\Content\Storage\StorageInterface; |
|
15
|
|
|
use Net\Bazzline\Component\ApacheServerStatusParser\Service\StateMachine\SectionStateMachine; |
|
16
|
|
|
use RuntimeException; |
|
17
|
|
|
|
|
18
|
|
|
abstract class AbstractStorageBuilder implements BuilderInterface |
|
19
|
|
|
{ |
|
20
|
|
|
const PARSE_MODE_ALL = 'all'; |
|
21
|
|
|
const PARSE_MODE_DETAIL_ONLY = 'detail_only'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var Processor */ |
|
24
|
|
|
protected $processor; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected $selectedParseMode; |
|
28
|
|
|
|
|
29
|
|
|
public function selectParseModeAllUpfront() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->selectedParseMode = self::PARSE_MODE_ALL; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function selectParseModeDetailOnlyUpfront() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->selectedParseMode = self::PARSE_MODE_DETAIL_ONLY; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @throws RuntimeException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function build() |
|
43
|
|
|
{ |
|
44
|
|
|
//begin of dependencies |
|
45
|
|
|
$fetcher = $this->buildFetcher(); |
|
46
|
|
|
$stateMachine = new SectionStateMachine(); |
|
47
|
|
|
$stringUtility = new StringUtility(); |
|
48
|
|
|
//end of dependencies |
|
49
|
|
|
|
|
50
|
|
|
//begin of business logic |
|
51
|
|
|
if ($this->isParseModeAllSelected()) { |
|
52
|
|
|
$storage = new FullStorage($stringUtility); |
|
53
|
|
|
} else if ($this->isParseModeDetailOnly()) { |
|
54
|
|
|
$storage = new DetailOnlyStorage($stringUtility); |
|
55
|
|
|
} else { |
|
56
|
|
|
throw new RuntimeException( |
|
57
|
|
|
'no parse mode set' |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$processor = new Processor( |
|
62
|
|
|
$stateMachine, |
|
63
|
|
|
$stringUtility, |
|
64
|
|
|
$storage |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($fetcher->fetch() as $line) { |
|
68
|
|
|
$processor->process($line); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->processor = $processor; |
|
72
|
|
|
//end of business logic |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return StorageInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
public function andGetStorageAfterTheBuild() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->processor->getStorage(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return FetcherInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
abstract protected function buildFetcher(); |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function isParseModeAllSelected() |
|
92
|
|
|
{ |
|
93
|
|
|
return ($this->selectedParseMode === self::PARSE_MODE_ALL); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function isParseModeDetailOnly() |
|
100
|
|
|
{ |
|
101
|
|
|
return ($this->selectedParseMode === self::PARSE_MODE_DETAIL_ONLY); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|