SetRepository   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generateFeatureSetCacheKey() 0 3 1
A deleteFeatureSet() 0 3 1
A saveFeatureSet() 0 6 1
A __construct() 0 3 1
A getFeatureSet() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\Doorkeeper\Features;
6
7
use Psr\Cache;
8
9
final class SetRepository
10
{
11
    private Cache\CacheItemPoolInterface $cache;
12
13
    public function __construct(Cache\CacheItemPoolInterface $cache)
14
    {
15
        $this->cache = $cache;
16
    }
17
18
    public function saveFeatureSet(Set $set): void
19
    {
20
        $cacheItem = $this->cache->getItem(self::generateFeatureSetCacheKey());
21
        $cacheItem->set($set);
22
23
        $this->cache->save($cacheItem);
24
    }
25
26
    public function getFeatureSet(SetProviderInterface $fallback = null): Set
27
    {
28
        $result = $this->cache->getItem(self::generateFeatureSetCacheKey())->get();
29
30
        if (!$result && $fallback) {
31
            $result = $fallback->getFeatureSet();
32
            $this->saveFeatureSet($fallback->getFeatureSet());
33
        }
34
35
        return ($result) ?? new Set();
36
    }
37
38
    public function deleteFeatureSet(): void
39
    {
40
        $this->cache->deleteItem(self::generateFeatureSetCacheKey());
41
    }
42
43
    public static function generateFeatureSetCacheKey(): string
44
    {
45
        return md5(self::class);
46
    }
47
}
48