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

TikaServerTextExtractor   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 109
wmc 17
lcom 1
cbo 3
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 10 2
A getServerEndpoint() 0 13 3
A getVersion() 0 6 1
A isAvailable() 0 6 3
A supportsExtension() 0 5 1
B supportsMime() 0 19 6
A getContent() 0 4 1
1
<?php
2
3
/**
4
 * Enables text extraction of file content via the Tika Rest Server
5
 *
6
 * {@link http://tika.apache.org/1.7/gettingstarted.html}
7
 */
8
class TikaServerTextExtractor extends FileTextExtractor
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...
9
{
10
    /**
11
     * Tika server is pretty efficient so use it immediately if available
12
     *
13
     * @var integer
14
     * @config
15
     */
16
    private static $priority = 80;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $priority 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...
17
18
    /**
19
     * Server endpoint
20
     *
21
     * @var string
22
     * @config
23
     */
24
    private static $server_endpoint;
0 ignored issues
show
Unused Code introduced by
The property $server_endpoint 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...
25
26
    /**
27
     * @var TikaRestClient
28
     */
29
    protected $client = null;
30
31
    /**
32
     * @return TikaRestClient
33
     */
34
    public function getClient()
35
    {
36
        return $this->client ?:
37
            ($this->client =
38
                Injector::inst()->createWithArgs(
39
                    'TikaRestClient',
40
                    array($this->getServerEndpoint())
41
                )
42
            );
43
    }
44
45
    public function getServerEndpoint()
46
    {
47
        if (defined('SS_TIKA_ENDPOINT')) {
48
            return SS_TIKA_ENDPOINT;
49
        }
50
51
        if (getenv('SS_TIKA_ENDPOINT')) {
52
            return getenv('SS_TIKA_ENDPOINT');
53
        }
54
55
        // Default to configured endpoint
56
        return $this->config()->server_endpoint;
57
    }
58
59
    /**
60
     * Get the version of tika installed, or 0 if not installed
61
     *
62
     * @return float version of tika
63
     */
64
    public function getVersion()
65
    {
66
        return $this
67
            ->getClient()
68
            ->getVersion();
69
    }
70
71
    public function isAvailable()
72
    {
73
        return $this->getServerEndpoint() &&
74
            $this->getClient()->isAvailable() &&
75
            $this->getVersion() >= 1.7;
76
    }
77
78
    public function supportsExtension($extension)
79
    {
80
        // Determine support via mime type only
81
        return false;
82
    }
83
84
85
    /**
86
     * Cache of supported mime types
87
     *
88
     * @var array
89
     */
90
    protected $supportedMimes = array();
91
92
    public function supportsMime($mime)
93
    {
94
        $supported = $this->supportedMimes ?:
95
            ($this->supportedMimes = $this->getClient()->getSupportedMimes());
96
97
        // Check if supported (most common / quickest lookup)
98
        if (isset($supported[$mime])) {
99
            return true;
100
        }
101
102
        // Check aliases
103
        foreach ($supported as $info) {
104
            if (isset($info['alias']) && in_array($mime, $info['alias'])) {
105
                return true;
106
            }
107
        }
108
109
        return false;
110
    }
111
112
    public function getContent($path)
113
    {
114
        return $this->getClient()->tika($path);
115
    }
116
}
117