Passed
Push — master ( eb38d0...297af8 )
by Thomas
01:48
created

GuzzleFactory::createUploadedFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Tebe\HttpFactory\Factory;
5
6
use GuzzleHttp\Psr7\Response;
7
use GuzzleHttp\Psr7\ServerRequest;
8
use GuzzleHttp\Psr7\Stream;
9
use GuzzleHttp\Psr7\UploadedFile;
10
use GuzzleHttp\Psr7\Uri;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Message\StreamInterface;
14
use Psr\Http\Message\UploadedFileInterface;
15
use Psr\Http\Message\UriInterface;
16
17
/**
18
 * Simple class to create response instances of PSR-7 classes.
19
 */
20
class GuzzleFactory implements FactoryInterface
21
{
22
    /**
23
     * Check whether Guzzle Http is available
24
     */
25
    public static function isInstalled(): bool
26
    {
27
        return class_exists('GuzzleHttp\\Psr7\\Response')
28
            && class_exists('GuzzleHttp\\Psr7\\ServerRequest')
29
            && class_exists('GuzzleHttp\\Psr7\\Stream')
30
            && class_exists('GuzzleHttp\\Psr7\\Uri');
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
37
    {
38
        return new Response($code, [], null, '1.1', $reasonPhrase);
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
45
    {
46
        return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function createStream(string $content = ''): StreamInterface
53
    {
54
        $stream = $this->createStreamFromFile('php://temp', 'r+');
55
        $stream->write($content);
56
57
        return $stream;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
64
    {
65
        return $this->createStreamFromResource(fopen($filename, $mode));
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function createStreamFromResource($resource): StreamInterface
72
    {
73
        return new Stream($resource);
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function createUri(string $uri = ''): UriInterface
80
    {
81
        return new Uri($uri);
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function createUploadedFile(
88
        StreamInterface $stream,
89
        int $size = null,
90
        int $error = \UPLOAD_ERR_OK,
91
        string $clientFilename = null,
92
        string $clientMediaType = null
93
    ): UploadedFileInterface {
94
        return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
95
    }
96
}
97