Completed
Push — master ( 31750b...5f0633 )
by Ryan
07:24
created

CacheKey   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 35
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B generateKey() 0 24 6
1
<?php namespace Anomaly\Streams\Platform\View\Cache;
2
3
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
4
use Anomaly\Streams\Platform\Support\Presenter;
5
use Asm89\Twig\CacheExtension\CacheStrategy\KeyGeneratorInterface;
6
use Illuminate\Contracts\Support\Arrayable;
7
8
/**
9
 * Class CacheKey
10
 *
11
 * @link          http://pyrocms.com/
12
 * @author        PyroCMS, Inc. <[email protected]>
13
 * @author        Ryan Thompson <[email protected]>
14
 * @package       Anomaly\Streams\Platform\View\Cache
15
 */
16
class CacheKey implements KeyGeneratorInterface
17
{
18
19
    /**
20
     * Generate a cache key for a given value.
21
     *
22
     * @param mixed $value
23
     *
24
     * @return string
25
     */
26
    public function generateKey($value)
27
    {
28
        if ($value instanceof Presenter) {
29
            $value = $value->getObject();
30
        }
31
32
        if ($value instanceof EntryInterface) {
33
            return get_class($value) . $value->getId() . $value->lastModified();
34
        }
35
36
        if ($value instanceof Arrayable) {
37
            $value = $value->toArray();
38
        }
39
40
        if (is_array($value)) {
41
            return 'array_' . md5(json_encode($value));
42
        }
43
44
        if (is_string($value)) {
45
            return 'string_' . md5($value);
46
        }
47
48
        return '';
49
    }
50
}
51