MemoryCachedLoader::getCacheKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Templates System.
5
 *
6
 * Copyright 2019 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2019 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\TemplatesSystem\Gimme\Loader;
16
17
use function json_encode;
18
19
class MemoryCachedLoader implements LoaderInterface
20
{
21
    private $decoratedLoader;
22
23
    private $loadedData = [];
24
25
    public function __construct(LoaderInterface $decoratedLoader)
26
    {
27
        $this->decoratedLoader = $decoratedLoader;
28
    }
29
30
    public function load($metaType, $withParameters = [], $withoutParameters = [], $responseType = self::SINGLE)
31
    {
32
        $cacheKey = $this->getCacheKey($metaType, $withParameters, $withoutParameters, $responseType);
33
        if (array_key_exists($cacheKey, $this->loadedData)) {
34
            return $this->loadedData[$cacheKey];
35
        }
36
37
        $loadedData = $this->decoratedLoader->load($metaType, $withParameters, $withoutParameters, $responseType);
38
        $this->loadedData[$cacheKey] = $loadedData;
39
40
        return $loadedData;
41
    }
42
43
    public function isSupported(string $type): bool
44
    {
45
        return $this->decoratedLoader->isSupported($type);
46
    }
47
48
    private function getCacheKey(string $metaType, array $withParameters, array $withoutParameters, int $responseType): string
49
    {
50
        $keys = json_encode([$metaType, $withParameters, $withoutParameters, $responseType], JSON_THROW_ON_ERROR, 512);
51
52
        return md5($keys);
53
    }
54
}
55