GenericRequestFactory::create()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 4
nop 1
crap 3
1
<?php
2
namespace LunixREST\RequestFactory;
3
4
use LunixREST\RequestFactory\Exceptions\UnableToCreateRequestException;
5
use LunixREST\RequestFactory\HeaderParser\Exceptions\UnableToParseHeadersException;
6
use LunixREST\RequestFactory\HeaderParser\HeaderParser;
7
use LunixREST\RequestFactory\URLParser\Exceptions\UnableToParseURLException;
8
use LunixREST\RequestFactory\URLParser\URLParser;
9
use LunixREST\Server\APIRequest\APIRequest;
10
use Psr\Http\Message\ServerRequestInterface;
11
12
/**
13
 * A generic Request Factory that derives it's behavior from a URLParser and a HeaderParser. Uses the requests Parsed Body.
14
 * Class GenericRequestFactory
15
 * @package LunixREST\RequestFactory
16
 */
17
class GenericRequestFactory implements RequestFactory {
18
19
    /**
20
     * @var URLParser
21
     */
22
    protected $URLParser;
23
    /**
24
     * @var HeaderParser
25
     */
26
    private $headerParser;
27
28
    /**
29
     * BasicRequestFactory constructor.
30
     * @param URLParser $URLParser
31
     * @param HeaderParser $headerParser
32
     */
33 4
    public function __construct(URLParser $URLParser, HeaderParser $headerParser)
34
    {
35 4
        $this->URLParser = $URLParser;
36 4
        $this->headerParser = $headerParser;
37 4
    }
38
39
    /**
40
     * Creates a request from raw $data and a $url
41
     * @param ServerRequestInterface $serverRequest
42
     * @return APIRequest
43
     * @throws UnableToCreateRequestException
44
     * @throws UnableToParseHeadersException
45
     * @throws UnableToParseURLException
46
     */
47 2
    public function create(ServerRequestInterface $serverRequest): APIRequest
48
    {
49 2
        $parsedURL = $this->URLParser->parse($serverRequest->getUri());
50
51
        //TODO: Evaluate if this is still needed, as serverRequest allows getting of specific headers
52 2
        $parsedHeaders = $this->headerParser->parse($serverRequest->getHeaders());
53
54 2
        $urlQueryData = [];
55 2
        if($urlQueryString = $parsedURL->getQueryString()) {
56 2
            parse_str($urlQueryString, $urlQueryData);
57
        }
58
59 2
        $apiKey = $parsedURL->getAPIKey();
60 2
        if($apiKey === null) {
61 1
            $apiKey = $parsedHeaders->getAPIKey();
62
        }
63
64 2
        $acceptableMIMETypes = array_unique(array_merge(
65 2
            $parsedURL->getAcceptableMIMETypes(),
66 2
            $parsedHeaders->getAcceptableMIMETypes()
67
        ));
68
69 2
        return new APIRequest($serverRequest->getMethod(), $parsedURL->getEndpoint(), $parsedURL->getElement(),
70 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...
71
    }
72
73
    /**
74
     * @return URLParser
75
     */
76 2
    public function getURLParser(): URLParser
77
    {
78 2
        return $this->URLParser;
79
    }
80
81
    /**
82
     * @return HeaderParser
83
     */
84 2
    public function getHeaderParser(): HeaderParser
85
    {
86 2
        return $this->headerParser;
87
    }
88
}
89