Completed
Push — master ( ce35d9...d4b6d7 )
by Tobias
03:30
created

Psr17Factory::createStreamFromFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.032
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Psr7\Factory;
6
7
use Nyholm\Psr7\{Request, Response, ServerRequest, Stream, UploadedFile, Uri};
8
use Psr\Http\Message\{RequestFactoryInterface, RequestInterface, ResponseFactoryInterface, ResponseInterface, ServerRequestFactoryInterface, ServerRequestInterface, StreamFactoryInterface, StreamInterface, UploadedFileFactoryInterface, UploadedFileInterface, UriFactoryInterface, UriInterface};
9
10
/**
11
 * @author Tobias Nyholm <[email protected]>
12
 * @author Martijn van der Ven <[email protected]>
13
 */
14
final class Psr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
15
{
16 7
    public function createRequest(string $method, $uri): RequestInterface
17
    {
18 7
        return new Request($method, $uri);
1 ignored issue
show
Bug introduced by
It seems like $uri defined by parameter $uri on line 16 can also be of type object<Psr\Http\Message\UriInterface>; however, Nyholm\Psr7\Request::__construct() does only seem to accept string|object<UriInterface>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
19
    }
20
21 4
    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
22
    {
23 4
        return new Response($code, [], null, '1.1', $reasonPhrase);
24
    }
25
26 6
    public function createStream(string $content = ''): StreamInterface
27
    {
28 6
        return Stream::create($content);
29
    }
30
31 1
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
32
    {
33 1
        $resource = @\fopen($filename, $mode);
34 1
        if (false === $resource) {
35
            throw new \RuntimeException('The file '.$filename.'cannot be opened.');
36
        }
37
38 1
        return Stream::create($resource);
39
    }
40
41 1
    public function createStreamFromResource($resource): StreamInterface
42
    {
43 1
        return Stream::create($resource);
44
    }
45
46 12
    public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null): UploadedFileInterface
47
    {
48 12
        if (null === $size) {
49 12
            $size = $stream->getSize();
50
        }
51
52 12
        return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
53
    }
54
55 13
    public function createUri(string $uri = ''): UriInterface
56
    {
57 13
        return new Uri($uri);
58
    }
59
60 23
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
61
    {
62 23
        return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
1 ignored issue
show
Bug introduced by
It seems like $uri defined by parameter $uri on line 60 can also be of type object<Psr\Http\Message\UriInterface>; however, Nyholm\Psr7\ServerRequest::__construct() does only seem to accept string|object<UriInterface>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
63
    }
64
}
65