Passed
Push — master ( 222752...4454ce )
by Marcel
02:38
created

DatasetController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 41
c 1
b 1
f 0
dl 0
loc 134
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A getOwnDataset() 0 3 1
A update() 0 6 2
A create() 0 25 3
A __construct() 0 13 1
A read() 0 3 1
A delete() 0 7 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\Controller;
13
14
//use OCA\Analytics\Activity\ActivityManager;
15
use OCP\AppFramework\Controller;
16
use OCP\AppFramework\Http\DataResponse;
17
use OCP\ILogger;
18
use OCP\IRequest;
19
20
class DatasetController extends Controller
21
{
22
    private $logger;
23
    private $ShareController;
24
    private $DBController;
25
    private $ActivityManager;
0 ignored issues
show
introduced by
The private property $ActivityManager is not used, and could be removed.
Loading history...
26
27
    public function __construct(
28
        $appName,
29
        IRequest $request,
30
        ILogger $logger,
31
        ShareController $ShareController,
32
        DbController $DBController
33
        //ActivityManager $ActivityManager
34
    )
35
    {
36
        parent::__construct($appName, $request);
37
        $this->logger = $logger;
38
        $this->ShareController = $ShareController;
39
        $this->DBController = $DBController;
40
        //$this->ActivityManager = $ActivityManager;
41
    }
42
43
    /**
44
     * get all datasets
45
     *
46
     * @NoAdminRequired
47
     */
48
    public function index()
49
    {
50
        $ownDatasets = $this->DBController->getDatasets();
51
        $sharedDatasets = $this->ShareController->getSharedDatasets();
52
        $ownDatasets = array_merge($ownDatasets, $sharedDatasets);
53
        return new DataResponse($ownDatasets);
54
    }
55
56
    /**
57
     * get own dataset details
58
     *
59
     * @NoAdminRequired
60
     * @param int $datasetId
61
     * @return array
62
     */
63
    public function read(int $datasetId)
64
    {
65
        return $this->DBController->getOwnDataset($datasetId);
66
    }
67
68
    /**
69
     * get own dataset details
70
     *
71
     * @NoAdminRequired
72
     * @param int $datasetId
73
     * @return array
74
     */
75
    public function getOwnDataset(int $datasetId)
76
    {
77
        return $this->read($datasetId);
78
    }
79
80
    /**
81
     * create new dataset
82
     *
83
     * @NoAdminRequired
84
     * @param string $file
85
     * @param string $link
86
     * @return int
87
     */
88
    public function create($file = '', $link = '')
89
    {
90
        //$this->logger->error('datasetcontroller 82: '.$file);
91
        //$this->ActivityManager->triggerEvent(0, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_ADD);
92
        $datasetId = $this->DBController->createDataset();
93
94
        if ($file === 'DEMO') {
95
            $name = 'Demo Report';
96
            $subheader = 'CSV Demo Data from GitHub';
97
            $parent = 0;
98
            $type = DataSourceController::DATASET_TYPE_EXTERNAL_FILE;
99
            $link = 'https://raw.githubusercontent.com/Rello/analytics/master/sample_data/sample1.csv';
100
            $visualization = 'table';
101
            $chart = 'line';
102
            $this->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, '', '', '');
103
        } elseif ($file !== '') {
104
            $name = explode('.', end(explode('/', $file)))[0];
105
            $subheader = $file;
106
            $parent = 0;
107
            $type = DataSourceController::DATASET_TYPE_INTERNAL_FILE;
108
            $visualization = 'ct';
109
            $chart = 'line';
110
            $this->update($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, '', '', '');
111
        }
112
        return $datasetId;
113
    }
114
115
    /**
116
     * Delete Dataset and all depending objects
117
     *
118
     * @NoAdminRequired
119
     * @param int $datasetId
120
     * @return bool
121
     */
122
    public function delete(int $datasetId)
123
    {
124
        $this->ShareController->deleteShareByDataset($datasetId);
125
        $this->DBController->deleteDataByDataset($datasetId);
126
        $this->DBController->deleteDataset($datasetId);
127
        //$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATASET, ActivityManager::SUBJECT_DATASET_DELETE);
128
        return true;
129
    }
130
131
    /**
132
     * get dataset details
133
     *
134
     * @NoAdminRequired
135
     * @param int $datasetId
136
     * @param $name
137
     * @param $subheader
138
     * @param int $parent
139
     * @param int $type
140
     * @param $link
141
     * @param $visualization
142
     * @param $chart
143
     * @param $dimension1
144
     * @param $dimension2
145
     * @param $dimension3
146
     * @return bool
147
     */
148
    public function update(int $datasetId, $name, $subheader, int $parent, int $type, $link, $visualization, $chart, $dimension1, $dimension2, $dimension3)
149
    {
150
        if ($type === DataSourceController::DATASET_TYPE_GROUP) {
151
            $parent = 0;
152
        }
153
        return $this->DBController->updateDataset($datasetId, $name, $subheader, $parent, $type, $link, $visualization, $chart, $dimension1, $dimension2, $dimension3);
154
    }
155
}