Passed
Push — master ( 5ce0f4...742c10 )
by Marcel
02:27
created

ThresholdService::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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
19
class ThresholdService
20
{
21
    private $logger;
22
    private $ThresholdMapper;
23
    private $ReportMapper;
24
    private $NotificationManager;
25
    private $VariableService;
26
27
    public function __construct(
28
        LoggerInterface $logger,
29
        ThresholdMapper $ThresholdMapper,
30
        NotificationManager $NotificationManager,
31
        ReportMapper $ReportMapper,
32
        VariableService $VariableService
33
    )
34
    {
35
        $this->logger = $logger;
36
        $this->ThresholdMapper = $ThresholdMapper;
37
        $this->NotificationManager = $NotificationManager;
38
        $this->ReportMapper = $ReportMapper;
39
        $this->VariableService = $VariableService;
40
    }
41
42
    /**
43
     * read all thresholds for a dataset
44
     *
45
     * @param int $reportId
46
     * @return array
47
     */
48
    public function read(int $reportId)
49
    {
50
        $thresholds = $this->ThresholdMapper->getThresholdsByReport($reportId);
51
        $thresholdsParsed = $this->VariableService->replaceThresholdsVariables($thresholds);
52
53
        return $thresholdsParsed;
54
    }
55
56
    /**
57
     * read all thresholds for a dataset without any replaced text variables
58
     *
59
     * @param int $reportId
60
     * @return array
61
     */
62
    public function readRaw(int $reportId)
63
    {
64
        return $this->ThresholdMapper->getThresholdsByReport($reportId);
65
    }
66
67
    /**
68
     * create new threshold for dataset
69
     *
70
     * @param int $reportId
71
     * @param $dimension1
72
     * @param $option
73
     * @param $value
74
     * @param int $severity
75
     * @return int
76
     */
77
    public function create(int $reportId, $dimension1, $option, $value, int $severity)
78
    {
79
        $value = $this->floatvalue($value);
80
        return $this->ThresholdMapper->create($reportId, $dimension1, $value, $option, $severity);
81
    }
82
83
    private function floatvalue($val)
84
    {
85
        $val = str_replace(",", ".", $val);
86
        $val = preg_replace('/\.(?=.*\.)/', '', $val);
87
        $val = preg_replace('/[^0-9-.]+/', '', $val);
88
        if (is_numeric($val)) {
89
            return number_format(floatval($val), 2, '.', '');
90
        } else {
91
            return false;
92
        }
93
    }
94
95
    /**
96
     * Delete threshold
97
     *
98
     * @param int $thresholdId
99
     * @return bool
100
     */
101
    public function delete(int $thresholdId)
102
    {
103
        $this->ThresholdMapper->deleteThreshold($thresholdId);
104
        return true;
105
    }
106
107
    /**
108
     * validate threshold per report
109
     *
110
     * @param int $reportId
111
     * @param $dimension1
112
     * @param $dimension2
113
     * @param $value
114
     * @return string
115
     * @throws \Exception
116
     */
117
    public function validate(int $reportId, $dimension1, $dimension2, $value)
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

117
    public function validate(int $reportId, $dimension1, /** @scrutinizer ignore-unused */ $dimension2, $value)

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...
118
    {
119
        $result = '';
120
        $thresholds = $this->ThresholdMapper->getSevOneThresholdsByReport($reportId);
121
        $datasetMetadata = $this->ReportMapper->read($reportId);
122
123
        foreach ($thresholds as $threshold) {
124
            if ($threshold['dimension1'] === $dimension1 or $threshold['dimension1'] === '*') {
125
                if (version_compare($value, $threshold['value'], $threshold['option'])) {
126
                    $this->NotificationManager->triggerNotification(NotificationManager::SUBJECT_THRESHOLD, $reportId, $threshold['id'], ['report' => $datasetMetadata['name'], 'subject' => $dimension1, 'rule' => $threshold['option'], 'value' => $threshold['value']], $threshold['user_id']);
127
                    $result = 'Threshold value met';
128
                }
129
            }
130
        }
131
        return $result;
132
    }
133
}