Completed
Push — master ( f5e876...d97b85 )
by Timo
13s
created

SimpleCache   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
wmc 2
lcom 1
cbo 0
ccs 7
cts 7
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A retrieve() 0 6 1
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models\Cache;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * Class SimpleCache
9
 * @package hamburgscleanest\DataTables\Models\Cache
10
 */
11
class SimpleCache implements Cache {
12
13
    /** @var int */
14
    protected $_minutes;
15
    /** @var string */
16
    private $_key;
17
18
    /**
19
     * SimpleCache constructor.
20
     * @param int $minutes For how long should the data be cached?
21
     */
22 1
    public function __construct(int $minutes = 60)
23
    {
24 1
        $this->_minutes = $minutes;
25 1
        $this->_key = \md5(\sprintf('%s-%s', $minutes, \request()->url()));
26 1
    }
27
28
    /**
29
     * @param \Closure $dataFunction
30
     * @return Collection
31
     */
32
    public function retrieve(\Closure $dataFunction) : Collection
33
    {
34 1
        return \Illuminate\Support\Facades\Cache::remember($this->_key, $this->_minutes, function() use ($dataFunction) {
35 1
            return $dataFunction();
36 1
        });
37
    }
38
}