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

Memoize   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
eloc 9
c 2
b 1
f 0
dl 0
loc 26
ccs 10
cts 10
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 10 2
A __construct() 0 3 1
A clear() 0 3 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