Test Failed
Push — master ( a81572...90f7a4 )
by Daniel
04:32
created

CachedPageDataMetadataFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 23
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 1
A __construct() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[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 Silverback\ApiComponentsBundle\Metadata\Factory;
15
16
use Psr\Cache\CacheItemPoolInterface;
17
use Silverback\ApiComponentsBundle\Cache\CachedTrait;
18
use Silverback\ApiComponentsBundle\Metadata\PageDataMetadata;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 * @description Based on API Platform CachedResourceMetadataFactory by Teoh Han Hui <[email protected]>
23
 */
24
class CachedPageDataMetadataFactory implements PageDataMetadataFactoryInterface
25
{
26
    use CachedTrait;
27
28
    public const CACHE_KEY_PREFIX = 'page_data_metadata_';
29
30
    private PageDataMetadataFactoryInterface $decorated;
31
32
    public function __construct(CacheItemPoolInterface $cacheItemPool, PageDataMetadataFactoryInterface $decorated)
33
    {
34
        $this->cacheItemPool = $cacheItemPool;
35
        $this->decorated = $decorated;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function create(string $resourceClass): PageDataMetadata
42
    {
43
        $cacheKey = self::CACHE_KEY_PREFIX . md5($resourceClass);
44
45
        return $this->getCached($cacheKey, function () use ($resourceClass) {
46
            return $this->decorated->create($resourceClass);
47
        });
48
    }
49
}
50