CachedService::query()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php declare(strict_types = 1);
2
  
3
namespace JSKOS;
4
5
use Psr\SimpleCache\CacheInterface;
6
7
/**
8
 * Caches a service.
9
 */
10
class CachedService extends Service
11
{
12
    protected $service;
13
    protected $cache;
14
15
    public function __construct(Service $service, CacheInterface $cache)
16
    {
17
        $this->service = $service;
18
        $this->cache = $cache;
19
    }
20
21
    public function query(array $query=[], string $path=''): Result
22
    {
23
        ksort($query);
24
        $key = $path . md5(serialize($query));
25
26
        $result = $this->cache->get($key);
27
        if (!$result) {
28
            $result = $this->service->query($query, $path);
29
            $this->cache->set($key, $result);
30
        }
31
32
        return $result;
33
    }
34
}
35