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

OutputController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 10
dl 0
loc 22
rs 9.9666

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\Controller;
13
14
use OCA\Analytics\DataSession;
15
use OCP\AppFramework\Controller;
16
use OCP\AppFramework\Http\DataResponse;
17
use OCP\AppFramework\Http\NotFoundResponse;
18
use OCP\Files\IRootFolder;
19
use OCP\Files\NotFoundException;
20
use OCP\ILogger;
21
use OCP\IRequest;
22
23
class OutputController extends Controller
24
{
25
    private $logger;
26
    private $DatasetController;
27
    private $rootFolder;
28
    private $userId;
29
    private $DataSession;
30
    private $ShareController;
31
    private $DataSourceController;
32
    private $StorageController;
33
34
    public function __construct(
35
        string $AppName,
36
        IRequest $request,
37
        $userId,
38
        ILogger $logger,
39
        IRootFolder $rootFolder,
40
        DatasetController $DatasetController,
41
        ShareController $ShareController,
42
        DataSession $DataSession,
43
        DataSourceController $DataSourceController,
44
        StorageController $StorageController
45
    )
46
    {
47
        parent::__construct($AppName, $request);
48
        $this->userId = $userId;
49
        $this->logger = $logger;
50
        $this->DatasetController = $DatasetController;
51
        $this->rootFolder = $rootFolder;
52
        $this->DataSession = $DataSession;
53
        $this->ShareController = $ShareController;
54
        $this->DataSourceController = $DataSourceController;
55
        $this->StorageController = $StorageController;
56
    }
57
58
    /**
59
     * get the data when requested from internal page
60
     *
61
     * @NoAdminRequired
62
     * @param int $datasetId
63
     * @param $objectDrilldown
64
     * @param $dateDrilldown
65
     * @return DataResponse|NotFoundResponse
66
     * @throws NotFoundException
67
     */
68
    public function read(int $datasetId, $objectDrilldown, $dateDrilldown)
69
    {
70
        $datasetMetadata = $this->DatasetController->getOwnDataset($datasetId);
71
        if (empty($datasetMetadata)) $datasetMetadata = $this->ShareController->getSharedDataset($datasetId);
72
73
        if (!empty($datasetMetadata)) {
74
            //$this->logger->error('test');
75
            $result = $this->getData($datasetMetadata, $objectDrilldown, $dateDrilldown);
76
            return new DataResponse($result);
77
        } else {
78
            return new NotFoundResponse();
79
        }
80
    }
81
82
    /**
83
     * Get the data from backend;
84
     * pre-evaluation of valid datasetId within read & readPublic is trusted here
85
     *
86
     * @NoAdminRequired
87
     * @param $datasetMetadata
88
     * @param $objectDrilldown
89
     * @param $dateDrilldown
90
     * @return array
91
     * @throws NotFoundException
92
     */
93
    private function getData($datasetMetadata, $objectDrilldown, $dateDrilldown)
94
    {
95
        $datasetMetadata['type'] = (int)$datasetMetadata['type'];
96
        if ($datasetMetadata['type'] === DataSourceController::DATASET_TYPE_INTERNAL_DB) {
97
            $result = $this->StorageController->read($datasetMetadata, $objectDrilldown, $dateDrilldown);
98
        } else {
99
            $result = $this->DataSourceController->read($datasetMetadata);
100
        }
101
102
        unset($datasetMetadata['id']
103
            , $datasetMetadata['parent']
104
            , $datasetMetadata['user_id']
105
            , $datasetMetadata['link']
106
            , $datasetMetadata['dimension1']
107
            , $datasetMetadata['dimension2']
108
            , $datasetMetadata['dimension3']
109
            , $datasetMetadata['password']
110
        );
111
        //$this->logger->error('dataset csv result: ' . $result);
112
113
        $result['options'] = $datasetMetadata;
114
        return $result;
115
    }
116
117
    /**
118
     * get the data when requested from public page
119
     *
120
     * @NoAdminRequired
121
     * @PublicPage
122
     * @UseSession
123
     * @param $token
124
     * @param $objectDrilldown
125
     * @param $dateDrilldown
126
     * @return DataResponse|NotFoundResponse
127
     * @throws NotFoundException
128
     */
129
    public function readPublic($token, $objectDrilldown, $dateDrilldown)
130
    {
131
        $share = $this->ShareController->getDatasetByToken($token);
132
        if (empty($share)) {
133
            // Dataset not shared or wrong token
134
            return new NotFoundResponse();
135
        } else {
136
            if ($share['password'] !== null) {
137
                $password = $this->DataSession->getPasswordForShare($token);
138
                $passwordVerification = $this->ShareController->verifyPassword($password, $share['password']);
139
                if ($passwordVerification === false) {
140
                    return new NotFoundResponse();
141
                }
142
            }
143
            $result = $this->getData($share, $objectDrilldown, $dateDrilldown);
144
            return new DataResponse($result);
145
        }
146
    }
147
}