Passed
Push — feature/improve-file-cache ( 6742d2 )
by Chema
04:18
created

DocBlockResolverCache   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 33
ccs 14
cts 14
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isProjectCacheEnabled() 0 3 1
A getCacheInstance() 0 17 3
A resetCache() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\DocBlockResolver;
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 22
    public static function resetCache(): void
24
    {
25 22
        self::$cache = null;
26
    }
27
28 25
    public static function getCacheInstance(): CacheInterface
29
    {
30 25
        if (self::$cache !== null) {
31 15
            self::dispatchEvent(new CustomServicesCacheCachedEvent());
32
33 15
            return self::$cache;
34
        }
35
36 13
        if (self::isProjectCacheEnabled()) {
37 4
            self::dispatchEvent(new CustomServicesPhpCacheCreatedEvent());
38 4
            self::$cache = new CustomServicesPhpCache(Config::getInstance()->getCacheDir());
39
        } else {
40 9
            self::dispatchEvent(new CustomServicesInMemoryCacheCreatedEvent());
41 9
            self::$cache = new InMemoryCache(CustomServicesPhpCache::class);
42
        }
43
44 13
        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 13
    private static function isProjectCacheEnabled(): bool
48
    {
49 13
        return (new GacelaFileCache(Config::getInstance()))->isEnabled();
50
    }
51
}
52