Passed
Pull Request — 4 (#10232)
by Steve
14:15 queued 07:56
created

FlushInvalidatedResource::__serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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(): array
44
    {
45
        return [];
46
    }
47
48
    public function __unserialize(array $data): void
49
    {
50
        // no-op
51
    }
52
53
    /**
54
     * The __serialize() magic method will be automatically used instead of this
55
     *
56
     * @return string
57
     * @deprecated will be removed in 5.0
58
     */
59
    public function serialize()
60
    {
61
        return '';
62
    }
63
64
    /**
65
     * The __unserialize() magic method will be automatically used instead of this almost all the time
66
     * This method will be automatically used if existing serialized data was not saved as an associative array
67
     * and the PHP version used in less than PHP 9.0
68
     *
69
     * @param string $serialized
70
     * @deprecated will be removed in 5.0
71
     */
72
    public function unserialize($serialized)
73
    {
74
        // no-op
75
    }
76
77
    public static function flush()
78
    {
79
        // Mark canary as dirty
80
        static::touch();
81
    }
82
83
    /**
84
     * Path to i18n canary
85
     *
86
     * @return string
87
     */
88
    protected static function canary()
89
    {
90
        return TEMP_PATH . DIRECTORY_SEPARATOR . 'catalog.i18n_canary';
91
    }
92
93
    /**
94
     * Touch the canary
95
     */
96
    protected static function touch()
97
    {
98
        touch(static::canary());
99
    }
100
}
101