RequestFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 42.86%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 12
cts 28
cp 0.4286
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B createFromGlobals() 0 18 5
B createRequest() 0 24 4
1
<?php
2
namespace Wandu\Http\Factory;
3
4
use Psr\Http\Message\StreamInterface;
5
use Wandu\Http\Factory\Exception\CannotCreateRequestException;
6
use Wandu\Http\Psr\Request;
7
use Wandu\Http\Psr\Stream;
8
9
class RequestFactory
10
{
11
    use HelperTrait;
12
13
    /**
14
     * @param array $serverParams
15
     * @return \Psr\Http\Message\RequestInterface
16
     */
17
    public function createFromGlobals(array $serverParams = null)
0 ignored issues
show
Coding Style introduced by
createFromGlobals uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
18
    {
19
        if (!isset($serverParams)) {
20
            $serverParams = $_SERVER;
21
        }
22
23
        $method = isset($serverParams['REQUEST_METHOD']) ? $serverParams['REQUEST_METHOD'] : 'GET';
24
        $uri = isset($serverParams['REQUEST_URI']) ? $serverParams['REQUEST_URI'] : '/';
25
26
        $headers = $this->getPsrHeadersFromServerParams($serverParams);
27
        return new Request(
28
            $method,
29
            $this->makeUriObjectWithPsrHeaders($uri, $headers),
30
            isset($stream) ? $stream : new Stream('php://memory'),
0 ignored issues
show
Bug introduced by
The variable $stream seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
31
            $headers,
32
            '1.1'
33
        );
34
    }
35
36
    /**
37
     * @param array $plainHeaders
38
     * @param \Psr\Http\Message\StreamInterface|null $stream
39
     * @return \Psr\Http\Message\RequestInterface
40
     * @throws \Wandu\Http\Factory\Exception\CannotCreateRequestException
41
     */
42 2
    public function createRequest(array $plainHeaders, StreamInterface $stream = null)
43
    {
44 2
        if (count($plainHeaders) === 0) {
45
            throw new CannotCreateRequestException(
46
                'first parameter is array, and it must have at least one value.',
47
                100
48
            );
49
        }
50
51 2
        $httpInformation = explode(' ', array_shift($plainHeaders));
52 2
        if (count($httpInformation) !== 3) {
53
            throw new CannotCreateRequestException('wrong http header.', 101);
54
        }
55 2
        list ($method, $uri, $serverProtocol) = $httpInformation;
56
57 2
        $headers = $this->getPsrHeadersFromPlainHeader($plainHeaders);
58 2
        return new Request(
59 2
            $method,
60 2
            $this->makeUriObjectWithPsrHeaders($uri, $headers),
61 2
            isset($stream) ? $stream : new Stream('php://memory'),
62 2
            $headers,
63 2
            explode('/', $serverProtocol)[1]
64
        );
65
    }
66
}
67