Completed
Push — master ( 40ba6a...eb7a45 )
by Robbie
01:23
created

TikaRestClient::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 2
1
<?php
2
3
use Guzzle\Http\Client;
4
use Guzzle\Http\Exception\RequestException;
5
6
class TikaRestClient extends Client
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...
7
{
8
    /**
9
     * Authentication options to be sent to the Tika server
10
     *
11
     * @var array
12
     */
13
    protected $options = array('username' => null, 'password' => null);
14
15
    /**
16
     * @var array
17
     */
18
    protected $mimes = array();
19
20
    public function __construct($baseUrl = '', $config = null)
21
    {
22
        if (defined('SS_TIKA_USERNAME') && defined('SS_TIKA_PASSWORD')) {
23
            $this->options = array(
24
                'username' => SS_TIKA_USERNAME,
25
                'password' => SS_TIKA_PASSWORD,
26
            );
27
        }
28
        parent::__construct($baseUrl, $config);
29
    }
30
31
    /**
32
     * Detect if the service is available
33
     *
34
     * @return bool
35
     */
36
    public function isAvailable()
37
    {
38
        try {
39
            $result = $this->get(null);
40
            $result->setAuth($this->options['username'], $this->options['password']);
41
            $result->send();
42
            if ($result->getResponse()->getStatusCode() == 200) {
43
                return true;
44
            }
45
        } catch (RequestException $ex) {
46
            SS_Log::log(sprintf("Tika unavailable - %s", $ex->getMessage()), SS_Log::ERR);
47
            return false;
48
        }
49
    }
50
51
    /**
52
     * Get version code
53
     *
54
     * @return float
55
     */
56
    public function getVersion()
57
    {
58
        $response = $this->get('version');
59
        $response->setAuth($this->options['username'], $this->options['password']);
60
        $response->send();
61
        $version = 0.0;
62
        // Parse output
63
        if ($response->getResponse()->getStatusCode() == 200 &&
64
            preg_match('/Apache Tika (?<version>[\.\d]+)/', $response->getResponse()->getBody(), $matches)
65
        ) {
66
            $version = (float)$matches['version'];
67
        }
68
        return $version;
69
    }
70
71
    /**
72
     * Gets supported mime data. May include aliased mime types.
73
     *
74
     * @return array
75
     */
76
    public function getSupportedMimes()
77
    {
78
        if ($this->mimes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->mimes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
79
            return $this->mimes;
80
        }
81
        $response = $this->get(
82
            'mime-types',
83
            array('Accept' => 'application/json')
84
        );
85
        $response->setAuth($this->options['username'], $this->options['password']);
86
        $response->send();
87
        return $this->mimes = $response->getResponse()->json();
88
    }
89
90
    /**
91
     * Extract text content from a given file.
92
     * Logs a notice-level error if the document can't be parsed.
93
     *
94
     * @param string $file Full filesystem path to a file to post
95
     * @return string Content of the file extracted as plain text
96
     */
97
    public function tika($file)
98
    {
99
        $text = null;
100
        try {
101
            $response = $this->put(
102
                'tika',
103
                array('Accept' => 'text/plain'),
104
                file_get_contents($file)
105
            );
106
            $response->setAuth($this->options['username'], $this->options['password']);
107
            $response->send();
108
            $text = $response->getResponse()->getBody(true);
109
        } catch (RequestException $e) {
110
            $msg = sprintf(
111
                'TikaRestClient was not able to process %s. Response: %s %s.',
112
                $file,
113
                $e->getResponse()->getStatusCode(),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Guzzle\Http\Exception\RequestException as the method getResponse() does only exist in the following sub-classes of Guzzle\Http\Exception\RequestException: Guzzle\Http\Exception\BadResponseException, Guzzle\Http\Exception\ClientErrorResponseException, Guzzle\Http\Exception\ServerErrorResponseException, Guzzle\Http\Exception\TooManyRedirectsException. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
114
                $e->getResponse()->getReasonPhrase()
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Guzzle\Http\Exception\RequestException as the method getResponse() does only exist in the following sub-classes of Guzzle\Http\Exception\RequestException: Guzzle\Http\Exception\BadResponseException, Guzzle\Http\Exception\ClientErrorResponseException, Guzzle\Http\Exception\ServerErrorResponseException, Guzzle\Http\Exception\TooManyRedirectsException. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
115
            );
116
            // Only available if tika-server was started with --includeStack
117
            $body = $e->getResponse()->getBody(true);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Guzzle\Http\Exception\RequestException as the method getResponse() does only exist in the following sub-classes of Guzzle\Http\Exception\RequestException: Guzzle\Http\Exception\BadResponseException, Guzzle\Http\Exception\ClientErrorResponseException, Guzzle\Http\Exception\ServerErrorResponseException, Guzzle\Http\Exception\TooManyRedirectsException. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
118
            if ($body) {
119
                $msg .= ' Body: ' . $body;
120
            }
121
            SS_Log::log($msg, SS_Log::NOTICE);
122
        }
123
        return $text;
124
    }
125
}
126