Completed
Push — 1.5 ( 846aba...07d276 )
by Paweł
16s
created

MemoryCachedLoader::isSupported()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
class MemoryCachedLoader implements LoaderInterface
18
{
19
    private $decoratedLoader;
20
21
    private $loadedData = [];
22
23
    public function __construct(LoaderInterface $decoratedLoader)
24
    {
25
        $this->decoratedLoader = $decoratedLoader;
26
    }
27
28
    public function load($metaType, $withParameters = [], $withoutParameters = [], $responseType = self::SINGLE)
29
    {
30
        $cacheKey = $this->getCacheKey($metaType, $withParameters, $withoutParameters, $responseType);
31
        if (array_key_exists($cacheKey, $this->loadedData)) {
32
            return $this->loadedData[$cacheKey];
33
        }
34
35
        $loadedData = $this->decoratedLoader->load($metaType, $withParameters, $withoutParameters, $responseType);
36
        $this->loadedData[$cacheKey] = $loadedData;
37
38
        return $loadedData;
39
    }
40
41
    public function isSupported(string $type): bool
42
    {
43
        return $this->decoratedLoader->isSupported($type);
44
    }
45
46
    private function getCacheKey(string $metaType, array $withParameters, array $withoutParameters, int $responseType): string
47
    {
48
        $keys = [\json_encode($metaType), \json_encode($withParameters), \json_encode($withoutParameters), \json_encode($responseType)];
49
50
        return base64_encode(\implode('::', $keys));
51
    }
52
}
53