Passed
Push — master ( 673777...5c76cd )
by Marcel
02:26
created

ThresholdController::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 2
b 0
f 0
nc 4
nop 4
dl 0
loc 16
rs 9.9666
1
<?php
2
/**
3
 * Data 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 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Controller;
13
14
use OCA\Analytics\Notification\NotificationManager;
15
use OCP\AppFramework\Controller;
16
use OCP\ILogger;
17
use OCP\IRequest;
18
19
class ThresholdController extends Controller
20
{
21
    private $logger;
22
    private $DBController;
23
    private $NotificationManager;
24
25
    public function __construct(
26
        $appName,
27
        IRequest $request,
28
        ILogger $logger,
29
        DbController $DBController,
30
        NotificationManager $NotificationManager
31
    )
32
    {
33
        parent::__construct($appName, $request);
34
        $this->logger = $logger;
35
        $this->DBController = $DBController;
36
        $this->NotificationManager = $NotificationManager;
37
    }
38
39
    /**
40
     * read all thresholds for a dataset
41
     *
42
     * @NoAdminRequired
43
     * @param int $datasetId
44
     * @return array
45
     */
46
    public function read(int $datasetId)
47
    {
48
        return $this->DBController->getThresholdsByDataset($datasetId);
49
    }
50
51
    /**
52
     * create new threshold for dataset
53
     *
54
     * @NoAdminRequired
55
     * @param int $datasetId
56
     * @param $dimension1
57
     * @param $option
58
     * @param $dimension3
59
     * @param int $severity
60
     * @return int
61
     */
62
    public function create(int $datasetId, $dimension1, $option, $dimension3, int $severity)
63
    {
64
        return $this->DBController->createThreshold($datasetId, $dimension1, $dimension3, $option, $severity);
65
    }
66
67
    /**
68
     * Delete threshold for dataset
69
     *
70
     * @NoAdminRequired
71
     * @param int $thresholdId
72
     * @return bool
73
     */
74
    public function delete(int $thresholdId)
75
    {
76
        $this->DBController->deleteThreshold($thresholdId);
77
        return true;
78
    }
79
80
    /**
81
     * validate threshold
82
     *
83
     * @NoAdminRequired
84
     * @param int $datasetId
85
     * @param $dimension1
86
     * @param $dimension2
87
     * @param $dimension3
88
     * @return string
89
     * @throws \Exception
90
     */
91
    public function validate(int $datasetId, $dimension1, $dimension2, $dimension3)
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

91
    public function validate(int $datasetId, $dimension1, /** @scrutinizer ignore-unused */ $dimension2, $dimension3)

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...
92
    {
93
        $result = '';
94
        $thresholds = $this->DBController->getSevOneThresholdsByDataset($datasetId);
95
        $datasetMetadata = $this->DBController->getOwnDataset($datasetId);
96
97
        foreach ($thresholds as $threshold) {
98
            //$this->logger->error('ThresholdController 108: ' . json_encode($threshold));
99
            if ($threshold['dimension1'] === $dimension1) {
100
                if (version_compare($dimension3, $threshold['dimension3'], $threshold['option'])) {
101
                    $this->NotificationManager->triggerNotification(NotificationManager::OBJECT_DATASET, $datasetId, NotificationManager::SUBJECT_THRESHOLD, ['report' => $datasetMetadata['name'], 'subject' => $dimension1, 'rule' => $threshold['option'], 'value' => $threshold['dimension3']]);
102
                    $result = 'Threshold value met';
103
                }
104
            }
105
        }
106
        return $result;
107
    }
108
}