Completed
Push — master ( 005b00...a8a0a1 )
by John
01:59
created

GenericRequestFactory::getBodyParserFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace LunixREST\Request\RequestFactory;
3
4
use LunixREST\Request\BodyParser\BodyParserFactory\BodyParserFactory;
5
use LunixREST\Request\BodyParser\BodyParserFactory\Exceptions\UnknownContentTypeException;
6
use LunixREST\Request\BodyParser\Exceptions\InvalidRequestDataException;
7
use LunixREST\Request\HeaderParser\HeaderParser;
8
use LunixREST\Request\Request;
9
use LunixREST\Request\URLParser\Exceptions\InvalidRequestURLException;
10
use LunixREST\Request\URLParser\URLParser;
11
12
/**
13
 * A generic Request Factory that derives it's behavior from a URLParser, a BodyParserFactory, and a HeaderParser.
14
 * Class GenericRequestFactory
15
 * @package LunixREST\Request\RequestFactory
16
 */
17
class GenericRequestFactory implements RequestFactory {
18
19
    /**
20
     * @var URLParser
21
     */
22
    protected $URLParser;
23
    /**
24
     * @var BodyParserFactory
25
     */
26
    private $bodyParserFactory;
27
    /**
28
     * @var HeaderParser
29
     */
30
    private $headerParser;
31
32
    /**
33
     * BasicRequestFactory constructor.
34
     * @param URLParser $URLParser
35
     * @param BodyParserFactory $bodyParserFactory
36
     * @param HeaderParser $headerParser
37
     */
38 5
    public function __construct(URLParser $URLParser, BodyParserFactory $bodyParserFactory, HeaderParser $headerParser) {
39 5
        $this->URLParser = $URLParser;
40 5
        $this->bodyParserFactory = $bodyParserFactory;
41 5
        $this->headerParser = $headerParser;
42 5
    }
43
44
    /**
45
     * Creates a request from raw $data and a $url
46
     * @param $method
47
     * @param array $headers
48
     * @param string $data
49
     * @param $ip
50
     * @param $url
51
     * @return Request
52
     * @throws InvalidRequestURLException
53
     * @throws UnknownContentTypeException
54
     * @throws InvalidRequestDataException
55
     */
56 2
    public function create($method, array $headers, string $data, $ip, $url): Request {
57 2
        $parsedURL = $this->URLParser->parse($url);
58
59 2
        $parsedHeaders = $this->headerParser->parse($headers);
60
61 2
        $bodyParser = $this->bodyParserFactory->create($parsedHeaders->getContentType());
62 2
        $parsedData = $bodyParser->parse($data);
63
64 2
        $apiKey = $parsedURL->getAPIKey();
65 2
        if($apiKey === null) {
66 1
            $apiKey = $parsedHeaders->getAPIKey();
67
        }
68
69 2
        $acceptableMIMETypes = array_unique(array_merge(
70 2
            $parsedURL->getAcceptableMIMETypes(),
71 2
            $parsedHeaders->getAcceptableMIMETypes()
72
        ));
73
74 2
        return new Request($method, $headers, $parsedData, $parsedURL->getRequestData(), $ip, $parsedURL->getVersion(),
75 2
            $apiKey, $parsedURL->getEndpoint(), $acceptableMIMETypes, $parsedURL->getInstance());
76
    }
77
78
    /**
79
     * @return URLParser
80
     */
81 3
    public function getURLParser(): URLParser {
82 3
        return $this->URLParser;
83
    }
84
85
    /**
86
     * @return BodyParserFactory
87
     */
88 3
    public function getBodyParserFactory(): BodyParserFactory {
89 3
        return $this->bodyParserFactory;
90
    }
91
92
    /**
93
     * @return HeaderParser
94
     */
95 3
    public function getHeaderParser(): HeaderParser {
96 3
        return $this->headerParser;
97
    }
98
}
99