Issues (496)

lib/Db/ThresholdMapper.php (3 issues)

Labels
Severity
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\Db;
10
11
use OCP\DB\Exception;
0 ignored issues
show
The type OCP\DB\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use OCP\IDBConnection;
0 ignored issues
show
The type OCP\IDBConnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Psr\Log\LoggerInterface;
0 ignored issues
show
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class ThresholdMapper
16
{
17
    private $userId;
18
    private $db;
19
    private $logger;
20
    const TABLE_NAME = 'analytics_threshold';
21
22
    public function __construct(
23
        $userId,
24
        IDBConnection $db,
25
        LoggerInterface $logger
26
    )
27
    {
28
        $this->userId = $userId;
29
        $this->db = $db;
30
        $this->logger = $logger;
31
    }
32
33
    /**
34
     * @throws Exception
35
     */
36
    public function create($reportId, $dimension1, $value, $option, $severity)
37
    {
38
        $sql = $this->db->getQueryBuilder();
39
        $sql->insert(self::TABLE_NAME)
40
            ->values([
41
                'user_id' => $sql->createNamedParameter($this->userId),
42
                'report' => $sql->createNamedParameter($reportId),
43
                'dimension1' => $sql->createNamedParameter($dimension1),
44
                'value' => $sql->createNamedParameter($value),
45
                'option' => $sql->createNamedParameter($option),
46
                'severity' => $sql->createNamedParameter($severity),
47
            ]);
48
        $sql->executeStatement();
49
        return (int)$sql->getLastInsertId();
50
    }
51
52
    /**
53
     * @throws Exception
54
     */
55
    public function getThresholdsByReport($reportId)
56
    {
57
        $sql = $this->db->getQueryBuilder();
58
        $sql->from(self::TABLE_NAME)
59
            ->select('id')
60
            ->addSelect('dimension1')
61
            ->addSelect('dimension2')
62
            ->addSelect('value')
63
            ->addSelect('option')
64
            ->addSelect('severity')
65
            ->addSelect('user_id')
66
            ->where($sql->expr()->eq('report', $sql->createNamedParameter($reportId)))
67
            ->andWhere($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
68
        ;
69
        $statement = $sql->executeQuery();
70
        $result = $statement->fetchAll();
71
        $statement->closeCursor();
72
        return $result;
73
    }
74
75
    /**
76
     * @throws Exception
77
     */
78
    public function getSevOneThresholdsByReport($reportId)
79
    {
80
        $sql = $this->db->getQueryBuilder();
81
        $sql->from(self::TABLE_NAME)
82
            ->select('*')
83
            ->where($sql->expr()->eq('report', $sql->createNamedParameter($reportId)))
84
            ->andWhere($sql->expr()->eq('severity', $sql->createNamedParameter('1')));
85
        $statement = $sql->executeQuery();
86
        $result = $statement->fetchAll();
87
        $statement->closeCursor();
88
        return $result;
89
    }
90
91
    /**
92
     * @throws Exception
93
     */
94
    public function deleteThreshold($thresholdId)
95
    {
96
        $sql = $this->db->getQueryBuilder();
97
        $sql->delete(self::TABLE_NAME)
98
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($thresholdId)))
99
            ->andWhere($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)));
100
        $sql->executeStatement();
101
        return true;
102
    }
103
104
    /**
105
     * @throws Exception
106
     */
107
    public function deleteThresholdByReport($reportId)
108
    {
109
        $sql = $this->db->getQueryBuilder();
110
        $sql->delete(self::TABLE_NAME)
111
            ->where($sql->expr()->eq('report', $sql->createNamedParameter($reportId)));
112
        $sql->executeStatement();
113
        return true;
114
    }
115
}