DefaultHeaderParser::getContentType()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 1
crap 4
1
<?php
2
namespace LunixREST\RequestFactory\HeaderParser;
3
4
use LunixREST\RequestFactory\HeaderParser\Exceptions\UnableToParseHeadersException;
5
6
/**
7
 * Parse out all the common headers from a request.
8
 * Class DefaultHeaderParser
9
 * @package LunixREST\RequestFactory\HeaderParser
10
 */
11
class DefaultHeaderParser implements HeaderParser
12
{
13
14
    /**
15
     * @var string
16
     */
17
    private $apiKeyHeaderKey;
18
19
    /**
20
     * DefaultHeaderParser constructor.
21
     * @param string $apiKeyHeaderKey
22
     */
23 7
    public function __construct($apiKeyHeaderKey = 'x-api-key')
24
    {
25 7
        $this->apiKeyHeaderKey = $apiKeyHeaderKey;
26 7
    }
27
28
    /**
29
     * @param array $headers
30
     * @return ParsedHeaders
31
     * @throws UnableToParseHeadersException
32
     */
33 6
    public function parse(array $headers): ParsedHeaders
34
    {
35 6
        $contentType = $this->getContentType($headers);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $contentType is correct as $this->getContentType($headers) (which targets LunixREST\RequestFactory...arser::getContentType()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
36 6
        $acceptableMIMETypes = $this->findAcceptableMIMETypes($headers);
37 6
        $apiKey = $this->findAPIKey($headers);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $apiKey is correct as $this->findAPIKey($headers) (which targets LunixREST\RequestFactory...derParser::findAPIKey()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
38
39 6
        return new ParsedHeaders($contentType, $acceptableMIMETypes, $apiKey);
40
    }
41
42
    /**
43
     * @param array $headers
44
     * @return array
45
     */
46 6
    protected function findAcceptableMIMETypes(array $headers): array
47
    {
48
        //TODO: follow RFC2616 order
49 6
        $acceptedMIMETypes = [];
50
51 6
        foreach ($headers as $key => $values) {
52 6
            if (strtolower($key) == 'http-accept') {
53 1
                foreach ($values as $acceptedType) {
54 1
                    $typeParts = explode(';', $acceptedType);
55 1
                    if (count($typeParts) > 0) {
56 1
                        $acceptedMIMETypes[] = trim($typeParts[0]);
57
                    }
58
                }
59 1
                break;
60
            }
61
        }
62 6
        return $acceptedMIMETypes;
63
    }
64
65
    /**
66
     * @param array $headers
67
     * @return null
68
     */
69 6 View Code Duplication
    protected function findAPIKey(array $headers)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
70
    {
71 6
        foreach ($headers as $key => $values) {
72 6
            if (strtolower($key) == strtolower($this->apiKeyHeaderKey)) {
73 3
                foreach($values as $value) {
74 3
                    return $value;
75
                }
76
            }
77
        }
78 3
        return null;
79
    }
80
81
    /**
82
     * @param array $headers
83
     * @return null
84
     */
85 6 View Code Duplication
    protected function getContentType(array $headers)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
86
    {
87 6
        foreach ($headers as $key => $values) {
88 6
            if (strtolower($key) == 'content-type') {
89 2
                foreach ($values as $value) {
90 2
                    return $value;
91
                }
92
            }
93
        }
94 4
        return null;
95
    }
96
}
97