AbstractStorageBuilder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
buildFetcher() 0 1 ?
A selectParseModeAllUpfront() 0 4 1
A selectParseModeDetailOnlyUpfront() 0 4 1
A build() 0 32 4
A andGetStorageAfterTheBuild() 0 4 1
A isParseModeAllSelected() 0 4 1
A isParseModeDetailOnly() 0 4 1
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