Passed
Push — master ( 9c2d3e...b67124 )
by Marcel
02:34
created

DataSourceController::getOwnDatasources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
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
    private $userId;
36
    /** @var IEventDispatcher */
37
    protected $dispatcher;
38
    private $l10n;
39
40
    const DATASET_TYPE_GROUP = 0;
41
    const DATASET_TYPE_INTERNAL_FILE = 1;
42
    const DATASET_TYPE_INTERNAL_DB = 2;
43
    const DATASET_TYPE_GIT = 3;
44
    const DATASET_TYPE_EXTERNAL_FILE = 4;
45
    const DATASET_TYPE_REGEX = 5;
46
    const DATASET_TYPE_JSON = 6;
47
48
    public function __construct(
49
        string $AppName,
50
        IRequest $request,
51
        $userId,
52
        ILogger $logger,
53
        Github $GithubService,
54
        File $FileService,
55
        Regex $RegexService,
56
        Json $JsonService,
57
        ExternalFile $ExternalFileService,
58
        IL10N $l10n,
59
        IEventDispatcher $dispatcher
60
    )
61
    {
62
        parent::__construct($AppName, $request);
63
        $this->userId = $userId;
64
        $this->logger = $logger;
65
        $this->ExternalFileService = $ExternalFileService;
66
        $this->GithubService = $GithubService;
67
        $this->RegexService = $RegexService;
68
        $this->FileService = $FileService;
69
        $this->JsonService = $JsonService;
70
        $this->dispatcher = $dispatcher;
71
        $this->l10n = $l10n;
72
    }
73
74
    /**
75
     * get all datasource ids + names
76
     *
77
     * @NoAdminRequired
78
     */
79
    public function index()
80
    {
81
        $result = array();
82
        foreach ($this->getDatasources() as $key => $class) {
83
            $result[$key] = $class->getName();
84
        }
85
        return $result;
86
    }
87
88
    /**
89
     * get all datasource templates
90
     *
91
     * @NoAdminRequired
92
     * @return array
93
     */
94
    public function getTemplates()
95
    {
96
        $result = array();
97
        foreach ($this->getDatasources() as $key => $class) {
98
            $result[$key] = $class->getTemplate();
99
        }
100
        return $result;
101
    }
102
103
    /**
104
     * Get the data from a datasource;
105
     *
106
     * @NoAdminRequired
107
     * @param int $datasource
108
     * @param $option
109
     * @return array|NotFoundException
110
     */
111
    public function read(int $datasource, $option)
112
    {
113
        //$this->logger->debug('DataSourceController 66: Datasource Id: ' . $datasource . ', Option: ' . json_encode($option));
114
        return $this->getDatasources()[$datasource]->readData($option);
115
    }
116
117
    /**
118
     * combine internal and registered datasources
119
     * @return array
120
     */
121
    private function getDatasources()
122
    {
123
        return $this->getOwnDatasources() + $this->getRegisteredDatasources();
124
    }
125
126
    /**
127
     * map all internal datasources to their IDs
128
     * @return array
129
     */
130
    private function getOwnDatasources()
131
    {
132
        $datasources = [];
133
        $datasources[self::DATASET_TYPE_INTERNAL_FILE] = $this->FileService;
134
        $datasources[self::DATASET_TYPE_GIT] = $this->GithubService;
135
        $datasources[self::DATASET_TYPE_EXTERNAL_FILE] = $this->ExternalFileService;
136
        $datasources[self::DATASET_TYPE_REGEX] = $this->RegexService;
137
        $datasources[self::DATASET_TYPE_JSON] = $this->JsonService;
138
        return $datasources;
139
    }
140
141
    /**
142
     * map all registered datasources to their IDs
143
     * @return array
144
     */
145
    private function getRegisteredDatasources()
146
    {
147
        $datasources = [];
148
        $event = new DatasourceEvent();
149
        $this->dispatcher->dispatchTyped($event);
150
151
        foreach ($event->getDataSources() as $class) {
152
            $uniqueId = '99' . \OC::$server->get($class)->getId();
153
154
            if (isset($datasources[$uniqueId])) {
155
                $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

155
                $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

155
                /** @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...
156
                continue;
157
            }
158
            $datasources[$uniqueId] = \OC::$server->get($class);
159
        }
160
        return $datasources;
161
    }
162
}