RequestFactory::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 2
eloc 11
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Wolnosciowiec\WebProxy\Factory;
4
5
use GuzzleHttp\Psr7\ServerRequest;
6
use Zend\Diactoros\ServerRequestFactory;
7
use Zend\Diactoros\Uri;
8
9
/**
10
 * Builds the request to the destination server
11
 * --------------------------------------------
12
 *
13
 * @package Wolnosciowiec\WebProxy\Factory
14
 */
15
class RequestFactory
16
{
17
    /**
18
     * @param string $destinationUrl URL address we want to call and retrieve data from
19
     *
20
     * @throws \Exception
21
     * @return ServerRequest
22
     */
23
    public function create(string $destinationUrl)
24
    {
25
        // create a PSR7 request based on the current browser request.
26
        $request     = ServerRequestFactory::fromGlobals();
27
        $currentHost = $request->getUri()->getHost();
28
29
        $requestedUrl = new Uri($destinationUrl);
30
        $requestedUrl = $requestedUrl->withPath('');
31
32
        /** @var ServerRequest $request */
33
        $request = $request->withUri($requestedUrl);
34
35
        if ($currentHost === $request->getUri()->getHost()) { // @codeCoverageIgnore
36
            throw new \Exception('Cannot make a request to the same host as we are'); // @codeCoverageIgnore
37
        }
38
39
        // do the clean up before passing through the request
40
        $request = $request->withoutHeader('ww-target-url');
41
        $request = $request->withoutHeader('ww-token');
42
43
        return $request;
44
    }
45
}
46