DiactorosFactory   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createServerRequestFromGlobals() 0 3 1
A createUploadedFile() 0 8 1
A createStreamFromFile() 0 4 1
A createServerRequest() 0 8 1
A createStream() 0 6 1
A createResponse() 0 5 2
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 Laminas\Diactoros\Response;
7
use Laminas\Diactoros\ServerRequest;
8
use Laminas\Diactoros\ServerRequestFactory;
9
use Laminas\Diactoros\Stream;
10
use Laminas\Diactoros\UploadedFile;
11
use Laminas\Diactoros\Uri;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\StreamInterface;
15
use Psr\Http\Message\UploadedFileInterface;
16
use Psr\Http\Message\UriInterface;
17
18
/**
19
 * Simple class to create response instances of PSR-7 classes.
20
 */
21
class DiactorosFactory implements FactoryInterface
22
{
23
    /**
24
     * Check whether Laminas Diactoros is available
25
     */
26
    public static function isInstalled(): bool
27
    {
28
        return class_exists('Laminas\\Diactoros\\Response')
29
            && class_exists('Laminas\\Diactoros\\ServerRequest')
30
            && class_exists('Laminas\\Diactoros\\Stream')
31
            && class_exists('Laminas\\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);
0 ignored issues
show
Bug introduced by
It seems like $size can also be of type null; however, parameter $size of Laminas\Diactoros\UploadedFile::__construct() does only seem to accept integer, 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

112
        return new UploadedFile($stream, /** @scrutinizer ignore-type */ $size, $error, $clientFilename, $clientMediaType);
Loading history...
113
    }
114
}
115