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

ImageGeneratorCache   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 15
dl 0
loc 31
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 12 2
A getKey() 0 6 1
A __construct() 0 4 1
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