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

CacheKey::generateKey()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
rs 8.5125
cc 6
eloc 12
nc 14
nop 1
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