Cache::generateKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 2
cp 0.5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1.125
1
<?php
2
3
namespace Ablunier\Laravel\Database\Repository\Eloquent;
4
5
use Ablunier\Laravel\Database\Contracts\Repository\Cache as CacheContract;
6
use Ablunier\Laravel\Database\Contracts\Repository\Repository as RepositoryContract;
7
use Ablunier\Laravel\Database\Repository\Exceptions\RepositoryException;
8
use Illuminate\Contracts\Cache\Repository as LaravelCache;
9
use ReflectionClass;
10
11
class Cache implements CacheContract
12
{
13
    /**
14
     * @var RepositoryContract
15
     */
16
    protected $repository;
17
18
    /**
19
     * @var LaravelCache
20
     */
21
    protected $cache;
22
23
    /**
24
     * @var bool
25
     */
26
    protected $skipCache = false;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $refreshCache = false;
32
33
    /**
34
     * @var string
35
     */
36
    protected $key;
37
38
    /**
39
     * @var int
40
     */
41
    protected $lifetime;
42
43
    public function __construct(RepositoryContract $repository, LaravelCache $cache)
44
    {
45 6
        $this->repository = $repository;
46
        $this->cache = $cache;
47 6
    }
48 6
49 6
    public function __call($method, $params)
50
    {
51
        if (!method_exists($this->repository, $method)) {
52
            throw new RepositoryException("Method $method not found on repository");
53
        }
54 3
55
        if ($this->skipCache === true || config('laravel-database.cache') === false) {
56 3
            return call_user_func_array([$this->repository, $method], $params);
57 1
        } else {
58
            if (empty($this->key)) {
59
                $this->cacheKey($this->generateKey($method, $params));
60 2
            }
61 1
            $key = $this->key;
62
            unset($this->key);
63 1
64 1
            if ($this->refreshCache) {
65 1
                $this->cache->forget($key);
66 1
67 1
                $this->refreshCache = false;
68
            }
69 1
70 1
            if (empty($this->lifetime)) {
71
                $this->cacheLifetime($this->repository->getModel()->cacheLifetime());
72 1
            }
73 1
            $lifetime = $this->lifetime;
74
            unset($this->lifetime);
75 1
76 1
            return $this->cache->remember($key, $lifetime, function () use ($method, $params) {
77 1
                return call_user_func_array([$this->repository, $method], $params);
78 1
            });
79 1
        }
80
    }
81 1
82
    protected function generateKey($method, $params)
83 1
    {
84
        $className = (new ReflectionClass($this->repository->getModel()))->getShortName();
85
86
        return strtolower($className).'.'.strtolower($method).'.'.md5(serialize($params));
87
    }
88
89
    public function skipCache($status = true)
90 1
    {
91
        $this->skipCache = $status;
92 1
93
        return $this;
94 1
    }
95
96
    public function refreshCache()
97
    {
98
        $this->refreshCache = true;
99
100 1
        return $this;
101
    }
102 1
103
    public function cacheKey($name)
104 1
    {
105
        $this->key = $name;
106
107
        return $this;
108
    }
109
110 1
    public function cacheLifetime($minutes)
111
    {
112 1
        $this->lifetime = $minutes;
113
114 1
        return $this;
115
    }
116
}
117