Completed
Push — master ( 3460d4...501fc3 )
by Paweł
76:02 queued 29:07
created

MetaKeyGenerator::generateKey()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 11
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 7
nop 1
crap 42
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\ContentBundle\KeyGenerator;
16
17
use Asm89\Twig\CacheExtension\CacheStrategy\KeyGeneratorInterface;
18
use SWP\Component\Common\Model\TimestampableInterface;
19
use SWP\Component\Storage\Model\PersistableInterface;
20
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
21
22
/**
23
 * Key generator for meta objects.
24
 */
25
class MetaKeyGenerator implements KeyGeneratorInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function generateKey($meta)
31
    {
32
        if (is_object($meta) && $meta instanceof Meta) {
33
            $value = $meta->getValues();
34
            $keyElements = [];
35
36
            if ($value instanceof TimestampableInterface) {
37
                $date = null !== $value->getUpdatedAt() ? $value->getUpdatedAt() : $value->getCreatedAt();
38
                $keyElements[] = $date->getTimestamp();
39
            }
40
41
            if ($value instanceof PersistableInterface) {
42
                $keyElements[] = $value->getId();
43
            }
44
45
            return sha1(implode('', $keyElements));
46
        }
47
48
        return sha1(serialize($meta));
49
    }
50
}
51