Completed
Pull Request — master (#28)
by
unknown
08:00
created

FileTextCache_Database   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 22
wmc 5
lcom 0
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 4 1
A save() 0 6 2
A invalidate() 0 7 2
1
<?php
2
3
interface FileTextCache
0 ignored issues
show
Coding Style Compatibility introduced by
Each interface must be in a namespace of at least one level (a top-level vendor name)

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    /**
6
     * Save extracted content for a given File entity
7
     *
8
     * @param File $file
9
     * @param string $content
10
     */
11
    public function save(File $file, $content);
12
13
    /**
14
     * Return any cached extracted content for a given file entity
15
     *
16
     * @param File $file
17
     */
18
    public function load(File $file);
19
20
    /**
21
     * Invalidate the cache for a given file.
22
     * Invoked in onBeforeWrite on the file
23
     *
24
     * @param File $file
25
     */
26
    public function invalidate(File $file);
27
}
28
29
/**
30
 * Caches the extracted content on the record for the file.
31
 * Limits the stored file content by default to avoid hitting query size limits.
32
 */
33
class FileTextCache_Database implements FileTextCache
0 ignored issues
show
Coding Style Compatibility introduced by
Each interface must be in a file by itself

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
34
{
35
    public function load(File $file)
36
    {
37
        return $file->FileContentCache;
0 ignored issues
show
Bug introduced by
The property FileContentCache does not seem to exist. Did you mean Content?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
38
    }
39
40
    public function save(File $file, $content)
41
    {
42
        $maxLength = Config::inst()->get('FileTextCache_Database', 'max_content_length');
43
        $file->FileContentCache = ($maxLength) ? substr($content, 0, $maxLength) : $content;
0 ignored issues
show
Bug introduced by
The property FileContentCache does not seem to exist. Did you mean Content?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
44
        $file->write();
45
    }
46
47
    public function invalidate(File $file)
48
    {
49
        // To prevent writing to the cache from invalidating it
50
        if (!$file->isChanged('FileContentCache')) {
51
            $file->FileContentCache = '';
0 ignored issues
show
Bug introduced by
The property FileContentCache does not seem to exist. Did you mean Content?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52
        }
53
    }
54
}
55
56
/**
57
 * Uses SS_Cache with a lifetime to cache extracted content
58
 */
59
class FileTextCache_SSCache implements FileTextCache, Flushable
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
60
{
61
    /**
62
     * Lifetime of cache in seconds
63
     * Null is indefinite
64
     *
65
     * @var int|null
66
     * @config
67
     */
68
    private static $lifetime = null;
0 ignored issues
show
Unused Code introduced by
The property $lifetime is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
69
70
    /**
71
     * @return SS_Cache
72
     */
73
    protected static function get_cache()
74
    {
75
        $lifetime = Config::inst()->get(__CLASS__, 'lifetime');
76
        $cache = SS_Cache::factory(__CLASS__);
77
        $cache->setLifetime($lifetime);
78
        return $cache;
79
    }
80
81
    protected function getKey(File $file)
82
    {
83
        return md5($file->getFullPath());
84
    }
85
86
    public function load(File $file)
87
    {
88
        $key = $this->getKey($file);
89
        $cache = self::get_cache();
90
        return $cache->load($key);
91
    }
92
93
    public function save(File $file, $content)
94
    {
95
        $key = $this->getKey($file);
96
        $cache = self::get_cache();
97
        return $cache->save($content, $key);
98
    }
99
100
    public static function flush()
101
    {
102
        $cache = self::get_cache();
103
        $cache->clean();
104
    }
105
106
    public function invalidate(File $file)
107
    {
108
        $key = $this->getKey($file);
109
        $cache = self::get_cache();
110
        return $cache->remove($key);
111
    }
112
}
113