Completed
Push — master ( 5a4b2b...9dcc41 )
by Woody
02:09
created

DiscoveryCache   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 28
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A clearCache() 0 6 2
A makeInstance() 0 8 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Http\Factory\Discovery;
5
6
use RuntimeException;
7
8
abstract class DiscoveryCache
9
{
10
    /** @var array */
11
    private static $cache = [];
12
13 6
    protected static function makeInstance(string $interface)
14
    {
15 6
        if (! isset(self::$cache[$interface])) {
16 6
            $implementation = static::locate($interface);
17 6
            self::$cache[$interface] = new $implementation();
18
        }
19
20 6
        return self::$cache[$interface];
21
    }
22
23 6
    public static function clearCache(?string $interface = null): void
24
    {
25 6
        if ($interface === null) {
26
            self::$cache = [];
27
        } else {
28 6
            unset(self::$cache[$interface]);
29
        }
30 6
    }
31
32
    /**
33
     * @throws RuntimeException If no implementation is available
34
     */
35
    abstract protected static function locate(string $interface): string;
36
}
37