Passed
Pull Request — master (#9)
by Magnar Ovedal
02:07
created

DiscoveryCache::makeInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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
    protected static function makeInstance(string $interface)
14
    {
15
        if (! isset(self::$cache[$interface])) {
16
            $implementation = static::locate($interface);
17
            self::$cache[$interface] = new $implementation();
18
        }
19
20
        return self::$cache[$interface];
21
    }
22
23 7
    public static function clearCache(?string $interface = null): void
24
    {
25 7
        if ($interface === null) {
26
            self::$cache = [];
27
        } else {
28 7
            unset(self::$cache[$interface]);
29
        }
30 7
    }
31
32
    /**
33
     * @throws RuntimeException If no implementation is available
34
     */
35
    abstract protected static function locate(string $interface): string;
36
}
37