Completed
Push — master ( 45d3c5...af8085 )
by
unknown
07:27
created

RedisUtil   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 11.54 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 10
c 3
b 1
f 0
lcom 1
cbo 1
dl 9
loc 78
ccs 5
cts 5
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B setupRedisInstance() 9 32 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Nextcloud - OCR
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later.
7
 * See the COPYING file.
8
 * 
9
 * @author Janis Koehr <[email protected]>
10
 * @copyright Janis Koehr 2017
11
 */
12
namespace OCA\Ocr\Util;
13
14
use OCP\ILogger;
15
use OCP\IL10N;
16
use OCA\Ocr\Service\NotFoundException;
17
use OCA\Ocr\Constants\OcrConstants;
18
use OCP\IConfig;
19
20
21
/**
22
 * Class RedisUtil
23
 * 
24
 * @package OCA\Ocr\Util
25
 */
26
class RedisUtil {
27
28
    /**
29
     *
30
     * @var IL10N
31
     */
32
    private $l10n;
33
34
    /**
35
     *
36
     * @var ILogger
37
     */
38
    private $logger;
39
40
    /**
41
     *
42
     * @var IConfig
43
     */
44
    private $config;
45
46
    /**
47
     *
48
     * @var string
49
     */
50
    private $appName = 'ocr';
51
52
    /**
53
     *
54
     * @param IL10N $l10n            
55
     * @param ILogger $logger            
56
     * @param IConfig $config            
57
     */
58 7
    public function __construct(IL10N $l10n, ILogger $logger, IConfig $config) {
59 7
        $this->l10n = $l10n;
60 7
        $this->logger = $logger;
61 7
        $this->config = $config;
62 7
    }
63
64
    /**
65
     * Setup the Redis instance and return to whom ever it needs.
66
     * @codeCoverageIgnore
67
     * 
68
     * @throws NotFoundException
69
     * @return \Redis
70
     */
71
    public function setupRedisInstance() {
72
        try {
73 View Code Duplication
        if (!extension_loaded('redis')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
            $this->logger->debug(
75
                    'It seems that the message queueing capabilities are not available in your local php installation. Please install php-redis.', ['app' => OcrConstants::APP_NAME]);
76
            throw new NotFoundException($this->l10n->t('Message queueing capabilities are missing on the server (package php-redis).'));
77
        }
78
        $redis = new \Redis();
79
        if (!$redis->connect($this->config->getAppValue($this->appName, 'redisHost'), 
80
                intval($this->config->getAppValue($this->appName, 'redisPort')), 2.5, null, 100)) {
81
                    $this->logger->debug('Cannot connect to Redis.', ['app' => OcrConstants::APP_NAME]);
82
            throw new NotFoundException($this->l10n->t('Cannot connect to Redis.'));
83
        }
84
        $password = $this->config->getAppValue($this->appName, 'redisPassword', '');
85
        if($password !== '') {
86
            $authenticated = $redis->auth($password);
87
        }
88 View Code Duplication
        if ($password !== '' && !$authenticated) {
0 ignored issues
show
Bug introduced by
The variable $authenticated does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
            $this->logger->debug('Redis authentication error.', ['app' => OcrConstants::APP_NAME]);
90
            throw new NotFoundException($this->l10n->t('Redis authentication error.'));
91
        }
92
        if (!$redis->select(intval($this->config->getAppValue($this->appName, 'redisDb')))) {
93
            throw new NotFoundException($this->l10n->t('Cannot connect to the right Redis database.'));
94
        }
95
        $redis->setOption(\Redis::OPT_PREFIX, OcrConstants::REDIS_KEY_PREFIX);
96
        return $redis;
97
        } catch (\RedisException $e) {
98
            if($e->getMessage() === 'Failed to AUTH connection') {
99
                throw new NotFoundException($this->l10n->t('Redis authentication error.'));
100
            }
101
        }
102
    }
103
}