SlimFactory   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 104
rs 10
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createServerRequestFromGlobals() 0 4 1
A createUploadedFile() 0 18 3
A createStreamFromFile() 0 3 1
A isInstalled() 0 7 5
A createServerRequest() 0 9 2
A createStream() 0 6 1
A createUri() 0 3 1
A createStreamFromResource() 0 3 1
A createResponse() 0 5 2
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 Slim\Http\Environment;
12
use Slim\Http\Headers;
13
use Slim\Http\Request;
14
use Slim\Http\Response;
15
use Slim\Http\Stream;
16
use Slim\Http\UploadedFile;
17
use Slim\Http\Uri;
18
19
/**
20
 * Simple class to create response instances of PSR-7 classes.
21
 */
22
class SlimFactory implements FactoryInterface
23
{
24
    /**
25
     * Check whether Slim Http is available
26
     */
27
    public static function isInstalled(): bool
28
    {
29
        return class_exists('Slim\\Http\\Environment')
30
            && class_exists('Slim\\Http\\Response')
31
            && class_exists('Slim\\Http\\Request')
32
            && class_exists('Slim\\Http\\Stream')
33
            && class_exists('Slim\\Http\\Uri');
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
40
    {
41
        $response = new Response($code);
42
43
        return $reasonPhrase !== '' ? $response->withStatus($code, $reasonPhrase) : $response;
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
50
    {
51
        return new Request(
52
            $method,
53
            is_string($uri) ? $this->createUri($uri) : $uri,
54
            new Headers(),
55
            [],
56
            $serverParams,
57
            $this->createStream()
58
        );
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function createServerRequestFromGlobals(): ServerRequestInterface
65
    {
66
        $environment = Environment::mock();
67
        return Request::createFromEnvironment($environment);
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function createUri(string $uri = ''): UriInterface
74
    {
75
        return Uri::createFromString($uri);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function createStream(string $content = ''): StreamInterface
82
    {
83
        $stream = $this->createStreamFromFile('php://temp', 'r+');
84
        $stream->write($content);
85
86
        return $stream;
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
93
    {
94
        return $this->createStreamFromResource(fopen($filename, $mode));
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function createStreamFromResource($resource): StreamInterface
101
    {
102
        return new Stream($resource);
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function createUploadedFile(
109
        StreamInterface $stream,
110
        int $size = null,
111
        int $error = \UPLOAD_ERR_OK,
112
        string $clientFilename = null,
113
        string $clientMediaType = null
114
    ): UploadedFileInterface {
115
        if ($size === null) {
116
            $size = $stream->getSize();
117
        }
118
        $meta = $stream->getMetadata();
119
        $file = $meta['uri'];
120
        if ($file === 'php://temp') {
121
            // Slim needs an actual path to the file
122
            $file = tempnam(sys_get_temp_dir(), 'factory-test');
123
            file_put_contents($file, $stream->getContents());
124
        }
125
        return new UploadedFile($file, $clientFilename, $clientMediaType, $size, $error);
126
    }
127
}
128