1 | <?php |
||
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() |
||
95 | |||
96 | /** |
||
97 | * @return bool |
||
98 | */ |
||
99 | protected function isParseModeDetailOnly() |
||
103 | } |
||
104 |