Passed
Push — master ( 93d7b9...7c99fc )
by Marcel
02:57 queued 12s
created

ThresholdService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 40
c 3
b 0
f 0
dl 0
loc 120
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 4 1
A floatvalue() 0 9 2
A read() 0 6 1
A readRaw() 0 3 1
B validate() 0 18 7
A __construct() 0 15 1
A create() 0 4 1
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2019-2022 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Service;
13
14
use OCA\Analytics\Db\ReportMapper;
15
use OCA\Analytics\Db\ThresholdMapper;
16
use OCA\Analytics\Notification\NotificationManager;
17
use Psr\Log\LoggerInterface;
18
use OCP\IL10N;
19
20
class ThresholdService
21
{
22
    private $logger;
23
    private $ThresholdMapper;
24
    private $ReportMapper;
25
    private $NotificationManager;
26
    private $VariableService;
27
    private $l10n;
28
29
    public function __construct(
30
        LoggerInterface $logger,
31
        ThresholdMapper $ThresholdMapper,
32
        NotificationManager $NotificationManager,
33
        ReportMapper $ReportMapper,
34
        VariableService $VariableService,
35
        IL10N $l10n
36
   )
37
    {
38
        $this->logger = $logger;
39
        $this->ThresholdMapper = $ThresholdMapper;
40
        $this->NotificationManager = $NotificationManager;
41
        $this->ReportMapper = $ReportMapper;
42
        $this->VariableService = $VariableService;
43
        $this->l10n = $l10n;
44
    }
45
46
    /**
47
     * read all thresholds for a dataset
48
     *
49
     * @param int $reportId
50
     * @return array
51
     */
52
    public function read(int $reportId)
53
    {
54
        $thresholds = $this->ThresholdMapper->getThresholdsByReport($reportId);
55
        $thresholdsParsed = $this->VariableService->replaceThresholdsVariables($thresholds);
56
57
        return $thresholdsParsed;
58
    }
59
60
    /**
61
     * read all thresholds for a dataset without any replaced text variables
62
     *
63
     * @param int $reportId
64
     * @return array
65
     */
66
    public function readRaw(int $reportId)
67
    {
68
        return $this->ThresholdMapper->getThresholdsByReport($reportId);
69
    }
70
71
    /**
72
     * create new threshold for dataset
73
     *
74
     * @param int $reportId
75
     * @param $dimension1
76
     * @param $option
77
     * @param $value
78
     * @param int $severity
79
     * @return int
80
     */
81
    public function create(int $reportId, $dimension1, $option, $value, int $severity)
82
    {
83
        $value = $this->floatvalue($value);
84
        return $this->ThresholdMapper->create($reportId, $dimension1, $value, $option, $severity);
85
    }
86
87
    private function floatvalue($val)
88
    {
89
        $val = str_replace(",", ".", $val);
90
        $val = preg_replace('/\.(?=.*\.)/', '', $val);
91
        $val = preg_replace('/[^0-9-.]+/', '', $val);
92
        if (is_numeric($val)) {
93
            return number_format(floatval($val), 2, '.', '');
94
        } else {
95
            return false;
96
        }
97
    }
98
99
    /**
100
     * Delete threshold
101
     *
102
     * @param int $thresholdId
103
     * @return bool
104
     */
105
    public function delete(int $thresholdId)
106
    {
107
        $this->ThresholdMapper->deleteThreshold($thresholdId);
108
        return true;
109
    }
110
111
    /**
112
     * validate notification thresholds per report
113
     *
114
     * @param int $reportId
115
     * @param $dimension1
116
     * @param $dimension2
117
     * @param $value
118
     * @param int $insert
119
     * @return string
120
     * @throws \Exception
121
     */
122
    public function validate(int $reportId, $dimension1, $dimension2, $value, int $insert = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $dimension2 is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

122
    public function validate(int $reportId, $dimension1, /** @scrutinizer ignore-unused */ $dimension2, $value, int $insert = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
    {
124
        $result = '';
125
        $thresholds = $this->ThresholdMapper->getSevOneThresholdsByReport($reportId);
126
        $datasetMetadata = $this->ReportMapper->read($reportId);
127
128
        foreach ($thresholds as $threshold) {
129
            if ($threshold['dimension1'] === $dimension1 or $threshold['dimension1'] === '*') {
130
                if (version_compare($value, $threshold['value'], $threshold['option'])) {
131
                    $this->NotificationManager->triggerNotification(NotificationManager::SUBJECT_THRESHOLD, $reportId, $threshold['id'], ['report' => $datasetMetadata['name'], 'subject' => $dimension1, 'rule' => $threshold['option'], 'value' => $threshold['value']], $threshold['user_id']);
132
                    $result = 'Threshold value met';
133
                } elseif ($threshold['option'] === 'new' && $insert != 0) {
134
                    $this->NotificationManager->triggerNotification(NotificationManager::SUBJECT_THRESHOLD, $reportId, $threshold['id'], ['report' => $datasetMetadata['name'], 'subject' => $dimension1, 'rule' => $this->l10n->t('new record'), 'value' => ''], $threshold['user_id']);
135
                    $result = 'Threshold value met';
136
                }
137
            }
138
        }
139
        return $result;
140
    }
141
}