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

DiscoveryCache::makeInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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