Passed
Push — master ( 96481a...673777 )
by Marcel
02:28
created

ThresholdController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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
     * update threshold
82
     *
83
     * @NoAdminRequired
84
     * @param int $datasetId
85
     * @param int $thresholdId
86
     * @return bool
87
     */
88
    public function update(int $datasetId, int $thresholdId)
0 ignored issues
show
Unused Code introduced by
The parameter $thresholdId 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

88
    public function update(int $datasetId, /** @scrutinizer ignore-unused */ int $thresholdId)

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...
89
    {
90
        return $this->DBController->updateDataset($datasetId);
0 ignored issues
show
Bug introduced by
The call to OCA\Analytics\Controller...roller::updateDataset() has too few arguments starting with name. ( Ignorable by Annotation )

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

90
        return $this->DBController->/** @scrutinizer ignore-call */ updateDataset($datasetId);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
91
    }
92
93
    /**
94
     * validate threshold
95
     *
96
     * @NoAdminRequired
97
     * @param int $datasetId
98
     * @param $dimension1
99
     * @param $dimension2
100
     * @param $dimension3
101
     * @return string
102
     * @throws \Exception
103
     */
104
    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

104
    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...
105
    {
106
        $thresholds = array();
107
        array_push($thresholds, ['dimension1' => 'a', 'option' => '<', 'value' => '10']);
108
        array_push($thresholds, ['dimension1' => 'b', 'option' => '<', 'value' => '10']);
109
        $thresholds = $this->DBController->getSevOneThresholdsByDataset($datasetId);
110
111
        $result = '';
112
        $datasetMetadata = $this->DBController->getOwnDataset($datasetId);
113
114
        foreach ($thresholds as $threshold) {
115
            //$this->logger->error('ThresholdController 108: ' . json_encode($threshold));
116
            if ($threshold['dimension1'] === $dimension1) {
117
                if (version_compare($dimension3, $threshold['dimension3'], $threshold['option'])) {
118
                    $this->NotificationManager->triggerNotification(NotificationManager::OBJECT_DATASET, $datasetId, NotificationManager::SUBJECT_THRESHOLD, ['report' => $datasetMetadata['name'], 'subject' => $dimension1, 'rule' => $threshold['option'], 'value' => $threshold['dimension3']]);
119
                    $result = 'Threshold value met';
120
                }
121
            }
122
        }
123
        return $result;
124
    }
125
126
}