Passed
Push — master ( 11546f...bc8fa1 )
by Pieter
01:45
created

CachedObjectAccess   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 170
rs 10
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getConstructorArguments() 0 8 1
A getDescription() 0 10 1
A getSetterFields() 0 8 1
A getGetterFields() 0 8 1
A getSetterTypes() 0 9 1
A getGetterTypes() 0 9 1
A cacheCheck() 0 11 2
A instantiate() 0 3 1
A setValue() 0 3 1
A getValue() 0 3 1
A __construct() 0 4 1
A isSupported() 0 11 2
1
<?php
2
3
4
namespace W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess;
5
6
use Closure;
7
use Psr\Cache\CacheItemPoolInterface;
8
use ReflectionClass;
9
10
class CachedObjectAccess implements ObjectAccessSupportedInterface
11
{
12
    /**
13
     * @var ObjectAccessInterface
14
     */
15
    private $internal;
16
17
    /**
18
     * @var CacheItemPoolInterface
19
     */
20
    private $cacheItemPool;
21
22
    /**
23
     * @param ObjectAccessInterface $internal
24
     * @param CacheItemPoolInterface $cacheItemPool
25
     */
26
    public function __construct(ObjectAccessInterface $internal, CacheItemPoolInterface $cacheItemPool)
27
    {
28
        $this->internal = $internal;
29
        $this->cacheItemPool = $cacheItemPool;
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function getGetterFields(ReflectionClass $reflectionClass): array
36
    {
37
        return $this->cacheCheck(
38
            __FUNCTION__ . ',' . $reflectionClass->name,
39
            function (ReflectionClass $reflectionClass) {
40
                return $this->internal->getGetterFields($reflectionClass);
41
            },
42
            $reflectionClass
43
        );
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function getSetterFields(ReflectionClass $reflectionClass): array
50
    {
51
        return $this->cacheCheck(
52
            __FUNCTION__ . ',' . $reflectionClass->name,
53
            function (ReflectionClass $reflectionClass) {
54
                return $this->internal->getSetterFields($reflectionClass);
55
            },
56
            $reflectionClass
57
        );
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function getGetterTypes(ReflectionClass $reflectionClass, string $fieldName): array
64
    {
65
        return $this->cacheCheck(
66
            __FUNCTION__ . ',' . $reflectionClass->name . ',' . $fieldName,
67
            function (ReflectionClass $reflectionClass, string $fieldName) {
68
                return $this->internal->getGetterTypes($reflectionClass, $fieldName);
69
            },
70
            $reflectionClass,
71
            $fieldName
72
        );
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function getSetterTypes(ReflectionClass $reflectionClass, string $fieldName): array
79
    {
80
        return $this->cacheCheck(
81
            __FUNCTION__ . ',' . $reflectionClass->name . ',' . $fieldName,
82
            function (ReflectionClass $reflectionClass, string $fieldName) {
83
                return $this->internal->getSetterTypes($reflectionClass, $fieldName);
84
            },
85
            $reflectionClass,
86
            $fieldName
87
        );
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    public function getConstructorArguments(ReflectionClass $reflectionClass): array
94
    {
95
        return $this->cacheCheck(
96
            __FUNCTION__ . ',' . $reflectionClass->name,
97
            function (ReflectionClass $reflectionClass) {
98
                return $this->internal->getConstructorArguments($reflectionClass);
99
            },
100
            $reflectionClass
101
        );
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function getValue(object $instance, string $fieldName)
108
    {
109
        return $this->internal->getValue($instance, $fieldName);
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function setValue(object $instance, string $fieldName, $value)
116
    {
117
        return $this->internal->setValue($instance, $fieldName, $value);
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123
    public function instantiate(ReflectionClass $reflectionClass, array $constructorArgs): object
124
    {
125
        return $this->internal->instantiate($reflectionClass, $constructorArgs);
126
    }
127
128
    /**
129
     * {@inheritDoc}
130
     */
131
    public function getDescription(ReflectionClass $reflectionClass, string $fieldName, bool $preferGetters): ?string
132
    {
133
        return $this->cacheCheck(
134
            __FUNCTION__ . ',' . $reflectionClass->name . ',' . $fieldName . ',' . json_encode($preferGetters),
135
            function (ReflectionClass $reflectionClass, string $fieldName, bool $preferGetters) {
136
                return $this->internal->getDescription($reflectionClass, $fieldName, $preferGetters);
137
            },
138
            $reflectionClass,
139
            $fieldName,
140
            $preferGetters
141
        );
142
    }
143
144
    /**
145
     * {@inheritDoc}
146
     */
147
    public function isSupported(ReflectionClass $reflectionClass): bool
148
    {
149
        return $this->cacheCheck(
150
            __FUNCTION__ . ',' . $reflectionClass->name,
151
            function (ReflectionClass $reflectionClass) {
152
                if ($this->internal instanceof ObjectAccessSupportedInterface) {
153
                    return $this->internal->isSupported($reflectionClass);
154
                }
155
                return true;
156
            },
157
            $reflectionClass
158
        );
159
    }
160
161
    /**
162
     * Cache helper method.
163
     *
164
     * @param string $cacheKey
165
     * @param Closure $callback
166
     * @param mixed ...$args
167
     * @return mixed
168
     */
169
    private function cacheCheck(string $cacheKey, Closure $callback, ...$args)
170
    {
171
        $cacheKey = str_replace('\\', '|', $cacheKey);
172
        $cacheItem = $this->cacheItemPool->getItem($cacheKey);
173
        if ($cacheItem->isHit()) {
174
            return $cacheItem->get();
175
        }
176
        $returnValue = $callback(...$args);
177
        $cacheItem->set($returnValue);
178
        $this->cacheItemPool->save($cacheItem);
179
        return $returnValue;
180
    }
181
}
182