Completed
Push — master ( 4fd8a4...c47858 )
by Woody
14s queued 10s
created

HttpFactory::serverRequestFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Http\Factory\Discovery;
5
6
use InvalidArgumentException;
7
use Psr\Http\Message\RequestFactoryInterface;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Psr\Http\Message\ServerRequestFactoryInterface;
10
use Psr\Http\Message\StreamFactoryInterface;
11
use Psr\Http\Message\UploadedFileFactoryInterface;
12
use Psr\Http\Message\UriFactoryInterface;
13
use RuntimeException;
14
15
final class HttpFactory
16
{
17
    /** @var array */
18
    private static $factories = [];
19
20 1
    public static function requestFactory(): RequestFactoryInterface
21
    {
22 1
        return self::factoryInstance(RequestFactoryInterface::class);
23
    }
24
25 1
    public static function responseFactory(): ResponseFactoryInterface
26
    {
27 1
        return self::factoryInstance(ResponseFactoryInterface::class);
28
    }
29
30 1
    public static function serverRequestFactory(): ServerRequestFactoryInterface
31
    {
32 1
        return self::factoryInstance(ServerRequestFactoryInterface::class);
33
    }
34
35 1
    public static function streamFactory(): StreamFactoryInterface
36
    {
37 1
        return self::factoryInstance(StreamFactoryInterface::class);
38
    }
39
40 1
    public static function uploadedFileFactory(): UploadedFileFactoryInterface
41
    {
42 1
        return self::factoryInstance(UploadedFileFactoryInterface::class);
43
    }
44
45 1
    public static function uriFactory(): UriFactoryInterface
46
    {
47 1
        return self::factoryInstance(UriFactoryInterface::class);
48
    }
49
50 6
    private static function factoryInstance(string $factoryInterface)
51
    {
52 6
        if (! isset(self::$factories[$factoryInterface])) {
53 6
            $factory = FactoryLocator::locate($factoryInterface);
54 6
            self::$factories[$factoryInterface] = new $factory();
55
        }
56
57 6
        return self::$factories[$factoryInterface];
58
    }
59
60 6
    public static function clearCache(?string $factoryInterface = null): void
61
    {
62 6
        if ($factoryInterface === null) {
63
            self::$factories = [];
64
        } else {
65 6
            unset(self::$factories[$factoryInterface]);
66
        }
67 6
    }
68
}
69