Passed
Push — master ( 6372a5...370f15 )
by Marcel
02:54
created

ThresholdService::create()   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 5
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 2021 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
26
    public function __construct(
27
        LoggerInterface $logger,
28
        ThresholdMapper $ThresholdMapper,
29
        NotificationManager $NotificationManager,
30
        ReportMapper $ReportMapper
31
    )
32
    {
33
        $this->logger = $logger;
34
        $this->ThresholdMapper = $ThresholdMapper;
35
        $this->NotificationManager = $NotificationManager;
36
        $this->ReportMapper = $ReportMapper;
37
    }
38
39
    /**
40
     * read all thresholds for a dataset
41
     *
42
     * @param int $reportId
43
     * @return array
44
     */
45
    public function read(int $reportId)
46
    {
47
        return $this->ThresholdMapper->getThresholdsByReport($reportId);
48
    }
49
50
    /**
51
     * create new threshold for dataset
52
     *
53
     * @param int $reportId
54
     * @param $dimension1
55
     * @param $option
56
     * @param $value
57
     * @param int $severity
58
     * @return int
59
     */
60
    public function create(int $reportId, $dimension1, $option, $value, int $severity)
61
    {
62
        $value = $this->floatvalue($value);
63
        return $this->ThresholdMapper->createThreshold($reportId, $dimension1, $value, $option, $severity);
64
    }
65
66
    private function floatvalue($val)
67
    {
68
        $val = str_replace(",", ".", $val);
69
        $val = preg_replace('/\.(?=.*\.)/', '', $val);
70
        $val = preg_replace('/[^0-9-.]+/', '', $val);
71
        if (is_numeric($val)) {
72
            return number_format(floatval($val), 2, '.', '');
73
        } else {
74
            return false;
75
        }
76
    }
77
78
    /**
79
     * Delete threshold
80
     *
81
     * @param int $thresholdId
82
     * @return bool
83
     */
84
    public function delete(int $thresholdId)
85
    {
86
        $this->ThresholdMapper->deleteThreshold($thresholdId);
87
        return true;
88
    }
89
90
    /**
91
     * validate threshold
92
     *
93
     * @param int $reportId
94
     * @param $dimension1
95
     * @param $dimension2
96
     * @param $value
97
     * @return string
98
     * @throws \Exception
99
     */
100
    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

100
    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...
101
    {
102
        $result = '';
103
        $thresholds = $this->ThresholdMapper->getSevOneThresholdsByReport($reportId);
104
        $datasetMetadata = $this->ReportMapper->read($reportId);
105
106
        foreach ($thresholds as $threshold) {
107
            if ($threshold['dimension1'] === $dimension1 or $threshold['dimension1'] === '*') {
108
                if (version_compare($value, $threshold['value'], $threshold['option'])) {
109
                    $this->NotificationManager->triggerNotification(NotificationManager::SUBJECT_THRESHOLD, $reportId, $threshold['id'], ['report' => $datasetMetadata['name'], 'subject' => $dimension1, 'rule' => $threshold['option'], 'value' => $threshold['value']], $datasetMetadata['user_id']);
110
                    $result = 'Threshold value met';
111
                }
112
            }
113
        }
114
        return $result;
115
    }
116
}