Completed
Push — master ( f9ded0...b13d5b )
by John
02:58 queued 01:00
created

GenericRequestFactory::create()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 26
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\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 5
    public function __construct(URLParser $URLParser, HeaderParser $headerParser)
32
    {
33 5
        $this->URLParser = $URLParser;
34 5
        $this->headerParser = $headerParser;
35 5
    }
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 2
        $parsedHeaders = $this->headerParser->parse($serverRequest->getHeaders());
48
49 2
        $urlQueryData = [];
50 2
        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...
51
        {
52 2
            parse_str($parsedURL->getQueryString(), $urlQueryData);
53
        }
54
55 2
        $apiKey = $parsedURL->getAPIKey();
56 2
        if($apiKey === null)
57
        {
58 1
            $apiKey = $parsedHeaders->getAPIKey();
59
        }
60
61 2
        $acceptableMIMETypes = array_unique(array_merge(
62 2
            $parsedURL->getAcceptableMIMETypes(),
63 2
            $parsedHeaders->getAcceptableMIMETypes()
64
        ));
65
66 2
        return new APIRequest($serverRequest->getMethod(), $parsedURL->getEndpoint(), $parsedURL->getElement(),
67 2
            $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...
68
    }
69
70
    /**
71
     * @return URLParser
72
     */
73 3
    public function getURLParser(): URLParser
74
    {
75 3
        return $this->URLParser;
76
    }
77
78
    /**
79
     * @return HeaderParser
80
     */
81 3
    public function getHeaderParser(): HeaderParser
82
    {
83 3
        return $this->headerParser;
84
    }
85
}
86