Passed
Push — master ( ce974b...f4e246 )
by Marcel
02:50
created

DatasourceController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 140
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getOwnDatasources() 0 9 1
A __construct() 0 22 1
A getDatasources() 0 3 1
A getTemplates() 0 7 2
A index() 0 16 3
A read() 0 4 1
A getRegisteredDatasources() 0 16 3
1
<?php
2
/**
3
 * 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 2020 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Controller;
13
14
use OCA\Analytics\Datasource\DatasourceEvent;
15
use OCA\Analytics\Datasource\ExternalFile;
16
use OCA\Analytics\Datasource\File;
17
use OCA\Analytics\Datasource\Github;
18
use OCA\Analytics\Datasource\Json;
19
use OCA\Analytics\Datasource\Regex;
20
use OCP\AppFramework\Controller;
21
use OCP\EventDispatcher\IEventDispatcher;
22
use OCP\Files\NotFoundException;
23
use OCP\IL10N;
24
use OCP\ILogger;
25
use OCP\IRequest;
26
27
class DatasourceController extends Controller
28
{
29
    private $logger;
30
    private $GithubService;
31
    private $FileService;
32
    private $ExternalFileService;
33
    private $RegexService;
34
    private $JsonService;
35
    /** @var IEventDispatcher */
36
    private $dispatcher;
37
    private $l10n;
38
39
    const DATASET_TYPE_GROUP = 0;
40
    const DATASET_TYPE_INTERNAL_FILE = 1;
41
    const DATASET_TYPE_INTERNAL_DB = 2;
42
    const DATASET_TYPE_GIT = 3;
43
    const DATASET_TYPE_EXTERNAL_FILE = 4;
44
    const DATASET_TYPE_REGEX = 5;
45
    const DATASET_TYPE_JSON = 6;
46
47
    public function __construct(
48
        string $AppName,
49
        IRequest $request,
50
        ILogger $logger,
51
        Github $GithubService,
52
        File $FileService,
53
        Regex $RegexService,
54
        Json $JsonService,
55
        ExternalFile $ExternalFileService,
56
        IL10N $l10n,
57
        IEventDispatcher $dispatcher
58
    )
59
    {
60
        parent::__construct($AppName, $request);
61
        $this->logger = $logger;
62
        $this->ExternalFileService = $ExternalFileService;
63
        $this->GithubService = $GithubService;
64
        $this->RegexService = $RegexService;
65
        $this->FileService = $FileService;
66
        $this->JsonService = $JsonService;
67
        $this->dispatcher = $dispatcher;
68
        $this->l10n = $l10n;
69
    }
70
71
    /**
72
     * get all datasource ids + names
73
     *
74
     * @NoAdminRequired
75
     */
76
    public function index()
77
    {
78
        $datasources = array();
79
        $result = array();
80
        foreach ($this->getDatasources() as $key => $class) {
81
            $datasources[$key] = $class->getName();
82
        }
83
        $result['datasources'] = $datasources;
84
85
        $options = array();
86
        foreach ($this->getDatasources() as $key => $class) {
87
            $options[$key] = $class->getTemplate();
88
        }
89
        $result['options'] = $options;
90
91
        return $result;
92
    }
93
94
    /**
95
     * get all datasource templates
96
     *
97
     * @NoAdminRequired
98
     * @return array
99
     */
100
    public function getTemplates()
101
    {
102
        $result = array();
103
        foreach ($this->getDatasources() as $key => $class) {
104
            $result[$key] = $class->getTemplate();
105
        }
106
        return $result;
107
    }
108
109
    /**
110
     * Get the data from a datasource;
111
     *
112
     * @NoAdminRequired
113
     * @param int $datasourceId
114
     * @param $option
115
     * @return array|NotFoundException
116
     */
117
    public function read(int $datasourceId, $option)
118
    {
119
        //$this->logger->debug('DatasourceController 66: Datasource Id: ' . $datasource . ', Option: ' . json_encode($option));
120
        return $this->getDatasources()[$datasourceId]->readData($option);
121
    }
122
123
    /**
124
     * combine internal and registered datasources
125
     * @return array
126
     */
127
    private function getDatasources()
128
    {
129
        return $this->getOwnDatasources() + $this->getRegisteredDatasources();
130
    }
131
132
    /**
133
     * map all internal datasources to their IDs
134
     * @return array
135
     */
136
    private function getOwnDatasources()
137
    {
138
        $datasources = [];
139
        $datasources[self::DATASET_TYPE_INTERNAL_FILE] = $this->FileService;
140
        $datasources[self::DATASET_TYPE_GIT] = $this->GithubService;
141
        $datasources[self::DATASET_TYPE_EXTERNAL_FILE] = $this->ExternalFileService;
142
        $datasources[self::DATASET_TYPE_REGEX] = $this->RegexService;
143
        $datasources[self::DATASET_TYPE_JSON] = $this->JsonService;
144
        return $datasources;
145
    }
146
147
    /**
148
     * map all registered datasources to their IDs
149
     * @return array
150
     */
151
    private function getRegisteredDatasources()
152
    {
153
        $datasources = [];
154
        $event = new DatasourceEvent();
155
        $this->dispatcher->dispatchTyped($event);
156
157
        foreach ($event->getDataSources() as $class) {
158
            $uniqueId = '99' . \OC::$server->get($class)->getId();
159
160
            if (isset($datasources[$uniqueId])) {
161
                $this->logger->logException(new \InvalidArgumentException('Datasource with the same ID already registered: ' . \OC::$server->get($class)->getName()), ['level' => ILogger::INFO]);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

161
                $this->logger->logException(new \InvalidArgumentException('Datasource with the same ID already registered: ' . \OC::$server->get($class)->getName()), ['level' => /** @scrutinizer ignore-deprecated */ ILogger::INFO]);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The function OCP\ILogger::logException() has been deprecated: 20.0.0 use the `exception` entry in the context of any method in \Psr\Log\LoggerInterface ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

161
                /** @scrutinizer ignore-deprecated */ $this->logger->logException(new \InvalidArgumentException('Datasource with the same ID already registered: ' . \OC::$server->get($class)->getName()), ['level' => ILogger::INFO]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
162
                continue;
163
            }
164
            $datasources[$uniqueId] = \OC::$server->get($class);
165
        }
166
        return $datasources;
167
    }
168
}