CachedSchemaFactoryDecorator::createSchema()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
c 1
b 0
f 1
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace ElevenLabs\Api\Factory;
3
4
use Psr\Cache\CacheItemPoolInterface;
5
6
/**
7
 * Load/Store API Schema from Cache
8
 */
9
class CachedSchemaFactoryDecorator implements SchemaFactory
10
{
11
    /** @var SchemaFactory */
12
    private $schemaFactory;
13
14
    /** @var CacheItemPoolInterface */
15
    private $cache;
16
17
    public function __construct(CacheItemPoolInterface $cache, SchemaFactory $schemaFactory)
18 2
    {
19
        $this->cache = $cache;
20 2
        $this->schemaFactory = $schemaFactory;
21 2
    }
22 2
23
    public function createSchema($schemaFile)
24 2
    {
25
        $cacheKey = hash('sha1', $schemaFile);
26 2
        $item = $this->cache->getItem($cacheKey);
27 2
        if ($item->isHit()) {
28 2
            $schema = $item->get();
29 1
        } else {
30
            $schema = $this->schemaFactory->createSchema($schemaFile);
31 1
            $this->cache->save($item->set($schema));
32 1
        }
33
34
        return $schema;
35 2
    }
36
}
37