Completed
Pull Request — master (#114)
by Janis
04:26
created

RedisUtil::setupRedisInstance()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 18

Duplication

Lines 8
Ratio 34.78 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 23
ccs 0
cts 0
cp 0
rs 8.5906
cc 5
eloc 18
nc 5
nop 0
crap 30
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
        if (!extension_loaded('redis')) {
73
            $this->logger->debug(
74
                    'It seems that the message queueing capabilities are not available in your local php installation. Please install php-redis.');
75
            throw new NotFoundException($this->l10n->t('Message queueing capabilities are missing on the server (package php-redis).'));
76
        }
77
        $redis = new \Redis();
78
        if (!$redis->connect($this->config->getAppValue($this->appName, 'redisHost'), 
79
                intval($this->config->getAppValue($this->appName, 'redisPort')), 2.5, null, 100)) {
80
            $this->logger->debug('Cannot connect to Redis.');
81
            throw new NotFoundException($this->l10n->t('Cannot connect to Redis.'));
82
        }
83 View Code Duplication
        if (!$redis->auth($this->config->getAppValue($this->appName, 'redisPassword', ''))) {
84
            $this->logger->debug('Redis authentication error.');
85
            throw new NotFoundException($this->l10n->t('Redis authentication error.'));
86
        }
87 View Code Duplication
        if (!$redis->select(intval($this->config->getAppValue($this->appName, 'redisDb')))) {
88
            $this->logger->debug('Cannot connect to the right Redis database.');
89
            throw new NotFoundException($this->l10n->t('Cannot connect to the right Redis database.'));
90
        }
91
        $redis->setOption(\Redis::OPT_PREFIX, OcrConstants::REDIS_KEY_PREFIX);
92
        return $redis;
93
    }
94
}