Completed
Push — master ( 1edbed...8ff159 )
by Thomas
02:48
created

DiactorosFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createStreamFromFile() 0 4 1
A createServerRequest() 0 8 1
A createStream() 0 6 1
A createServerRequestFromGlobals() 0 3 1
A createResponse() 0 5 2
A createUploadedFile() 0 8 1
A isInstalled() 0 6 4
A createUri() 0 3 1
A createStreamFromResource() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Tebe\HttpFactory\Factory;
5
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\StreamInterface;
9
use Psr\Http\Message\UploadedFileInterface;
10
use Psr\Http\Message\UriInterface;
11
use Zend\Diactoros\Response;
12
use Zend\Diactoros\ServerRequest;
13
use Zend\Diactoros\ServerRequestFactory;
14
use Zend\Diactoros\Stream;
15
use Zend\Diactoros\UploadedFile;
16
use Zend\Diactoros\Uri;
17
18
/**
19
 * Simple class to create response instances of PSR-7 classes.
20
 */
21
class DiactorosFactory implements FactoryInterface
22
{
23
    /**
24
     * Check whether Zend Diactoros is available
25
     */
26
    public static function isInstalled(): bool
27
    {
28
        return class_exists('Zend\\Diactoros\\Response')
29
            && class_exists('Zend\\Diactoros\\ServerRequest')
30
            && class_exists('Zend\\Diactoros\\Stream')
31
            && class_exists('Zend\\Diactoros\\Uri');
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
38
    {
39
        $response = new Response('php://memory', $code);
40
41
        return $reasonPhrase !== '' ? $response->withStatus($code, $reasonPhrase) : $response;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
48
    {
49
        return new ServerRequest(
50
            $serverParams,
51
            [],
52
            $uri,
53
            $method,
54
            $this->createStream()
55
        );
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function createServerRequestFromGlobals(): ServerRequestInterface
62
    {
63
        return ServerRequestFactory::fromGlobals();
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function createStream(string $content = ''): StreamInterface
70
    {
71
        $stream = $this->createStreamFromFile('php://temp', 'r+');
72
        $stream->write($content);
73
74
        return $stream;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
81
    {
82
        $resource = fopen($filename, $mode);
83
        return $this->createStreamFromResource($resource);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function createStreamFromResource($resource): StreamInterface
90
    {
91
        return new Stream($resource);
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function createUri(string $uri = ''): UriInterface
98
    {
99
        return new Uri($uri);
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    public function createUploadedFile(
106
        StreamInterface $stream,
107
        int $size = null,
108
        int $error = \UPLOAD_ERR_OK,
109
        string $clientFilename = null,
110
        string $clientMediaType = null
111
    ): UploadedFileInterface {
112
        return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
113
    }
114
}
115