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

PHPUtil::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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 OCA\Ocr\Service\NotFoundException;
14
use OCP\IL10N;
15
use OCP\ILogger;
16
17
18
/**
19
 * Class PHPUtil
20
 * 
21
 * @package OCA\Ocr\Util
22
 */
23
class PHPUtil {
24
25
    /**
26
     *
27
     * @var ILogger
28
     */
29
    private $logger;
30
31
    /**
32
     *
33
     * @var IL10N
34
     */
35
    private $l10n;
36
37 5
    public function __construct(IL10N $l10n, ILogger $logger) {
38 5
        $this->l10n = $l10n;
39 5
        $this->logger = $logger;
40 5
    }
41
42
    /**
43
     * Wraps the php native function tempnam()
44
     * @codeCoverageIgnore
45
     * 
46
     * @param string $dir            
47
     * @param string $prefix            
48
     * @throws NotFoundException
49
     * @return string
50
     */
51
    public function tempnamWrapper($dir, $prefix) {
52
        $tempFile = tempnam($dir, $prefix);
53
        if ($tempFile === false) {
54
            throw new NotFoundException($this->l10n->t('Temp file cannot be created.'));
55
        } else {
56
            return $tempFile;
57
        }
58
    }
59
60
    /**
61
     * Wraps the php native function unlink()
62
     * @codeCoverageIgnore
63
     * 
64
     * @param string $fileName            
65
     * @throws NotFoundException
66
     * @return boolean
67
     */
68
    public function unlinkWrapper($fileName) {
69
        if (unlink($fileName)) {
70
            return true;
71
        } else {
72
            throw new NotFoundException(
73
                    $this->l10n->t('Cannot delete temporary file during temp file creation for tesseract.'));
74
        }
75
    }
76
77
    /**
78
     * Wraps the php native function touch()
79
     * @codeCoverageIgnore
80
     * 
81
     * @param string $fileName            
82
     * @throws NotFoundException
83
     * @return boolean
84
     */
85
    public function touchWrapper($fileName) {
86
        if (touch($fileName)) {
87
            return true;
88
        } else {
89
            throw new NotFoundException($this->l10n->t('Cannot create temporary file for tesseract.'));
90
        }
91
    }
92
93
    /**
94
     * Wraps the php native function chmod()
95
     * @codeCoverageIgnore
96
     * 
97
     * @param string $fileName            
98
     * @param integer $mode            
99
     * @throws NotFoundException
100
     * @return boolean
101
     */
102
    public function chmodWrapper($fileName, $mode) {
103
        if (chmod($fileName, $mode)) {
104
            return true;
105
        } else {
106
            throw new NotFoundException($this->l10n->t('Cannot set permissions to temporary file for tesseract.'));
107
        }
108
    }
109
}