DiscoveryCache   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A makeInstance() 0 8 2
A clearCache() 0 6 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 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