Completed
Push — master ( 4e07a2...96fcad )
by Tobias
03:18
created

Psr17Factory::createStreamFromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Psr7\Factory;
6
7
use Interop\Http\Factory\RequestFactoryInterface;
8
use Interop\Http\Factory\ResponseFactoryInterface;
9
use Interop\Http\Factory\StreamFactoryInterface;
10
use Interop\Http\Factory\UploadedFileFactoryInterface;
11
use Interop\Http\Factory\UriFactoryInterface;
12
use Nyholm\Psr7\Request;
13
use Nyholm\Psr7\Response;
14
use Nyholm\Psr7\Stream;
15
use Nyholm\Psr7\UploadedFile;
16
use Nyholm\Psr7\Uri;
17
use Psr\Http\Message\RequestInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\StreamInterface;
20
use Psr\Http\Message\UploadedFileInterface;
21
use Psr\Http\Message\UriInterface;
22
23
/**
24
 * @author Tobias Nyholm <[email protected]>
25
 */
26
class Psr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, UriFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface
27
{
28 7
    public function createRequest(string $method, $uri): RequestInterface
29
    {
30 7
        return new Request($method, $uri);
31
    }
32
33 4
    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
34
    {
35 4
        return new Response($code, [], null, '1.1', $reasonPhrase);
36
    }
37
38 6
    public function createStream(string $content = ''): StreamInterface
39
    {
40 6
        return Stream::create($content);
41
    }
42
43 1
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
44
    {
45 1
        return Stream::createFromResource(fopen($filename, $mode));
46
    }
47
48 1
    public function createStreamFromResource($resource): StreamInterface
49
    {
50 1
        return Stream::createFromResource($resource);
51
    }
52
53 12
    public function createUploadedFile(
54
        StreamInterface $stream,
55
        int $size = null,
56
        int $error = \UPLOAD_ERR_OK,
57
        string $clientFilename = null,
58
        string $clientMediaType = null
59
    ): UploadedFileInterface {
60 12
        if (null === $size) {
61 12
            $size = $stream->getSize();
62
        }
63
64 12
        return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
65
    }
66
67 13
    public function createUri(string $uri = ''): UriInterface
68
    {
69 13
        return new Uri($uri);
70
    }
71
}
72