Completed
Push — master ( 6ee7c4...eb4679 )
by John
02:32
created

GenericRequestFactory::create()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 14
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 4
nop 1
crap 12
1
<?php
2
namespace LunixREST\APIRequest\RequestFactory;
3
4
use LunixREST\APIRequest\HeaderParser\HeaderParser;
5
use LunixREST\APIRequest\APIRequest;
6
use LunixREST\APIRequest\URLParser\Exceptions\InvalidRequestURLException;
7
use LunixREST\APIRequest\URLParser\URLParser;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
/**
11
 * A generic Request Factory that derives it's behavior from a URLParser, a BodyParserFactory, and a HeaderParser.
12
 * Class GenericRequestFactory
13
 * @package LunixREST\Request\RequestFactory
14
 */
15
class GenericRequestFactory implements RequestFactory {
16
17
    /**
18
     * @var URLParser
19
     */
20
    protected $URLParser;
21
    /**
22
     * @var HeaderParser
23
     */
24
    private $headerParser;
25
26
    /**
27
     * BasicRequestFactory constructor.
28
     * @param URLParser $URLParser
29
     * @param HeaderParser $headerParser
30
     */
31 2
    public function __construct(URLParser $URLParser, HeaderParser $headerParser)
32
    {
33 2
        $this->URLParser = $URLParser;
34 2
        $this->headerParser = $headerParser;
35 2
    }
36
37
    /**
38
     * Creates a request from raw $data and a $url
39
     * @param ServerRequestInterface $serverRequest
40
     * @return APIRequest
41
     * @throws InvalidRequestURLException
42
     */
43
    public function create(ServerRequestInterface $serverRequest): APIRequest
44
    {
45
        $parsedURL = $this->URLParser->parse($serverRequest->getUri());
46
47
        //TODO: Evauluate if this is still needed, as serverRequest allows getting of specific headers
48
        $parsedHeaders = $this->headerParser->parse($serverRequest->getHeaders());
49
50
        $urlQueryData = [];
51
        if($urlQueryString = $parsedURL->getQueryString())
0 ignored issues
show
Unused Code introduced by
$urlQueryString is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
        {
53
            parse_str($parsedURL->getQueryString(), $urlQueryData);
54
        }
55
56
        $apiKey = $parsedURL->getAPIKey();
57
        if($apiKey === null)
58
        {
59
            $apiKey = $parsedHeaders->getAPIKey();
60
        }
61
62
        $acceptableMIMETypes = array_unique(array_merge(
63
            $parsedURL->getAcceptableMIMETypes(),
64
            $parsedHeaders->getAcceptableMIMETypes()
65
        ));
66
67
        return new APIRequest($serverRequest->getMethod(), $parsedURL->getEndpoint(), $parsedURL->getElement(),
68
            $acceptableMIMETypes, $parsedURL->getVersion(), $apiKey, $urlQueryData, $serverRequest->getParsedBody());
1 ignored issue
show
Bug introduced by
It seems like $urlQueryData can also be of type null; however, LunixREST\APIRequest\APIRequest::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69
    }
70
71
    /**
72
     * @return URLParser
73
     */
74 2
    public function getURLParser(): URLParser
75
    {
76 2
        return $this->URLParser;
77
    }
78
79
    /**
80
     * @return HeaderParser
81
     */
82 2
    public function getHeaderParser(): HeaderParser
83
    {
84 2
        return $this->headerParser;
85
    }
86
}
87