Passed
Push — master ( 6627c9...55f346 )
by Pedro
02:07
created

ImageGeneratorCache::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Application\Service;
4
5
use Zend\Cache\Storage\Adapter\AbstractAdapter;
6
7
class ImageGeneratorCache
8
{
9
    private $imageGenerator;
10
    private $cache;
11
12
    public function __construct(ImageGenerator $imageGenerator, AbstractAdapter $cache)
13
    {
14
        $this->imageGenerator = $imageGenerator;
15
        $this->cache = $cache;
16
    }
17
18
    private function getKey(string $method, array $args)
19
    {
20
        $key = str_replace(['\\', ':'], '_', get_class() . '::' . $method);
21
        $key .= str_replace('.', '_', implode("-", $args));
22
23
        return $key;
24
    }
25
26
    public function __call($name, $arguments)
27
    {
28
        $key = $this->getKey($name, $arguments);
29
30
        if ($this->cache->hasItem($key)) {
31
            $result = $this->cache->getItem($key);
32
        } else {
33
            $result = call_user_func_array([$this->imageGenerator, $name], $arguments);
34
            $this->cache->setItem($key, $result);
35
        }
36
37
        return $result;
38
    }
39
}
40