Completed
Push — master ( 0a059e...292010 )
by John
02:21
created

GenericRequestFactory::getURLParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace LunixREST\RequestFactory;
3
4
use LunixREST\RequestFactory\HeaderParser\HeaderParser;
5
use LunixREST\RequestFactory\URLParser\Exceptions\InvalidRequestURLException;
6
use LunixREST\RequestFactory\URLParser\URLParser;
7
use LunixREST\Server\APIRequest\APIRequest;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
/**
11
 * A generic Request Factory that derives it's behavior from a URLParser and a HeaderParser. Uses the requests Parsed Body.
12
 * Class GenericRequestFactory
13
 * @package LunixREST\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 4
    public function __construct(URLParser $URLParser, HeaderParser $headerParser)
32
    {
33 4
        $this->URLParser = $URLParser;
34 4
        $this->headerParser = $headerParser;
35 4
    }
36
37
    /**
38
     * Creates a request from raw $data and a $url
39
     * @param ServerRequestInterface $serverRequest
40
     * @return APIRequest
41
     * @throws InvalidRequestURLException
42
     */
43 2
    public function create(ServerRequestInterface $serverRequest): APIRequest
44
    {
45 2
        $parsedURL = $this->URLParser->parse($serverRequest->getUri());
46
47
        //TODO: Evaluate if this is still needed, as serverRequest allows getting of specific headers
48 2
        $parsedHeaders = $this->headerParser->parse($serverRequest->getHeaders());
49
50 2
        $urlQueryData = [];
51 2
        if($urlQueryString = $parsedURL->getQueryString()) {
52 2
            parse_str($urlQueryString, $urlQueryData);
53
        }
54
55 2
        $apiKey = $parsedURL->getAPIKey();
56 2
        if($apiKey === null) {
57 1
            $apiKey = $parsedHeaders->getAPIKey();
58
        }
59
60 2
        $acceptableMIMETypes = array_unique(array_merge(
61 2
            $parsedURL->getAcceptableMIMETypes(),
62 2
            $parsedHeaders->getAcceptableMIMETypes()
63
        ));
64
65 2
        return new APIRequest($serverRequest->getMethod(), $parsedURL->getEndpoint(), $parsedURL->getElement(),
66 2
            $acceptableMIMETypes, $parsedURL->getVersion(), $apiKey, $urlQueryData, $serverRequest->getParsedBody());
0 ignored issues
show
Bug introduced by
It seems like $urlQueryData can also be of type null; however, LunixREST\Server\APIRequ...IRequest::__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...
67
    }
68
69
    /**
70
     * @return URLParser
71
     */
72 2
    public function getURLParser(): URLParser
73
    {
74 2
        return $this->URLParser;
75
    }
76
77
    /**
78
     * @return HeaderParser
79
     */
80 2
    public function getHeaderParser(): HeaderParser
81
    {
82 2
        return $this->headerParser;
83
    }
84
}
85