Passed
Branch master (7b1276)
by Marcel
06:24
created

DataController::readPublic()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 16
rs 9.9
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\DataSession;
15
use OCA\Analytics\Datasource\FileService;
16
use OCA\Analytics\Datasource\GithubService;
17
use OCA\Analytics\Service\DataService;
18
use OCA\Analytics\Service\DatasetService;
19
use OCP\AppFramework\Controller;
20
use OCP\AppFramework\Http\DataResponse;
21
use OCP\AppFramework\Http\NotFoundResponse;
22
use OCP\Files\IRootFolder;
23
use OCP\Files\NotFoundException;
24
use OCP\ILogger;
25
use OCP\IRequest;
26
27
class DataController extends Controller
28
{
29
    private $logger;
30
    private $DataService;
31
    private $DatasetService;
32
    private $GithubService;
33
    private $FileService;
34
    private $rootFolder;
35
    private $userId;
36
    /** @var DataSession */
37
    private $DataSession;
38
    private $ShareController;
39
40
    public function __construct(
41
        string $AppName,
42
        IRequest $request,
43
        $userId,
44
        ILogger $logger,
45
        IRootFolder $rootFolder,
46
        DataService $DataService,
47
        GithubService $GithubService,
48
        DatasetService $DatasetService,
49
        FileService $FileService,
50
        ShareController $ShareController,
51
        DataSession $DataSession
52
    )
53
    {
54
        parent::__construct($AppName, $request);
55
        $this->userId = $userId;
56
        $this->logger = $logger;
57
        $this->DataService = $DataService;
58
        $this->DatasetService = $DatasetService;
59
        $this->GithubService = $GithubService;
60
        $this->FileService = $FileService;
61
        $this->rootFolder = $rootFolder;
62
        $this->DataSession = $DataSession;
63
        $this->ShareController = $ShareController;
64
    }
65
66
    /**
67
     * get the data when requested from internal page
68
     *
69
     * @NoAdminRequired
70
     * @param int $datasetId
71
     * @param $objectDrilldown
72
     * @param $dateDrilldown
73
     * @return DataResponse|NotFoundResponse
74
     * @throws NotFoundException
75
     */
76
    public function read(int $datasetId, $objectDrilldown, $dateDrilldown)
77
    {
78
79
        $datasetMetadata = $this->DatasetService->getOwnDataset($datasetId);
80
        if ($datasetMetadata === false) $datasetMetadata = $this->ShareController->getSharedDataset($datasetId);
0 ignored issues
show
introduced by
The condition $datasetMetadata === false is always false.
Loading history...
81
82
        if ($datasetMetadata !== false) {
0 ignored issues
show
introduced by
The condition $datasetMetadata !== false is always true.
Loading history...
83
            //$this->logger->error('test');
84
            $result = $this->getData($datasetMetadata, $objectDrilldown, $dateDrilldown);
85
            return new DataResponse($result);
86
        } else {
87
            return new NotFoundResponse();
88
        }
89
    }
90
91
    /**
92
     * get the data when requested from public page
93
     *
94
     * @NoAdminRequired
95
     * @PublicPage
96
     * @UseSession
97
     * @param $token
98
     * @param $objectDrilldown
99
     * @param $dateDrilldown
100
     * @return DataResponse|NotFoundResponse
101
     * @throws NotFoundException
102
     */
103
    public function readPublic($token, $objectDrilldown, $dateDrilldown)
104
    {
105
        $share = $this->ShareController->getDatasetByToken($token);
106
        if ($share === false) {
0 ignored issues
show
introduced by
The condition $share === false is always false.
Loading history...
107
            // Dataset not shared or wrong token
108
            return new NotFoundResponse();
109
        } else {
110
            if ($share['password'] !== null) {
111
                $password = $this->DataSession->getPasswordForShare($token);
112
                $passwordVerification = $this->ShareController->verifyPassword($password, $share['password']);
113
                if ($passwordVerification === false) {
114
                    return new NotFoundResponse();
115
                }
116
            }
117
            $result = $this->getData($share, $objectDrilldown, $dateDrilldown);
118
            return new DataResponse($result);
119
        }
120
    }
121
122
    /**
123
     * Get the data from backend;
124
     * pre-evaluation of valid datasetId within read & readPublic is trusted here
125
     *
126
     * @NoAdminRequired
127
     * @param $datasetMetadata
128
     * @param $objectDrilldown
129
     * @param $dateDrilldown
130
     * @return array
131
     * @throws NotFoundException
132
     */
133
    private function getData($datasetMetadata, $objectDrilldown, $dateDrilldown)
134
    {
135
        $datasetMetadata['type'] = (int)$datasetMetadata['type'];
136
        if ($datasetMetadata['type'] === 1) $result = $this->FileService->read($datasetMetadata);
137
        elseif ($datasetMetadata['type'] === 2) $result = $this->DataService->read($datasetMetadata, $objectDrilldown, $dateDrilldown);
138
        elseif ($datasetMetadata['type'] === 3) $result = $this->GithubService->read($datasetMetadata);
139
140
        unset($datasetMetadata['id']
141
            , $datasetMetadata['parent']
142
            , $datasetMetadata['user_id']
143
            , $datasetMetadata['link']
144
            , $datasetMetadata['dimension1']
145
            , $datasetMetadata['dimension2']
146
            , $datasetMetadata['dimension3']
147
            , $datasetMetadata['password']
148
        );
149
150
        $result['options'] = $datasetMetadata;
151
        return $result;
152
    }
153
154
155
    /**
156
     * Get the items for the selected category
157
     *
158
     * @NoAdminRequired
159
     * @param int $datasetId
160
     * @param $import
161
     * @return DataResponse
162
     */
163
    public function importCSV(int $datasetId, $import)
164
    {
165
        return new DataResponse($this->DataService->import($datasetId, $import));
166
    }
167
168
    /**
169
     * Get the items for the selected category
170
     *
171
     * @NoAdminRequired
172
     * @param int $datasetId
173
     * @param $path
174
     * @return DataResponse
175
     * @throws NotFoundException
176
     */
177
    public function importFile(int $datasetId, $path)
178
    {
179
        return new DataResponse($this->FileService->import($datasetId, $path));
180
    }
181
182
    /**
183
     * Get the items for the selected category
184
     *
185
     * @NoAdminRequired
186
     * @param int $datasetId
187
     * @param $dimension1
188
     * @param $dimension2
189
     * @param $dimension3
190
     * @return DataResponse
191
     */
192
    public function update(int $datasetId, $dimension1, $dimension2, $dimension3)
193
    {
194
        return new DataResponse($this->DataService->update($datasetId, $dimension1, $dimension2, $dimension3));
195
    }
196
}