Completed
Push — master ( 90072e...c81959 )
by Daniel
11:23
created

FlushInvalidatedResource   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A getResource() 0 5 1
A isFresh() 0 12 2
A serialize() 0 4 1
A unserialize() 0 4 1
A flush() 0 5 1
A canary() 0 4 1
A touch() 0 4 1
1
<?php
2
3
namespace SilverStripe\i18n\Messages\Symfony;
4
5
use SilverStripe\Core\Flushable;
6
use Symfony\Component\Config\Resource\DirectoryResource;
7
use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
8
9
/**
10
 * Some arbitrary resource which expires when flush is invoked.
11
 * Uses a canary file to mark future freshness requests as stale.
12
 *
13
 * @link https://media.giphy.com/media/fRRD3T37DeY6Y/giphy.gif for use case
14
 * @see DirectoryResource
15
 */
16
class FlushInvalidatedResource implements SelfCheckingResourceInterface, \Serializable, Flushable
17
{
18
19
    public function __toString()
20
    {
21
        return md5(__CLASS__);
22
    }
23
24
    public function getResource()
25
    {
26
        // @deprecated at 3.0, do nothing
27
        return null;
28
    }
29
30
    public function isFresh($timestamp)
31
    {
32
        // Check mtime of canary
33
        $canary = static::canary();
34
        if (file_exists($canary)) {
35
            return filemtime($canary) < $timestamp;
36
        }
37
38
        // Rebuild canary
39
        static::touch();
40
        return false;
41
    }
42
43
    public function serialize()
44
    {
45
        return '';
46
    }
47
48
    public function unserialize($serialized)
49
    {
50
        // no-op
51
    }
52
53
    public static function flush()
54
    {
55
        // Mark canary as dirty
56
        static::touch();
57
    }
58
59
    /**
60
     * Path to i18n canary
61
     *
62
     * @return string
63
     */
64
    protected static function canary()
65
    {
66
        return TEMP_FOLDER . '/catalog.i18n_canary';
67
    }
68
69
    /**
70
     * Touch the canary
71
     */
72
    protected static function touch()
73
    {
74
        touch(static::canary());
75
    }
76
}
77