Completed
Push — 0.3 ( da43ab...625b56 )
by Ben
96:17 queued 52:29
created

Memoize::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Common\Helpers;
4
5
class Memoize
6
{
7
    public static $cache = [];
8
9
    private $baseKey;
10
11 87
    public function __construct($baseKey)
12
    {
13 87
        $this->baseKey = $baseKey;
14 87
    }
15
16 87
    public function run(\Closure $closure, array $parameters = [])
17
    {
18
        // construct cachekey
19 87
        $cachekey = $this->baseKey.':'.md5(implode('', $parameters));
20
21 87
        if (isset(static::$cache[$cachekey])) {
22 65
            return static::$cache[$cachekey];
23
        }
24
25 87
        return static::$cache[$cachekey] = call_user_func_array($closure, $parameters);
26
    }
27
28 433
    public static function clear()
29
    {
30 433
        static::$cache = [];
31 433
    }
32
}
33