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

DocBlockResolverCache   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resetCache() 0 3 1
A getCacheInstance() 0 17 3
A isProjectCacheEnabled() 0 3 1
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