BotParserCache::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Application\Service;
4
5
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
6
7
/**
8
 * Class BotParserCache
9
 * @package Application\Service
10
 * @method getPenaltiesList()
11
 * @method getItemsList()
12
 * @method getItems()
13
 * @method getScoreboard()
14
 * @method getDatabase(string $nick = null)
15
 * @method getEvents(int $limit, string $nick = null)
16
 * @method getCoordinates()
17
 * @method getQuestData()
18
 * @method getPlayers()
19
 * @method getMapDimensions()
20
 */
21
class BotParserCache
22
{
23
    private $cache;
24
    private $parser;
25
26
    public function __construct(BotParser $parser, AbstractAdapter $cache)
27
    {
28
        $this->parser = $parser;
29
        $this->cache = $cache;
30
    }
31
32
    private function getKey(string $method, array $args)
33
    {
34
        $key = str_replace(['\\', ':'], '_', get_class() . '::' . $method);
35
        $key .= str_replace('.', '_', implode("-", $args));
36
37
        return $key;
38
    }
39
40
    public function __call($name, $arguments)
41
    {
42
        $key = $this->getKey($name, $arguments);
43
44
        if ($this->cache->hasItem($key)) {
45
            $result = $this->cache->getItem($key);
46
        } else {
47
            $result = call_user_func_array([$this->parser, $name], $arguments);
48
            $this->cache->setItem($key, $result);
49
        }
50
51
        return $result;
52
    }
53
}
54