DiscoveryCache::clearCache()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 1
crap 2.0625
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 7
    protected static function makeInstance(string $interface)
14
    {
15 7
        if (! isset(self::$cache[$interface])) {
16 7
            $implementation = static::locate($interface);
17 7
            self::$cache[$interface] = new $implementation();
18
        }
19
20 7
        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