Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
9 / 9 |
|
100.00% |
8 / 8 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| ExternalDataCacheManager | |
100.00% |
20 / 20 |
|
100.00% |
9 / 9 |
|
100.00% |
8 / 8 |
|
100.00% |
7 / 7 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hash | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSets | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getParts | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPartElements | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSetElements | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| clear | |
100.00% |
2 / 2 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\CollectionManagement\Infrastructure\Service; |
| 4 | |
| 5 | use App\CollectionManagement\Domain\Model\External\ExternalElementCollection; |
| 6 | use App\CollectionManagement\Domain\Model\External\ExternalSetElementCollection; |
| 7 | use App\CollectionManagement\Domain\Model\PartCollection; |
| 8 | use App\CollectionManagement\Domain\Model\SetCollection; |
| 9 | use Symfony\Component\Cache\Adapter\AbstractAdapter; |
| 10 | use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; |
| 11 | use Symfony\Contracts\Cache\CacheInterface; |
| 12 | use Symfony\Contracts\Cache\ItemInterface; |
| 13 | |
| 14 | #[Autoconfigure] |
| 15 | class ExternalDataCacheManager |
| 16 | { |
| 17 | private const int TTL = 86400; |
| 18 | |
| 19 | public function __construct( |
| 20 | private readonly CacheInterface $cache, |
| 21 | ) { |
| 22 | } |
| 23 | |
| 24 | private function hash(string $key): string |
| 25 | { |
| 26 | return hash('sha256', strtolower($key)); |
| 27 | } |
| 28 | |
| 29 | public function getSets(string $search, callable $callback): ?SetCollection |
| 30 | { |
| 31 | // return cache when present |
| 32 | return $this->cache->get('search_set_'.$this->hash($search), function (ItemInterface $item) use ($search, $callback) { |
| 33 | $item->expiresAfter(self::TTL); |
| 34 | return $callback($search); |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | public function getParts(string $search, callable $callback): ?PartCollection |
| 39 | { |
| 40 | // return cache when present |
| 41 | return $this->cache->get('search_part_'.$this->hash($search), function (ItemInterface $item) use ($search, $callback) { |
| 42 | $item->expiresAfter(self::TTL); |
| 43 | return $callback($search); |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | public function getPartElements(string $partExternalId, callable $callback): ?ExternalElementCollection |
| 48 | { |
| 49 | // return cache when present |
| 50 | return $this->cache->get('get_part_elements'.$this->hash($partExternalId), function (ItemInterface $item) use ($partExternalId, $callback) { |
| 51 | $item->expiresAfter(self::TTL); |
| 52 | return $callback($partExternalId); |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | public function getSetElements(string $setExternalId, callable $callback): ?ExternalSetElementCollection |
| 57 | { |
| 58 | // return cache when present |
| 59 | return $this->cache->get('get_set_elements'.$this->hash($setExternalId), function (ItemInterface $item) use ($setExternalId, $callback) { |
| 60 | $item->expiresAfter(self::TTL); |
| 61 | return $callback($setExternalId); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | public function clear(): void |
| 66 | { |
| 67 | if ($this->cache instanceof AbstractAdapter) { |
| 68 | $this->cache->clear(); |
| 69 | } |
| 70 | } |
| 71 | } |