Passed
Push — master ( 544106...3ee5a5 )
by Marcel
02:27
created

DatasetMapper::updateDataset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 11
dl 0
loc 7
rs 10
c 1
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Db;
13
14
use OCP\IDbConnection;
15
use OCP\IL10N;
16
use OCP\ILogger;
17
18
class DatasetMapper
19
{
20
    private $userId;
21
    private $l10n;
22
    private $db;
23
    private $logger;
24
25
    public function __construct(
26
        $userId,
27
        IL10N $l10n,
28
        IDbConnection $db,
29
        ILogger $logger
30
    )
31
    {
32
        $this->userId = $userId;
33
        $this->l10n = $l10n;
34
        $this->db = $db;
35
        $this->logger = $logger;
36
    }
37
38
    /**
39
     * create dataset
40
     */
41
    public function createDataset()
42
    {
43
        $SQL = 'INSERT INTO `*PREFIX*analytics_dataset` (`user_id`,`name`,`type`,`parent`,`dimension1`,`dimension2`,`dimension3`) VALUES(?,?,?,?,?,?,?)';
44
        //$this->logger->error($SQL);
45
46
        $stmt = $this->db->prepare($SQL);
47
        $stmt->execute(array($this->userId, $this->l10n->t('New'), 2, 0, $this->l10n->t('Object'), $this->l10n->t('Date'), $this->l10n->t('Value')));
48
        $insertid = $this->db->lastInsertId('*PREFIX*analytics_dataset');
49
        return $insertid;
50
    }
51
52
    /**
53
     * update dataset
54
     * @param $id
55
     * @param $name
56
     * @param $subheader
57
     * @param $parent
58
     * @param $type
59
     * @param $link
60
     * @param $visualization
61
     * @param $chart
62
     * @param $dimension1
63
     * @param $dimension2
64
     * @param $dimension3
65
     * @return bool
66
     */
67
    public function updateDataset($id, $name, $subheader, $parent, $type, $link, $visualization, $chart, $dimension1, $dimension2, $dimension3)
68
    {
69
        $SQL = 'UPDATE `*PREFIX*analytics_dataset` SET `name`= ?, `subheader`= ?, `type`= ?, `link`= ?, `visualization`= ?, `chart`= ?, `parent`= ?, `dimension1` = ?, `dimension2` = ?, `dimension3` = ? WHERE `user_id` = ? AND `id` = ?';
70
        $stmt = $this->db->prepare($SQL);
71
        $name = $this->truncate($name, 64);
0 ignored issues
show
Bug introduced by
The method truncate() does not exist on OCA\Analytics\Db\DatasetMapper. ( Ignorable by Annotation )

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

71
        /** @scrutinizer ignore-call */ 
72
        $name = $this->truncate($name, 64);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
        $stmt->execute(array($name, $subheader, $type, $link, $visualization, $chart, $parent, $dimension1, $dimension2, $dimension3, $this->userId, $id));
73
        return true;
74
    }
75
76
    /**
77
     * delete dataset
78
     * @param $id
79
     * @return
80
     */
81
    public function deleteDataset($id)
82
    {
83
        $SQL = 'DELETE FROM `*PREFIX*analytics_dataset` WHERE `user_id` = ? AND `id` = ?';
84
        $stmt = $this->db->prepare($SQL);
85
        $stmt->execute(array($this->userId, $id));
86
        return true;
87
    }
88
89
    /**
90
     * get datasets
91
     */
92
    public function getDatasets()
93
    {
94
        $SQL = 'SELECT id, name, type, parent FROM `*PREFIX*analytics_dataset` WHERE  `user_id` = ? ORDER BY `parent` ASC, `name` ASC';
95
        //$this->logger->error($SQL);
96
97
        $stmt = $this->db->prepare($SQL);
98
        $stmt->execute(array($this->userId));
99
        return $stmt->fetchAll();
100
    }
101
102
    /**
103
     * get datasets
104
     * @param int $id
105
     * @param string $user_id
106
     * @return
107
     */
108
    public function getOwnDataset($id, string $user_id = null)
109
    {
110
        $SQL = 'SELECT * FROM `*PREFIX*analytics_dataset` WHERE `id` = ? AND `user_id` = ?';
111
        //$this->logger->error($SQL);
112
        $stmt = $this->db->prepare($SQL);
113
        if ($user_id) $this->userId = $user_id;
114
        $stmt->execute(array($id, $this->userId));
115
        return $stmt->fetch();
116
    }
117
118
    /**
119
     * get datasets
120
     * @param $id
121
     * @return
122
     */
123
    public function getDatasetOptions($id)
124
    {
125
        $SQL = 'SELECT `name`, `visualization`, `chart` FROM `*PREFIX*analytics_dataset` WHERE `id` = ?';
126
        //$this->logger->error($SQL);
127
128
        $stmt = $this->db->prepare($SQL);
129
        $stmt->execute(array($id));
130
        return $stmt->fetch();
131
    }
132
}
133