Completed
Push — master ( bde4cf...61750e )
by Daniel
9s
created

FileTextExtractable   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 85
wmc 9
lcom 1
cbo 3
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setTextCache() 0 4 1
A getTextCache() 0 4 1
A getFileContent() 0 4 1
B extractFileAsText() 0 24 5
A onBeforeWrite() 0 5 1
1
<?php
2
3
/**
4
 * Decorate File or a File derivative to enable text extraction from the file content. Uses a set of subclasses of
5
 * FileTextExtractor to do the extraction based on the content type of the file.
6
 * 
7
 * Adds an additional property which is the cached contents, which is populated on demand.
8
 *
9
 * @author mstephens
10
 *
11
 */
12
class FileTextExtractable extends DataExtension
0 ignored issues
show
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...
13
{
14
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db 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...
15
        'FileContentCache' => 'Text'
16
    );
17
18
    private static $casting = array(
0 ignored issues
show
Unused Code introduced by
The property $casting 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...
19
        'FileContent' => 'Text'
20
    );
21
22
    private static $dependencies = array(
0 ignored issues
show
Unused Code introduced by
The property $dependencies 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...
23
        'TextCache' => '%$FileTextCache'
24
    );
25
26
    /**
27
     * @var FileTextCache
28
     */
29
    protected $fileTextCache = null;
30
31
    /**
32
     *
33
     * @param FileTextCache $cache
34
     */
35
    public function setTextCache(FileTextCache $cache)
36
    {
37
        $this->fileTextCache = $cache;
38
    }
39
40
    /**
41
     * @return FileTextCache
42
     */
43
    public function getTextCache()
44
    {
45
        return $this->fileTextCache;
46
    }
47
48
    /**
49
     * Helper function for template
50
     *
51
     * @return string
52
     */
53
    public function getFileContent()
54
    {
55
        return $this->extractFileAsText();
56
    }
57
58
    /**
59
     * Tries to parse the file contents if a FileTextExtractor class exists to handle the file type, and returns the text.
60
     * The value is also cached into the File record itself.
61
     * 
62
     * @param boolean $disableCache If false, the file content is only parsed on demand.
63
     * If true, the content parsing is forced, bypassing the cached version
64
     * @return string
65
     */
66
    public function extractFileAsText($disableCache = false)
67
    {
68
        if (!$disableCache) {
69
            $text = $this->getTextCache()->load($this->owner);
70
            if ($text) {
71
                return $text;
72
            }
73
        }
74
75
        // Determine which extractor can process this file.
76
        $extractor = FileTextExtractor::for_file($this->owner->FullPath);
77
        if (!$extractor) {
78
            return null;
79
        }
80
81
        $text = $extractor->getContent($this->owner->FullPath);
82
        if (!$text) {
83
            return null;
84
        }
85
86
        $this->getTextCache()->save($this->owner, $text);
87
88
        return $text;
89
    }
90
91
    public function onBeforeWrite()
92
    {
93
        // Clear cache before changing file
94
        $this->getTextCache()->invalidate($this->owner);
95
    }
96
}
97