Completed
Branch master (73f336)
by John
01:59
created

GenericRequestFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 5
1
<?php
2
namespace LunixREST\Request\RequestFactory;
3
4
use LunixREST\Request\BodyParser\BodyParser;
5
use LunixREST\Request\Request;
6
use LunixREST\Request\URLParser\Exceptions\InvalidRequestURLException;
7
use LunixREST\Request\URLParser\URLParser;
8
9
class GenericRequestFactory implements RequestFactory {
10
11
    /**
12
     * @var URLParser
13
     */
14
    protected $URLParser;
15
    /**
16
     * @var BodyParser
17
     */
18
    private $bodyParser;
19
20
    /**
21
     * BasicRequestFactory constructor.
22
     * @param URLParser $URLParser
23
     * @param BodyParser $bodyParser
24
     */
25
    public function __construct(URLParser $URLParser, BodyParser $bodyParser) {
26
        $this->URLParser = $URLParser;
27
        $this->bodyParser = $bodyParser;
28
    }
29
30
    /**
31
     * Creates a request from raw $data and a $url
32
     * @param $method
33
     * @param array $headers
34
     * @param string $data
35
     * @param $ip
36
     * @param $url
37
     * @return Request
38
     * @throws InvalidRequestURLException
39
     */
40
    public function create($method, array $headers, string $data, $ip, $url): Request {
41
        $parsedURL = $this->URLParser->parse($url);
42
        $parsedData = $this->bodyParser->parse($data);
43
44
        return new Request($method, $headers, $parsedData, $parsedURL->getRequestData(), $ip, $parsedURL->getVersion(),
45
            $parsedURL->getApiKey(), $parsedURL->getEndpoint(), $parsedURL->getExtension(), $parsedURL->getInstance());
46
    }
47
}
48