ServerRequestFactory::create()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 22
cts 22
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 21
nc 2
nop 1
crap 2
1
<?php
2
namespace Kambo\Http\Message\Factories\Environment;
3
4
// \Http\Message
5
use Kambo\Http\Message\ServerRequest;
6
use Kambo\Http\Message\Stream;
7
8
// \Http\Message\Environment
9
use Kambo\Http\Message\Environment\Environment;
10
11
// \Http\Message\Factories
12
use Kambo\Http\Message\Factories\Environment\Interfaces\FactoryInterface;
13
use Kambo\Http\Message\Factories\Environment\Superglobal\FilesFactory;
14
use Kambo\Http\Message\Factories\Environment\Superglobal\HeadersFactory;
15
use Kambo\Http\Message\Factories\Environment\Superglobal\UriFactory;
16
17
/**
18
 * Create instance of ServerRequest object from instance of Environment object
19
 *
20
 * @package Kambo\Http\Message\Factories\Environment
21
 * @author  Bohuslav Simek <[email protected]>
22
 * @license MIT
23
 */
24
class ServerRequestFactory implements FactoryInterface
25
{
26
    /**
27
     * Create instance of ServerRequest object from instance of Environment object
28
     *
29
     * @param Environment $environment environment data
30
     *
31
     * @return ServerRequest Instance of ServerRequest object
32
     */
33 3
    public function create(Environment $environment)
34
    {
35 3
        $uri         = (new UriFactory())->create($environment);
36 3
        $uploadFiles = (new FilesFactory())->create($environment);
37 3
        $headers     = (new HeadersFactory())->create($environment);
38
39 3
        $cookies       = $environment->getCookies();
40 3
        $requestMethod = $environment->getRequestMethod();
41 3
        $bodyStream    = new Stream($environment->getBody());
42 3
        $protocol      = $environment->getProtocolVersion();
43
44 3
        $serverVariables = $environment->getServer();
45
46 3
        $serverRequest = new ServerRequest(
47 3
            $requestMethod,
48 3
            $uri,
49 3
            $bodyStream,
50 3
            $headers,
51 3
            $serverVariables,
52 3
            $cookies,
53 3
            $uploadFiles,
54
            $protocol
55 3
        );
56
57
        // php://input is not available with enctype="multipart/form-data".
58 3
        if ($this->usePostAsParsed($requestMethod, $serverRequest)) {
59 2
            $serverRequest = $serverRequest->withParsedBody($environment->getPost());
60 2
        }
61
62 3
        return $serverRequest;
63
    }
64
65
    /**
66
     * Check if the body request should be taken from the $_POST super global
67
     * php://input is not available with enctype="multipart/form-data". POST
68
     * data are also used if the content type is "application/x-www-form-urlencoded"
69
     * for performance reason.
70
     *
71
     * @param string        $requestMethod Request method GET, POST etc.
72
     * @param ServerRequest $serverRequest Instance of server request
73
     *
74
     * @return Boolean True if the post data should be used as a parsed body
75
     */
76 3
    private function usePostAsParsed($requestMethod, ServerRequest $serverRequest)
77
    {
78 3
        $contentType = '';
79 3
        if ($serverRequest->hasHeader('Content-Type')) {
80 2
            $contentType = $serverRequest->getHeader('Content-Type')[0];
81 2
        }
82
83
        return ($requestMethod === 'POST'
84 3
            && in_array($contentType, ['application/x-www-form-urlencoded', 'multipart/form-data']));
85
    }
86
}
87