ApiPlatform\Core\Metadata\Resource\Factory\CachedResourceMetadataFactory::create PHP Method

create() public method

public create ( string $resourceClass ) : ResourceMetadata
$resourceClass string
return ApiPlatform\Core\Metadata\Resource\ResourceMetadata
    public function create(string $resourceClass) : ResourceMetadata
    {
        $cacheKey = self::CACHE_KEY_PREFIX . md5(serialize([$resourceClass]));
        try {
            $cacheItem = $this->cacheItemPool->getItem($cacheKey);
            if ($cacheItem->isHit()) {
                return $cacheItem->get();
            }
        } catch (CacheException $e) {
            // do nothing
        }
        $resourceMetadata = $this->decorated->create($resourceClass);
        if (!isset($cacheItem)) {
            return $resourceMetadata;
        }
        $cacheItem->set($resourceMetadata);
        $this->cacheItemPool->save($cacheItem);
        return $resourceMetadata;
    }

Usage Example

 public function testCreateWithGetCacheItemThrowsCacheException()
 {
     $decoratedResourceMetadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
     $decoratedResourceMetadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(null, 'Dummy.'))->shouldBeCalled();
     $cacheException = $this->prophesize(CacheException::class);
     $cacheException->willExtend(\Exception::class);
     $cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
     $cacheItemPool->getItem($this->generateCacheKey())->willThrow($cacheException->reveal())->shouldBeCalled();
     $cachedResourceMetadataFactory = new CachedResourceMetadataFactory($cacheItemPool->reveal(), $decoratedResourceMetadataFactory->reveal());
     $resultedResourceMetadata = $cachedResourceMetadataFactory->create(Dummy::class);
     $this->assertInstanceOf(ResourceMetadata::class, $resultedResourceMetadata);
     $this->assertEquals(new ResourceMetadata(null, 'Dummy.'), $resultedResourceMetadata);
 }
CachedResourceMetadataFactory