CacheableBlock::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 5
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace TheCodingMachine\CMS\Block;
3
4
use TheCodingMachine\CMS\Cache\CacheableInterface;
5
use TheCodingMachine\CMS\Theme\ThemeDescriptorInterface;
6
7
class CacheableBlock extends Block implements CacheableInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    private $key;
13
    /**
14
     * @var int
15
     */
16
    private $ttl;
17
    /**
18
     * @var array
19
     */
20
    private $tags;
21
22
    /**
23
     * @param ThemeDescriptorInterface $themeDescriptor
24
     * @param mixed[] $context
25
     */
26
    public function __construct(ThemeDescriptorInterface $themeDescriptor, array $context, string $key, int $ttl, array $tags = [])
27
    {
28
        parent::__construct($themeDescriptor, $context);
29
        $this->key = $key;
30
        $this->ttl = $ttl;
31
        $this->tags = $tags;
32
    }
33
34
    public function jsonSerialize()
35
    {
36
        $json = parent::jsonSerialize();
37
        $json['key'] = $this->key;
38
        $json['ttl'] = $this->ttl;
39
        $json['tags'] = $this->tags;
40
        return $json;
41
    }
42
43
    /**
44
     * Returns the time to live of this object, in seconds.
45
     *
46
     * @return int
47
     */
48
    public function getTtl(): int
49
    {
50
        return $this->ttl;
51
    }
52
53
    /**
54
     * Returns the tags applied to this object.
55
     * Useful for purging all elements by a certain tag.
56
     *
57
     * @return string[]
58
     */
59
    public function getTags(): array
60
    {
61
        return $this->tags;
62
    }
63
64
    /**
65
     * Returns the cache key for this object.
66
     *
67
     * @return string
68
     */
69
    public function getKey(): string
70
    {
71
        return $this->key;
72
    }
73
}
74