Passed
Push — master ( ffe1e4...544106 )
by Marcel
05:27
created

DataloadMapper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 108
rs 10
c 1
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A update() 0 7 1
A read() 0 6 1
A truncate() 0 3 2
A create() 0 6 1
A delete() 0 6 1
A getDataloadById() 0 6 1
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 DataloadMapper
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 a new dataload
40
     *
41
     * @NoAdminRequired
42
     * @param int $datasetId
43
     * @param int $datasourceId
44
     * @return integer
45
     */
46
    public function create(int $datasetId, int $datasourceId)
47
    {
48
        $SQL = 'INSERT INTO `*PREFIX*analytics_dataload` (`user_id`,`name`,`dataset`,`datasource`,`option`) VALUES(?,?,?,?,?)';
49
        $stmt = $this->db->prepare($SQL);
50
        $stmt->execute(array($this->userId, 'New', $datasetId, $datasourceId, '{}'));
51
        return $this->db->lastInsertId('*PREFIX*analytics_dataload');
52
    }
53
54
    /**
55
     * get all dataloads for a dataset
56
     *
57
     * @NoAdminRequired
58
     * @param int $datasetId
59
     * @return array
60
     */
61
    public function read(int $datasetId)
62
    {
63
        $SQL = 'SELECT * FROM `*PREFIX*analytics_dataload` WHERE `user_id` = ? AND `dataset` = ?';
64
        $stmt = $this->db->prepare($SQL);
65
        $stmt->execute(array($this->userId, $datasetId));
66
        return $stmt->fetchAll();
67
    }
68
69
    /**
70
     * update dataload
71
     *
72
     * @NoAdminRequired
73
     * @param int $dataloadId
74
     * @param $name
75
     * @param $option
76
     * @return bool
77
     */
78
    public function update(int $dataloadId, $name, $option)
79
    {
80
        $SQL = 'UPDATE `*PREFIX*analytics_dataload` SET `name`= ?, `option`= ? WHERE `user_id` = ? AND `id` = ?';
81
        $stmt = $this->db->prepare($SQL);
82
        $name = $this->truncate($name, 64);
83
        $stmt->execute(array($name, $option, $this->userId, $dataloadId));
84
        return true;
85
    }
86
87
    /**
88
     * delete a dataload
89
     *
90
     * @NoAdminRequired
91
     * @param int $dataloadId
92
     * @return bool
93
     */
94
    public function delete(int $dataloadId)
95
    {
96
        $SQL = 'DELETE FROM `*PREFIX*analytics_dataload` WHERE `id` = ? AND `user_id` = ?';
97
        $stmt = $this->db->prepare($SQL);
98
        $stmt->execute([$dataloadId, $this->userId]);
99
        return true;
100
    }
101
102
    /**
103
     * get Dataload by id
104
     * @param int $dataloadId
105
     * @return array
106
     */
107
    public function getDataloadById(int $dataloadId)
108
    {
109
        $SQL = 'SELECT * FROM `*PREFIX*analytics_dataload` WHERE `id` = ?';
110
        $stmt = $this->db->prepare($SQL);
111
        $stmt->execute(array($dataloadId));
112
        return $stmt->fetch();
113
    }
114
115
    /**
116
     * truncates fiels do DB-field size
117
     *
118
     * @param $string
119
     * @param $length
120
     * @param $dots
121
     * @return string
122
     */
123
    private function truncate($string, $length, $dots = "...")
124
    {
125
        return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string;
126
    }
127
}
128