• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 14037207180

24 Mar 2025 01:59PM UTC coverage: 8.442% (-0.07%) from 8.513%
14037207180

push

github

soyuka
Merge 4.0

0 of 1 new or added line in 1 file covered. (0.0%)

10772 existing lines in 358 files now uncovered.

13274 of 157241 relevant lines covered (8.44%)

22.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.47
/src/Metadata/Resource/Factory/CachedResourceMetadataCollectionFactory.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Metadata\Resource\Factory;
15

16
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
17
use Psr\Cache\CacheException;
18
use Psr\Cache\CacheItemPoolInterface;
19

20
/**
21
 * Caches resource metadata.
22
 *
23
 * @author Antoine Bluchet <soyuka@gmail.com>
24
 */
25
final class CachedResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
26
{
27
    public const CACHE_KEY_PREFIX = 'resource_metadata_collection_';
28
    private array $localCache = [];
29

30
    public function __construct(private readonly CacheItemPoolInterface $cacheItemPool, private readonly ResourceMetadataCollectionFactoryInterface $decorated)
31
    {
UNCOV
32
    }
2,051✔
33

34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function create(string $resourceClass): ResourceMetadataCollection
38
    {
UNCOV
39
        $cacheKey = self::CACHE_KEY_PREFIX.hash('xxh3', $resourceClass);
2,044✔
UNCOV
40
        if (\array_key_exists($cacheKey, $this->localCache)) {
2,044✔
UNCOV
41
            return new ResourceMetadataCollection($resourceClass, $this->localCache[$cacheKey]);
1,953✔
42
        }
43

44
        try {
UNCOV
45
            $cacheItem = $this->cacheItemPool->getItem($cacheKey);
2,044✔
46
        } catch (CacheException) {
×
47
            $resourceMetadataCollection = $this->decorated->create($resourceClass);
×
48
            $this->localCache[$cacheKey] = (array) $resourceMetadataCollection;
×
49

50
            return $resourceMetadataCollection;
×
51
        }
52

UNCOV
53
        if ($cacheItem->isHit()) {
2,044✔
UNCOV
54
            $this->localCache[$cacheKey] = $cacheItem->get();
2,007✔
55

UNCOV
56
            return new ResourceMetadataCollection($resourceClass, $this->localCache[$cacheKey]);
2,007✔
57
        }
58

UNCOV
59
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
135✔
UNCOV
60
        $this->localCache[$cacheKey] = (array) $resourceMetadataCollection;
135✔
UNCOV
61
        $cacheItem->set($this->localCache[$cacheKey]);
135✔
UNCOV
62
        $this->cacheItemPool->save($cacheItem);
135✔
63

UNCOV
64
        return $resourceMetadataCollection;
135✔
65
    }
66
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc