Passed
Push — master ( 3334e1...093f01 )
by Marcel
02:14
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);
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
        $stmt = $this->db->prepare($SQL);
128
        $stmt->execute(array($id));
129
        return $stmt->fetch();
130
    }
131
132
    /**
133
     * truncates fiels do DB-field size
134
     *
135
     * @param $string
136
     * @param $length
137
     * @param $dots
138
     * @return string
139
     */
140
    private function truncate($string, $length, $dots = "...")
141
    {
142
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
143
    }
144
}
145