StatisticListOfLineParser::parse()   D
last analyzed

Complexity

Conditions 15
Paths 37

Size

Total Lines 147

Duplication

Lines 9
Ratio 6.12 %

Importance

Changes 0
Metric Value
cc 15
nc 37
nop 1
dl 9
loc 147
rs 4.7333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2017-04-04
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\Statistic;
12
13
class StatisticListOfLineParser implements ListOfLineParserInterface
14
{
15
    /** @var StringUtility */
16
    private $stringUtility;
17
18
    public function __construct(
19
        StringUtility $stringUtility
20
    ) {
21
        $this->stringUtility = $stringUtility;
22
    }
23
24
    /**
25
     * @param string[] $listOfLine
26
     *
27
     * @return Statistic
28
     * @throws InvalidArgumentException
29
     */
30
    public function parse(array $listOfLine)
31
    {
32
        //begin of dependencies
33
        $stringUtility = $this->stringUtility;
34
        //end of dependencies
35
36
        //begin of business logic
37
        $listOfLineHasMinimalSize = (count($listOfLine) > 9);
38
39
        if ($listOfLineHasMinimalSize) {
40
            $listOfMandatoryPropertyNameToStartsWithPrefix = [
41
                'current_timestamp'                         => 'Current Time: ',
42
                'cpu_usage'                                 => 'CPU Usage: ',
43
                'parent_server_configuration_generation'    => 'Parent Server Config. Generation: ',
44
                'parent_server_mpm_generation'              => 'Parent Server MPM Generation: ',
45
                'restart_time'                              => 'Restart Time: ',
46
                'server_up_time'                            => 'Server uptime:  ',
47
                'server_load'                               => 'Server load: ',
48
                'total_accesses'                            => 'Total accesses: '
49
            ];
50
            /**
51
             * always use numbers if you are dealing with static strings
52
             * <prefix >: string length
53
                Current Time: : 14
54
                CPU Usage: : 11
55
                Parent Server Config. Generation: : 34
56
                Parent Server MPM Generation: : 30
57
                Restart Time: : 14
58
                Server uptime:  : 16
59
                Server load: : 13
60
                Total accesses: : 16
61
                Total Traffic: : 15
62
             */
63
            $listOMandatoryProperties = [
64
                'b_per_seconds'                             => null,
65
                'current_time'                              => null,
66
                'cpu_load'                                  => null,
67
                'cpu_usage'                                 => null,
68
                'idle_workers'                              => null,
69
                'kb_per_seconds'                            => null,
70
                'parent_server_configuration_generation'    => null,
71
                'parent_server_mpm_generation'              => null,
72
                'request_per_seconds'                       => null,
73
                'request_currently_being_processed'         => null,
74
                'restart_time'                              => null,
75
                'server_up_time'                            => null,
76
                'server_load'                               => null,
77
                'total_accesses'                            => null,
78
                'total_traffic'                             => null
79
            ];
80
81
            foreach ($listOfLine as $line) {
82
                if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['current_timestamp'])) {
83
                    $listOMandatoryProperties['current_time'] = strtotime(
84
                        substr($line, 14)
85
                    );
86
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['cpu_usage'])) {
87
                    $lineAsArray = explode(' - ', $line);
88
89
                    $listOMandatoryProperties['cpu_load'] = filter_var(
90
                        $lineAsArray[1],
91
                        FILTER_SANITIZE_NUMBER_INT
92
                    );
93
                    $listOMandatoryProperties['cpu_usage'] = substr($lineAsArray[0], 11);
94
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['parent_server_configuration_generation'])) {
95
                    $listOMandatoryProperties['parent_server_configuration_generation'] = substr($line, 34);
96
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['parent_server_mpm_generation'])) {
97
                    $listOMandatoryProperties['parent_server_mpm_generation'] = substr($line, 30);
98
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['restart_time'])) {
99
                    $listOMandatoryProperties['restart_time'] = strtotime(
100
                        substr($line, 14)
101
                    );
102
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['server_up_time'])) {
103
                    $listOMandatoryProperties['server_up_time'] = substr($line, 16);
104
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['server_load'])) {
105
                    $listOMandatoryProperties['server_load'] = substr($line, 13);
106
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['total_accesses'])) {
107
                    $lineAsArray = explode(' - ', $line);
108
109
                    $listOMandatoryProperties['total_accesses'] = substr($lineAsArray[0], 16);
110
                    $listOMandatoryProperties['total_traffic']  = substr($lineAsArray[1], 15);
111
                } else if ($stringUtility->endsWith($line, 'request')) {
112
                    $lineAsArray = explode(' - ', $line);
113
114
                    $listOMandatoryProperties['b_per_seconds'] = filter_var(
115
                        $lineAsArray[1],
116
                        FILTER_SANITIZE_NUMBER_INT
117
                    );
118
                    $listOMandatoryProperties['kb_per_seconds'] = filter_var(
119
                        $lineAsArray[2],
120
                        FILTER_SANITIZE_NUMBER_INT
121
                    );
122
                    $listOMandatoryProperties['request_per_seconds'] = filter_var(
123
                        $lineAsArray[0],
124
                        FILTER_SANITIZE_NUMBER_INT
125
                    );
126
                } else if ($stringUtility->endsWith($line, 'workers')) {
127
                    $lineAsArray = explode(',', $line);
128
129
                    $listOMandatoryProperties['idle_workers'] = filter_var(
130
                        $lineAsArray[1],
131
                        FILTER_SANITIZE_NUMBER_INT
132
                    );
133
                    $listOMandatoryProperties['request_currently_being_processed'] = filter_var(
134
                        $lineAsArray[0],
135
                        FILTER_SANITIZE_NUMBER_INT
136
                    );
137
                }
138
            }
139
140 View Code Duplication
            foreach ($listOMandatoryProperties as $name => $mandatoryProperty) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
141
                if (is_null($mandatoryProperty)) {
142
                    throw new InvalidArgumentException(
143
                        'could not find mandatory property "'
144
                        . $listOfMandatoryPropertyNameToStartsWithPrefix[$name]
145
                        . '"'
146
                    );
147
                }
148
            }
149
150
            $statistic = new Statistic(
151
                $listOMandatoryProperties['b_per_seconds'],
152
                $listOMandatoryProperties['cpu_load'],
153
                $listOMandatoryProperties['cpu_usage'],
154
                $listOMandatoryProperties['current_time'],
155
                $listOMandatoryProperties['idle_workers'],
156
                $listOMandatoryProperties['kb_per_seconds'],
157
                $listOMandatoryProperties['parent_server_configuration_generation'],
158
                $listOMandatoryProperties['parent_server_configuration_generation'],
159
                $listOMandatoryProperties['request_currently_being_processed'],
160
                $listOMandatoryProperties['request_per_seconds'],
161
                $listOMandatoryProperties['restart_time'],
162
                $listOMandatoryProperties['server_load'],
163
                $listOMandatoryProperties['server_up_time'],
164
                $listOMandatoryProperties['total_accesses'],
165
                $listOMandatoryProperties['total_traffic']
166
            );
167
        } else {
168
            throw new InvalidArgumentException(
169
                self::class . ' can not parse given list of line "'
170
                . implode(',', $listOfLine) . '"'
171
            );
172
        }
173
174
        return $statistic;
175
        //end of business logic
176
    }
177
}
178