Completed
Push — master ( 69e974...6c5ab2 )
by Bohuslav
02:42
created

ServerRequestFactory::usePostAsParsed()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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