1<?php
2
3declare(strict_types=1);
4
5namespace Metadata\Cache;
6
7use Doctrine\Common\Cache\Cache;
8use Metadata\ClassMetadata;
9
10/**
11 * @author Henrik Bjornskov <henrik@bjrnskov.dk>
12 */
13class DoctrineCacheAdapter implements CacheInterface
14{
15    /**
16     * @var string
17     */
18    private $prefix;
19    /**
20     * @var Cache
21     */
22    private $cache;
23
24    public function __construct(string $prefix, Cache $cache)
25    {
26        $this->prefix = $prefix;
27        $this->cache = $cache;
28    }
29
30    /**
31     * {@inheritDoc}
32     */
33    public function load(string $class): ?ClassMetadata
34    {
35        $cache = $this->cache->fetch($this->prefix . $class);
36        return false === $cache ? null : $cache;
37    }
38
39    /**
40     * {@inheritDoc}
41     */
42    public function put(ClassMetadata $metadata): void
43    {
44        $this->cache->save($this->prefix . $metadata->name, $metadata);
45    }
46
47    /**
48     * {@inheritDoc}
49     */
50    public function evict(string $class): void
51    {
52        $this->cache->delete($this->prefix . $class);
53    }
54}
55