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

HttpFactory::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;
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 RuntimeException;
12
use Tebe\HttpFactory\Factory\DiactorosFactory;
13
use Tebe\HttpFactory\Factory\FactoryInterface;
14
use Tebe\HttpFactory\Factory\GuzzleFactory;
15
use Tebe\HttpFactory\Factory\NyholmFactory;
16
use Tebe\HttpFactory\Factory\SlimFactory;
17
18
/**
19
 * Simple class to create instances of PSR-7 classes.
20
 */
21
class HttpFactory implements FactoryInterface
22
{
23
24
    /**
25
     * @var FactoryInterface
26
     */
27
    private $factory;
28
29
    /**
30
     * @var array
31
     */
32
    private static $strategies = [
33
        DiactorosFactory::class,
34
        GuzzleFactory::class,
35
        SlimFactory::class,
36
        NyholmFactory::class
37
    ];
38
39
    /**
40
     * @param FactoryInterface $factory
41
     * @return HttpFactory
42
     */
43
    public function setFactory(FactoryInterface $factory)
44
    {
45
        $this->factory = $factory;
46
        return $this;
47
    }
48
49
    /**
50
     * @return FactoryInterface
51
     */
52
    public function getFactory()
53
    {
54
        if ($this->factory !== null) {
55
            return $this->factory;
56
        }
57
58
        foreach (self::$strategies as $className) {
59
            if (!class_exists($className)) {
60
                continue;
61
            }
62
63
            if (strpos($className, __NAMESPACE__) === 0 && !$className::isInstalled()) {
64
                continue;
65
            }
66
67
            return $this->factory = new $className();
68
        }
69
70
        throw new RuntimeException('No PSR-7 library detected');
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
77
    {
78
        return $this->getFactory()->createResponse($code, $reasonPhrase);
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
85
    {
86
        return $this->getFactory()->createServerRequest($method, $uri, $serverParams);
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function createStream(string $content = ''): StreamInterface
93
    {
94
        return $this->getFactory()->createStream($content);
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
101
    {
102
        return $this->getFactory()->createStreamFromFile($filename, $mode);
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function createStreamFromResource($resource): StreamInterface
109
    {
110
        return $this->getFactory()->createStreamFromResource($resource);
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function createUri(string $uri = ''): UriInterface
117
    {
118
        return $this->getFactory()->createUri($uri);
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function createUploadedFile(
125
        StreamInterface $stream,
126
        int $size = null,
127
        int $error = \UPLOAD_ERR_OK,
128
        string $clientFilename = null,
129
        string $clientMediaType = null
130
    ): UploadedFileInterface {
131
        return $this->getFactory()->createUploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
132
    }
133
134
    /**
135
     * @return static
136
     */
137
    public static function instance()
138
    {
139
        static $instance;
140
        if (is_null($instance)) {
141
            $instance = new static();
142
        }
143
        return $instance;
144
    }
145
146
    /**
147
     * @param array $strategies
148
     */
149
    public static function setStrategies(array $strategies)
150
    {
151
        static::$strategies = $strategies;
0 ignored issues
show
Bug introduced by
Since $strategies is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $strategies to at least protected.
Loading history...
152
    }
153
154
    /**
155
     * @return array
156
     */
157
    public static function getStrategies()
158
    {
159
        return static::$strategies;
0 ignored issues
show
Bug introduced by
Since $strategies is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $strategies to at least protected.
Loading history...
160
    }
161
}
162