Completed
Push — master ( da6adc...c42001 )
by arto
02:03
created

Content/Parser/StatisticListOfLineParser.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 DateTime;
10
use InvalidArgumentException;
11
use JonasRudolph\PHPComponents\StringUtility\Implementation\StringUtility;
12
use Net\Bazzline\Component\ApacheServerStatusParser\DomainModel\Statistic;
13
14
class StatisticListOfLineParser implements ListOfLineParserInterface
15
{
16
    /** @var StringUtility */
17
    private $stringUtility;
18
19
    public function __construct(
20
        StringUtility $stringUtility
21
    )
22
    {
23
        $this->stringUtility    = $stringUtility;
24
    }
25
26
    /**
27
     * @param string[] $listOfLine
28
     *
29
     * @return Statistic
30
     * @throws InvalidArgumentException
31
     */
32
    public function parse(array $listOfLine)
33
    {
34
        //begin of dependencies
35
        $stringUtility  = $this->stringUtility;
36
        //end of dependencies
37
38
        //begin of business logic
39
        $listOfLineHasMinimalSize = (count($listOfLine) > 9);
40
41
        if ($listOfLineHasMinimalSize) {
42
            $listOfMandatoryPropertyNameToStartsWithPrefix  = [
43
                'current_timestamp'                         => 'Current Time: ',
44
                'cpu_usage'                                 => 'CPU Usage: ',
45
                'parent_server_configuration_generation'    => 'Parent Server Config. Generation: ',
46
                'parent_server_mpm_generation'              => 'Parent Server MPM Generation: ',
47
                'restart_time'                              => 'Restart Time: ',
48
                'server_up_time'                            => 'Server uptime:  ',
49
                'server_load'                               => 'Server load: ',
50
                'total_accesses'                            => 'Total accesses: '
51
            ];
52
            /**
53
             * always use numbers if you are dealing with static strings
54
             * <prefix >: string length
55
                Current Time: : 14
56
                CPU Usage: : 11
57
                Parent Server Config. Generation: : 34
58
                Parent Server MPM Generation: : 30
59
                Restart Time: : 14
60
                Server uptime:  : 16
61
                Server load: : 13
62
                Total accesses: : 16
63
                Total Traffic: : 15
64
             */
65
66
            $listOMandatoryProperties   = [
67
                'b_per_seconds'                             => null,
68
                'current_time'                              => null,
69
                'cpu_load'                                  => null,
70
                'cpu_usage'                                 => null,
71
                'idle_workers'                              => null,
72
                'kb_per_seconds'                            => null,
73
                'parent_server_configuration_generation'    => null,
74
                'parent_server_mpm_generation'              => null,
75
                'request_per_seconds'                       => null,
76
                'request_currently_being_processed'         => null,
77
                'restart_time'                              => null,
78
                'server_up_time'                            => null,
79
                'server_load'                               => null,
80
                'total_accesses'                            => null,
81
                'total_traffic'                             => null
82
            ];
83
84
            foreach ($listOfLine as $line) {
85
                if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['current_timestamp'])) {
86
                    $listOMandatoryProperties['current_time'] = strtotime(
87
                        substr($line, 14)
88
                    );
89
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['cpu_usage'])) {
90
                    $lineAsArray    = explode(' - ', $line);
91
92
                    $listOMandatoryProperties['cpu_load']   = filter_var(
93
                        $lineAsArray[1],
94
                        FILTER_SANITIZE_NUMBER_INT
95
                    );
96
                    $listOMandatoryProperties['cpu_usage']  = substr($lineAsArray[0], 11);
97
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['parent_server_configuration_generation'])) {
98
                    $listOMandatoryProperties['parent_server_configuration_generation'] = substr($line, 34);
99
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['parent_server_mpm_generation'])) {
100
                    $listOMandatoryProperties['parent_server_mpm_generation'] = substr($line, 30);
101
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['restart_time'])) {
102
                    $listOMandatoryProperties['restart_time'] = strtotime(
103
                        substr($line, 14)
104
                    );
105
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['server_up_time'])) {
106
                    $listOMandatoryProperties['server_up_time'] = substr($line, 16);
107
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['server_load'])) {
108
                    $listOMandatoryProperties['server_load'] = substr($line, 13);
109
                } else if ($stringUtility->startsWith($line, $listOfMandatoryPropertyNameToStartsWithPrefix['total_accesses'])) {
110
                    $lineAsArray    = explode(' - ', $line);
111
112
                    $listOMandatoryProperties['total_accesses'] = substr($lineAsArray[0], 16);
113
                    $listOMandatoryProperties['total_traffic']  = substr($lineAsArray[1], 15);
114
                } else if ($stringUtility->endsWith($line, 'request')) {
115
                    $lineAsArray    = explode(' - ', $line);
116
117
                    $listOMandatoryProperties['b_per_seconds']          = filter_var(
118
                        $lineAsArray[1],
119
                        FILTER_SANITIZE_NUMBER_INT
120
                    );
121
                    $listOMandatoryProperties['kb_per_seconds']         = filter_var(
122
                        $lineAsArray[2],
123
                        FILTER_SANITIZE_NUMBER_INT
124
                    );
125
                    $listOMandatoryProperties['request_per_seconds']    = filter_var(
126
                        $lineAsArray[0],
127
                        FILTER_SANITIZE_NUMBER_INT
128
                    );
129
                } else if ($stringUtility->endsWith($line, 'workers')) {
130
                    $lineAsArray    = explode(',', $line);
131
132
                    $listOMandatoryProperties['idle_workers']                       = filter_var(
133
                        $lineAsArray[1],
134
                        FILTER_SANITIZE_NUMBER_INT
135
                    );
136
                    $listOMandatoryProperties['request_currently_being_processed']  = filter_var(
137
                        $lineAsArray[0],
138
                        FILTER_SANITIZE_NUMBER_INT
139
                    );
140
                }
141
            }
142
143 View Code Duplication
            foreach ($listOMandatoryProperties as $name => $mandatoryProperty) {
0 ignored issues
show
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...
144
                if (is_null($mandatoryProperty)) {
145
                    throw new InvalidArgumentException(
146
                        'could not find mandatory property "'
147
                        . $listOfMandatoryPropertyNameToStartsWithPrefix[$name]
148
                        . '"'
149
                    );
150
                }
151
            }
152
153
            $statistic = new Statistic(
154
                $listOMandatoryProperties['b_per_seconds'],
155
                $listOMandatoryProperties['cpu_load'],
156
                $listOMandatoryProperties['cpu_usage'],
157
                $listOMandatoryProperties['current_time'],
158
                $listOMandatoryProperties['idle_workers'],
159
                $listOMandatoryProperties['kb_per_seconds'],
160
                $listOMandatoryProperties['parent_server_configuration_generation'],
161
                $listOMandatoryProperties['parent_server_configuration_generation'],
162
                $listOMandatoryProperties['request_currently_being_processed'],
163
                $listOMandatoryProperties['request_per_seconds'],
164
                $listOMandatoryProperties['restart_time'],
165
                $listOMandatoryProperties['server_load'],
166
                $listOMandatoryProperties['server_up_time'],
167
                $listOMandatoryProperties['total_accesses'],
168
                $listOMandatoryProperties['total_traffic']
169
            );
170
        } else {
171
            throw new InvalidArgumentException(
172
                self::class . ' can not parse given list of line "'
173
                . implode(',', $listOfLine) . '"'
174
            );
175
        }
176
177
        return $statistic;
178
        //end of business logic
179
    }
180
}
181