Completed
Pull Request — master (#5)
by Magnar Ovedal
04:12
created

HttpFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 51
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A streamFactory() 0 3 1
A serverRequestFactory() 0 3 1
A uploadedFileFactory() 0 3 1
A responseFactory() 0 3 1
A factoryInstance() 0 8 2
A uriFactory() 0 3 1
A requestFactory() 0 3 1
A clearCache() 0 6 2
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