Issues (26)

src/Metadata/Repository.php (1 issue)

Labels
Severity
1
<?php namespace JSONAPI\Resource\Metadata;
2
3
use JSONAPI\Resource\Metadata\Resource as ResourceMeta;
4
5
use JSONAPI\Resource\Cache\ArrayCache;
6
use Psr\SimpleCache\CacheInterface;
7
8
/**
9
 * Class used for getting metadata from JSONAPI resource objects.
10
 * To get best performance from all the JSONAPI resource library, cache must be used and same metadata object reused
11
 * between serializers or event requests.
12
 */
13
class Repository
14
{
15
    const CACHE_KEY_RESOURCE = 'class::resource';
16
    const CACHE_KEY_ATTRIBUTES = 'class::attributes';
17
    const CACHE_KEY_RELATIONSHIPS = 'class::relationships';
18
19
    public function __construct(
20
        protected ?CacheInterface $cache = null,
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_PROTECTED, expecting T_VARIABLE on line 20 at column 8
Loading history...
21
        protected ?Factory $factory = null
22
    ) {
23
        $this->cache ??= new ArrayCache();
24
        $this->factory ??= new Factory();
25
    }
26
27
    /**
28
     * Get object resource metadata.
29
     *
30
     * @param object $resource
31
     * @return ResourceMeta
32
     */
33
    public function getResourceMeta(object $resource): ResourceMeta
34
    {
35
        $key = get_class($resource).'::'.static::CACHE_KEY_RESOURCE;
36
37
        if (null === ($result = $this->cache->get($key))) {
38
            $result = $this->factory->buildResourceMeta($resource);
39
40
            $this->cache->set($key, $result);
41
        }
42
43
        return $result;
44
    }
45
46
    /**
47
     * Get resource mapped attributes.
48
     * Returns map of property\method reflection mapped to it is resource attribute.
49
     *
50
     * @param object $resource
51
     * @return Field[]
52
     */
53
    public function getResourceAttributes(object $resource): array
54
    {
55
        $key = get_class($resource).'::'.static::CACHE_KEY_ATTRIBUTES;
56
57
        if (null === ($result = $this->cache->get($key))) {
58
            $result = [];
59
            foreach ($this->factory->buildResourceAttributes($resource) as $attribute) {
60
                $result[$attribute->getKey()] = $attribute;
61
            }
62
63
            $this->cache->set($key, $result);
64
        }
65
66
        return $result;
67
    }
68
69
    /**
70
     * Get resource mapped relationships.
71
     * Returns map of property\method reflection mapped to it is resource attribute.
72
     *
73
     * @param object $resource
74
     * @return Field[]
75
     */
76
    public function getResourceRelationships(object $resource): array
77
    {
78
        $key = get_class($resource).'::'.static::CACHE_KEY_RELATIONSHIPS;
79
80
        if (null === ($result = $this->cache->get($key))) {
81
            $result = [];
82
            foreach ($this->factory->buildResourceRelationships($resource) as $relationship) {
83
                $result[$relationship->getKey()] = $relationship;
84
            }
85
86
            $this->cache->set($key, $result);
87
        }
88
89
        return $result;
90
    }
91
}