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

TikaRestClient   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isAvailable() 0 10 2
A getVersion() 0 12 3
A getSupportedMimes() 0 13 2
B tika() 0 29 3
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
     * Detect if the service is available
10
     *
11
     * @return bool
12
     */
13
    public function isAvailable()
14
    {
15
        try {
16
            return $this
17
                ->get()->send()
18
                ->getStatusCode() == 200;
19
        } catch (RequestException $ex) {
20
            return false;
21
        }
22
    }
23
24
    /**
25
     * Get version code
26
     *
27
     * @return float
28
     */
29
    public function getVersion()
30
    {
31
        $response = $this->get('version')->send();
32
        // Parse output
33
        if ($response->getStatusCode() == 200 &&
34
            preg_match('/Apache Tika (?<version>[\.\d]+)/', $response->getBody(), $matches)
35
        ) {
36
            return (float)$matches['version'];
37
        }
38
39
        return 0.0;
40
    }
41
42
    protected $mimes = array();
43
44
    /**
45
     * Gets supported mime data. May include aliased mime types.
46
     *
47
     * @return array
48
     */
49
    public function getSupportedMimes()
50
    {
51
        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...
52
            return $this->mimes;
53
        }
54
55
        $response = $this->get(
56
            'mime-types',
57
            array('Accept' => 'application/json')
58
        )->send();
59
60
        return $this->mimes = $response->json();
61
    }
62
63
    /**
64
     * Extract text content from a given file.
65
     * Logs a notice-level error if the document can't be parsed.
66
     *
67
     * @param string $file Full filesystem path to a file to post
68
     * @return string Content of the file extracted as plain text
69
     */
70
    public function tika($file)
71
    {
72
        $text = null;
73
        try {
74
            $response = $this->put(
75
                'tika',
76
                array('Accept' => 'text/plain'),
77
                file_get_contents($file)
78
            )->send();
79
            $text = $response->getBody(true);
80
        } catch (RequestException $e) {
81
            $msg = sprintf(
82
                'TikaRestClient was not able to process %s. Response: %s %s.',
83
                $file,
84
                $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...
85
                $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...
86
            );
87
88
            // Only available if tika-server was started with --includeStack
89
            $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...
90
            if ($body) {
91
                $msg .= ' Body: ' . $body;
92
            }
93
94
            SS_Log::log($msg, SS_Log::NOTICE);
95
        }
96
        
97
        return $text;
98
    }
99
}
100