Passed
Pull Request — master (#17)
by Mihail
15:10
created

HttpFactory::createUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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 Koded\Http\Interfaces\HttpMethod;
23
use Psr\Http\Message\{RequestFactoryInterface,
24
    RequestInterface,
25
    ResponseFactoryInterface,
26
    ResponseInterface,
27
    ServerRequestFactoryInterface,
28
    ServerRequestInterface,
29
    StreamFactoryInterface,
30
    StreamInterface,
31
    UploadedFileFactoryInterface,
32
    UploadedFileInterface,
33
    UriFactoryInterface,
34
    UriInterface};
35
use function array_replace;
36
use function strtoupper;
37
use const UPLOAD_ERR_OK;
38
39
40
class HttpFactory implements RequestFactoryInterface,
41
    ResponseFactoryInterface,
42
    ServerRequestFactoryInterface,
43 1
    StreamFactoryInterface,
44
    UploadedFileFactoryInterface,
45 1
    UriFactoryInterface
46
{
47
    public function createRequest(string $method, $uri): RequestInterface
48 1
    {
49
        return new ClientRequest(HttpMethod::tryFrom(strtoupper($method)), $uri);
0 ignored issues
show
Bug introduced by
It seems like Koded\Http\Interfaces\Ht...om(strtoupper($method)) can also be of type null; however, parameter $method of Koded\Http\ClientRequest::__construct() does only seem to accept Koded\Http\Interfaces\HttpMethod, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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