CachedPageDataMetadataFactory::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
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
 *
23
 * @description Based on API Platform CachedResourceMetadataFactory by Teoh Han Hui <[email protected]>
24
 */
25
class CachedPageDataMetadataFactory implements PageDataMetadataFactoryInterface
26
{
27
    use CachedTrait;
28
29
    public const CACHE_KEY_PREFIX = 'page_data_metadata_';
30
31
    private PageDataMetadataFactoryInterface $decorated;
32
33
    public function __construct(CacheItemPoolInterface $cacheItemPool, PageDataMetadataFactoryInterface $decorated)
34
    {
35
        $this->cacheItemPool = $cacheItemPool;
36
        $this->decorated = $decorated;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function create(string $resourceClass): PageDataMetadata
43
    {
44
        $cacheKey = self::CACHE_KEY_PREFIX . md5($resourceClass);
45
46
        return $this->getCached($cacheKey, function () use ($resourceClass) {
47
            return $this->decorated->create($resourceClass);
48
        });
49
    }
50
}
51