Completed
Pull Request — master (#35)
by Jake Dale
01:11
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
    * @config
12
    * @var array
13
    */
14
    private $options = ['username' => null, 'password' => null];
15
16
    /*
17
    * @var array
18
    */
19
    protected $mimes = [];
20
21
    public function __construct($baseUrl = '', $config = null)
22
    {
23
        if (defined('SS_TIKA_USERNAME') && defined('SS_TIKA_PASSWORD')) {
24
            $this->options = [
25
                'username' => SS_TIKA_USERNAME,
26
                'password' => SS_TIKA_PASSWORD,
27
            ];
28
        }
29
        parent::__construct($baseUrl, $config);
30
    }
31
32
    /**
33
     * Detect if the service is available
34
     *
35
     * @return bool
36
     */
37
    public function isAvailable()
38
    {
39
        try {
40
            $result = $this->get(null);
41
            $result->setAuth($this->options['username'], $this->options['password']);
42
            $result->send();
43
            if ($result->getResponse()->getStatusCode() == 200) {
44
                return true;
45
            }
46
        } catch (RequestException $ex) {
47
            SS_Log::log(sprintf("Tika unavailable - %s", $ex->getMessage()), SS_Log::ERR);
48
            return false;
49
        }
50
    }
51
52
    /**
53
     * Get version code
54
     *
55
     * @return float
56
     */
57
    public function getVersion()
58
    {
59
        $response = $this->get('version');
60
        $response->setAuth($this->options['username'], $this->options['password']);
61
        $response->send();
62
        $version = 0.0;
63
        // Parse output
64
        if ($response->getResponse()->getStatusCode() == 200 &&
65
            preg_match('/Apache Tika (?<version>[\.\d]+)/', $response->getResponse()->getBody(), $matches)
66
        ) {
67
            $version = (float)$matches['version'];
68
        }
69
        return $version;
70
    }
71
72
    /**
73
     * Gets supported mime data. May include aliased mime types.
74
     *
75
     * @return array
76
     */
77
    public function getSupportedMimes()
78
    {
79
        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...
80
            return $this->mimes;
81
        }
82
        $response = $this->get(
83
            'mime-types',
84
            ['Accept' => 'application/json']
85
        );
86
        $response->setAuth($this->options['username'], $this->options['password']);
87
        $response->send();
88
        return $this->mimes = $response->getResponse()->json();
89
    }
90
91
    /**
92
     * Extract text content from a given file.
93
     * Logs a notice-level error if the document can't be parsed.
94
     *
95
     * @param string $file Full filesystem path to a file to post
96
     * @return string Content of the file extracted as plain text
97
     */
98
    public function tika($file)
99
    {
100
        $text = null;
101
        try {
102
            $response = $this->put(
103
                'tika',
104
                ['Accept' => 'text/plain'],
105
                file_get_contents($file)
106
            );
107
            $response->setAuth($this->options['username'], $this->options['password']);
108
            $response->send();
109
            $text = $response->getResponse()->getBody(true);
110
        } catch (RequestException $e) {
111
            $msg = sprintf(
112
                'TikaRestClient was not able to process %s. Response: %s %s.',
113
                $file,
114
                $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...
115
                $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...
116
            );
117
            // Only available if tika-server was started with --includeStack
118
            $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...
119
            if ($body) {
120
                $msg .= ' Body: ' . $body;
121
            }
122
            SS_Log::log($msg, SS_Log::NOTICE);
123
        }
124
        return $text;
125
    }
126
}
127