Completed
Push — master ( d4cfd0...1c3910 )
by Antoine
18s queued 10s
created

CachedPropertyMetadataFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Metadata\Property\Factory;
15
16
use ApiPlatform\Core\Cache\CachedTrait;
17
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
18
use Psr\Cache\CacheItemPoolInterface;
19
20
/**
21
 * Caches property metadata.
22
 *
23
 * @author Teoh Han Hui <[email protected]>
24
 */
25
final class CachedPropertyMetadataFactory implements PropertyMetadataFactoryInterface
26
{
27
    use CachedTrait;
28
29
    const CACHE_KEY_PREFIX = 'property_metadata_';
30
31
    private $decorated;
32
33
    public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyMetadataFactoryInterface $decorated)
34
    {
35
        $this->cacheItemPool = $cacheItemPool;
36
        $this->decorated = $decorated;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata
43
    {
44
        $cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $property, $options]));
45
46
        return $this->getCached($cacheKey, function () use ($resourceClass, $property, $options) {
47
            return $this->decorated->create($resourceClass, $property, $options);
48
        });
49
    }
50
}
51