Passed
Push — main ( 724a5a...47e974 )
by Chema
06:46 queued 02:34
created

DocBlockResolverCache::resetCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\ServiceResolver;
6
7
use Gacela\Framework\ClassResolver\Cache\CacheInterface;
8
use Gacela\Framework\ClassResolver\Cache\CustomServicesPhpCache;
9
use Gacela\Framework\ClassResolver\Cache\GacelaFileCache;
10
use Gacela\Framework\ClassResolver\Cache\InMemoryCache;
11
use Gacela\Framework\Config\Config;
12
use Gacela\Framework\Event\ClassResolver\Cache\CustomServicesCacheCachedEvent;
13
use Gacela\Framework\Event\ClassResolver\Cache\CustomServicesInMemoryCacheCreatedEvent;
14
use Gacela\Framework\Event\ClassResolver\Cache\CustomServicesPhpCacheCreatedEvent;
15
use Gacela\Framework\Event\Dispatcher\EventDispatchingCapabilities;
16
17
final class DocBlockResolverCache
18
{
19
    use EventDispatchingCapabilities;
20
21
    private static ?CacheInterface $cache = null;
22
23
    public static function resetCache(): void
24
    {
25
        self::$cache = null;
26
    }
27
28
    public static function getCacheInstance(): CacheInterface
29
    {
30
        if (self::$cache instanceof CacheInterface) {
31
            self::dispatchEvent(new CustomServicesCacheCachedEvent());
32
33
            return self::$cache;
34
        }
35
36
        if (self::isProjectCacheEnabled()) {
37
            self::dispatchEvent(new CustomServicesPhpCacheCreatedEvent());
38
            self::$cache = new CustomServicesPhpCache(Config::getInstance()->getCacheDir());
39
        } else {
40
            self::dispatchEvent(new CustomServicesInMemoryCacheCreatedEvent());
41
            self::$cache = new InMemoryCache(CustomServicesPhpCache::class);
42
        }
43
44
        return self::$cache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::cache returns the type null which is incompatible with the type-hinted return Gacela\Framework\ClassRe...er\Cache\CacheInterface.
Loading history...
45
    }
46
47
    private static function isProjectCacheEnabled(): bool
48
    {
49
        return (new GacelaFileCache(Config::getInstance()))->isEnabled();
50
    }
51
}
52