DetailLineParser::parse()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 58
rs 8.6052
c 0
b 0
f 0

How to fix   Long Method   

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-03-26
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\Detail;
12
13
class DetailLineParser implements LineParserInterface
14
{
15
    /** @var StringUtility */
16
    private $stringUtility;
17
18
    /**
19
     * DetailLineParserTest constructor.
20
     *
21
     * @param StringUtility $stringUtility
22
     */
23
    public function __construct(
24
        StringUtility $stringUtility
25
    ) {
26
        $this->stringUtility = $stringUtility;
27
    }
28
29
    /**
30
     * @param string $line
31
     * @return Detail
32
     * @throws InvalidArgumentException
33
     */
34
    public function parse($line)
35
    {
36
        //begin of dependencies
37
        $stringUtility = $this->stringUtility;
38
        //end of dependencies
39
40
        //begin of business logic
41
        $lineAsArray = explode(' ', $line);
42
43
        $arrayIsInvalid = (count($lineAsArray) < 19);
44
45
        if ($arrayIsInvalid) {
46
            throw new InvalidArgumentException(
47
                self::class . ' can not parse given line "' . $line . '"'
48
            );
49
        }
50
51
        $httpMethod = (
52
            $stringUtility->startsWith($lineAsArray[16], '{')
53
                ? substr($lineAsArray[16], 1)
54
                : $lineAsArray[16]
55
        );
56
        $pid = filter_var(
57
                $lineAsArray[2],
58
                FILTER_SANITIZE_NUMBER_INT
59
            );
60
        $status = str_replace(
61
                [
62
                    '[',
63
                    ']'
64
                ],
65
                '',
66
                $lineAsArray[4]
67
            );
68
        $uriAuthority = str_replace(
69
                [
70
                    '[',
71
                    ']'
72
                ],
73
                '',
74
                (isset($lineAsArray[19]) ? $lineAsArray[19] : $lineAsArray[18])
75
            );
76
        $uriPathWithQuery = (
77
            $stringUtility->endsWith($lineAsArray[17], '}')
78
                ? substr($lineAsArray[17], 0, -1)
79
                : $lineAsArray[17]
80
        );
81
82
        return new Detail(
83
            $httpMethod,
84
            $lineAsArray[15],
85
            $pid,
86
            $status,
87
            $uriAuthority,
88
            $uriPathWithQuery
89
        );
90
        //end of business logic
91
    }
92
}
93