CacheEventListener   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onGet() 0 11 1
A onDelete() 0 11 1
A __construct() 0 3 1
A onSet() 0 11 1
1
<?php
2
3
namespace PhpCache\Example\EventListener;
4
5
use DateTimeImmutable;
6
use DateTimeZone;
7
use PhpCache\CacheEventListener\CacheEventListenerInterface;
8
use PhpCache\Example\Logger\LogEntry;
9
10
/**
11
 * Description of CacheEventListener.
12
 *
13
 * @author kdudas
14
 */
15
class CacheEventListener implements CacheEventListenerInterface
16
{
17
    private $logger;
18
19
    public function __construct($logger)
20
    {
21
        $this->logger = $logger;
22
    }
23
24
    public function onDelete($key, $entry)
25
    {
26
        $logEntry = new LogEntry(
27
            new DateTimeImmutable(date('Y-m-d H:i:s'), new DateTimeZone('UTC')),
28
            LogEntry::SEVERITY_INFO,
29
            'Cache Entry with key '.$key.' deleted'
30
        );
31
32
        $this->logger->log($logEntry);
33
34
        return $entry;
35
    }
36
37
    public function onGet($key, $entry)
38
    {
39
        $logEntry = new LogEntry(
40
            new DateTimeImmutable(date('Y-m-d H:i:s'), new DateTimeZone('UTC')),
41
            LogEntry::SEVERITY_INFO,
42
            'Cache Entry with key '.$key.' requested'
43
        );
44
45
        $this->logger->log($logEntry);
46
47
        return $entry;
48
    }
49
50
    public function onSet($key, $entry)
51
    {
52
        $logEntry = new LogEntry(
53
            new DateTimeImmutable(date('Y-m-d H:i:s'), new DateTimeZone('UTC')),
54
            LogEntry::SEVERITY_INFO,
55
            'Cache Entry with key '.$key.' was set into cache pool'
56
        );
57
58
        $this->logger->log($logEntry);
59
60
        return $entry;
61
    }
62
}
63