HttpFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 60
ccs 23
cts 23
cp 1
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createRequest() 0 3 1
A createServerRequest() 0 8 2
A createStreamFromResource() 0 3 1
A createResponse() 0 3 1
A createStreamFromFile() 0 3 1
A createStream() 0 3 1
A createUploadedFile() 0 13 1
A createUri() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded\Http;
14
15
/*
16
 *
17
 * Implementation of PSR-17 (HTTP Message Factories)
18
 * @see https://www.php-fig.org/psr/psr-17/
19
 *
20
 */
21
22
use Psr\Http\Message\{RequestFactoryInterface,
23
    RequestInterface,
24
    ResponseFactoryInterface,
25
    ResponseInterface,
26
    ServerRequestFactoryInterface,
27
    ServerRequestInterface,
28
    StreamFactoryInterface,
29
    StreamInterface,
30
    UploadedFileFactoryInterface,
31
    UploadedFileInterface,
32
    UriFactoryInterface,
33
    UriInterface};
34
35
36
class HttpFactory implements RequestFactoryInterface,
37
    ResponseFactoryInterface,
38
    ServerRequestFactoryInterface,
39
    StreamFactoryInterface,
40
    UploadedFileFactoryInterface,
41
    UriFactoryInterface
42
{
43 1
    public function createRequest(string $method, $uri): RequestInterface
44
    {
45 1
        return new ClientRequest($method, $uri);
46
    }
47
48 1
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
49
    {
50 1
        if ($serverParams) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $serverParams of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
51 1
            $_SERVER = \array_replace($_SERVER, $serverParams);
52
        }
53
        $_SERVER['REQUEST_METHOD'] = $method;
54 1
        $_SERVER['REQUEST_URI']    = (string)$uri;
55 1
        return new ServerRequest;
56
    }
57 1
58
    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
59
    {
60 1
        return (new ServerResponse)->withStatus($code, $reasonPhrase);
61
    }
62 1
63
    public function createStream(string $content = ''): StreamInterface
64
    {
65 3
        return create_stream($content);
66
    }
67 3
68
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
69
    {
70 1
        return new FileStream($filename, $mode);
71
    }
72 1
73
    public function createStreamFromResource($resource): StreamInterface
74
    {
75 1
        return create_stream($resource);
76
    }
77 1
78
    public function createUri(string $uri = ''): UriInterface
79
    {
80 2
        return new Uri($uri);
81
    }
82 2
83
    public function createUploadedFile(
84
        StreamInterface $stream,
85 1
        ?int $size = null,
86
        ?int $error = \UPLOAD_ERR_OK,
87
        ?string $clientFilename = null,
88
        ?string $clientMediaType = null
89
    ): UploadedFileInterface {
90
        return new UploadedFile([
91
            'tmp_name' => $stream,
92 1
            'name'     => $clientFilename,
93 1
            'type'     => $clientMediaType,
94 1
            'size'     => $size,
95 1
            'error'    => $error,
96 1
        ]);
97 1
    }
98
}
99