Passed
Push — master ( 3660fa...998d5d )
by Marcel
03:22
created

ThresholdService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 112
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteThresholdByDataset() 0 7 2
A delete() 0 4 1
A floatvalue() 0 9 2
A read() 0 3 1
A validate() 0 16 5
A __construct() 0 11 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 2020 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Service;
13
14
use OCA\Analytics\Db\DatasetMapper;
15
use OCA\Analytics\Db\ThresholdMapper;
16
use OCA\Analytics\Notification\NotificationManager;
17
use OCP\ILogger;
18
19
class ThresholdService
20
{
21
    private $logger;
22
    private $ThresholdMapper;
23
    private $DatasetMapper;
24
    private $NotificationManager;
25
26
    public function __construct(
27
        ILogger $logger,
28
        ThresholdMapper $ThresholdMapper,
29
        NotificationManager $NotificationManager,
30
        DatasetMapper $DatasetMapper
31
    )
32
    {
33
        $this->logger = $logger;
34
        $this->ThresholdMapper = $ThresholdMapper;
35
        $this->NotificationManager = $NotificationManager;
36
        $this->DatasetMapper = $DatasetMapper;
37
    }
38
39
    /**
40
     * read all thresholds for a dataset
41
     *
42
     * @param int $datasetId
43
     * @return array
44
     */
45
    public function read(int $datasetId)
46
    {
47
        return $this->ThresholdMapper->getThresholdsByDataset($datasetId);
48
    }
49
50
    /**
51
     * create new threshold for dataset
52
     *
53
     * @param int $datasetId
54
     * @param $dimension1
55
     * @param $option
56
     * @param $value
57
     * @param int $severity
58
     * @return int
59
     */
60
    public function create(int $datasetId, $dimension1, $option, $value, int $severity)
61
    {
62
        $value = $this->floatvalue($value);
63
        return $this->ThresholdMapper->createThreshold($datasetId, $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
     * Delete threshold for dataset
92
     *
93
     * @param int $datasetId
94
     * @return bool
95
     */
96
    public function deleteThresholdByDataset(int $datasetId)
97
    {
98
        $thresholds = $this->ThresholdMapper->getThresholdsByDataset($datasetId);
99
        foreach ($thresholds as $threshold) {
100
            $this->ThresholdMapper->deleteThreshold($threshold['id']);
101
        }
102
        return true;
103
    }
104
105
    /**
106
     * validate threshold
107
     *
108
     * @param int $datasetId
109
     * @param $dimension1
110
     * @param $dimension2
111
     * @param $value
112
     * @return string
113
     * @throws \Exception
114
     */
115
    public function validate(int $datasetId, $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

115
    public function validate(int $datasetId, $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...
116
    {
117
        $result = '';
118
        $thresholds = $this->ThresholdMapper->getSevOneThresholdsByDataset($datasetId);
119
        $datasetMetadata = $this->DatasetMapper->getDatasetOptions($datasetId);
120
121
        foreach ($thresholds as $threshold) {
122
            //$this->logger->error('ThresholdController 104: ' . $threshold['value'].'==='.$threshold['option'].'==='.$value);
123
            if ($threshold['dimension1'] === $dimension1 or $threshold['dimension1'] === '*') {
124
                if (version_compare($value, $threshold['value'], $threshold['option'])) {
125
                    $this->NotificationManager->triggerNotification(NotificationManager::SUBJECT_THRESHOLD, $datasetId, $threshold['id'], ['report' => $datasetMetadata['name'], 'subject' => $dimension1, 'rule' => $threshold['option'], 'value' => $threshold['value']], $datasetMetadata['user_id']);
126
                    $result = 'Threshold value met';
127
                }
128
            }
129
        }
130
        return $result;
131
    }
132
}