Completed
Pull Request — master (#93)
by Janis
04:19
created

RedisUtil   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setupRedisInstance() 0 18 4
1
<?php
2
3
/**
4
 * Nextcloud - OCR
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Janis Koehr <[email protected]>
9
 * @copyright Janis Koehr 2017
10
 */
11
namespace OCA\Ocr\Util;
12
13
use OCP\ILogger;
14
use OCP\IL10N;
15
use OCA\Ocr\Service\NotFoundException;
16
use OCA\Ocr\Constants\OcrConstants;
17
use OCP\IConfig;
18
19
/**
20
 * Class RedisUtil
21
 *
22
 * @package OCA\Ocr\Util
23
 */
24
class RedisUtil {
25
    /**
26
     *
27
     * @var IL10N
28
     */
29
    private $l10n;
30
    
31
    /**
32
     * 
33
     * @var ILogger
34
     */
35
    private $logger;
36
    
37
    /**
38
     * 
39
     * @var IConfig
40
     */
41
    private $config;
42
    
43
    /**
44
     *
45
     * @var string
46
     */
47
    private $appName = 'ocr';
48
 
49
    /**
50
     * 
51
     * @param IL10N $l10n
52
     * @param ILogger $logger
53
     * @param IConfig $config
54
     */
55 6
    public function __construct(IL10N $l10n, ILogger $logger, IConfig $config) {
56 6
        $this->l10n = $l10n;
57 6
        $this->logger = $logger;
58 6
        $this->config = $config;
59 6
    }
60
    
61
    /**
62
     * Setup the Redis instance and return to whom ever it needs.
63
     * @codeCoverageIgnore
64
     * 
65
     * @throws NotFoundException
66
     * @return \Redis
67
     */
68
    public function setupRedisInstance() {
69
        if (!extension_loaded('redis')) {
70
            $this->logger->debug(
71
                    'It seems that the message queueing capabilities are not available in your local php installation. Please install php-redis.');
72
            throw new NotFoundException($this->l10n->t('Message queueing capabilities are missing on the server.'));
73
        }
74
        $redis = new \Redis();
75
        if (!$redis->connect($this->config->getAppValue($this->appName, 'redisHost'), intval($this->config->getAppValue($this->appName, 'redisPort')), 2.5, NULL, 100)) {
76
            $this->logger->debug('Cannot connect to Redis.');
77
            throw new NotFoundException($this->l10n->t('Cannot connect to Redis.'));
78
        }
79
        if (!$redis->select(intval($this->config->getAppValue($this->appName, 'redisDb')))) {
80
            $this->logger->debug('Cannot connect to the right Redis database.');
81
            throw new NotFoundException($this->l10n->t('Cannot connect to the right Redis database.'));
82
        }
83
        $redis->setOption(\Redis::OPT_PREFIX, OcrConstants::REDIS_KEY_PREFIX);
84
        return $redis;
85
    }
86
}