Passed
Push — master ( 14e493...9c2d3e )
by Marcel
02:30
created

DataSourceController::EventTest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
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\ExternalFileService;
16
use OCA\Analytics\Datasource\FileService;
17
use OCA\Analytics\Datasource\GithubService;
18
use OCA\Analytics\Datasource\JsonService;
19
use OCA\Analytics\Datasource\RegexService;
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
        GithubService $GithubService,
54
        FileService $FileService,
55
        RegexService $RegexService,
56
        JsonService $JsonService,
57
        ExternalFileService $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 datasets
76
     *
77
     * @NoAdminRequired
78
     */
79
    public function index()
80
    {
81
        //$result[self::DATASET_TYPE_GROUP] = $this->l10n->t('No Data / Group');
82
        //$result[self::DATASET_TYPE_INTERNAL_DB] = $this->l10n->t('Internal Database');
83
        $result[self::DATASET_TYPE_INTERNAL_FILE] = $this->FileService->getName();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
84
        $result[self::DATASET_TYPE_GIT] = $this->GithubService->getName();
85
        $result[self::DATASET_TYPE_EXTERNAL_FILE] = $this->ExternalFileService->getName();
86
        $result[self::DATASET_TYPE_REGEX] = $this->RegexService->getName();
87
        $result[self::DATASET_TYPE_JSON] = $this->JsonService->getName();
88
89
        $result = $result + $this->getRegisteredDatasourceNames();
90
        return $result;
91
    }
92
93
    /**
94
     * template for options & settings
95
     *
96
     * @NoAdminRequired
97
     * @return array
98
     */
99
    public function getTemplates()
100
    {
101
        $result[self::DATASET_TYPE_INTERNAL_FILE] = $this->FileService->getTemplate();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
102
        $result[self::DATASET_TYPE_GIT] = $this->GithubService->getTemplate();
103
        $result[self::DATASET_TYPE_EXTERNAL_FILE] = $this->ExternalFileService->getTemplate();
104
        $result[self::DATASET_TYPE_REGEX] = $this->RegexService->getTemplate();
105
        $result[self::DATASET_TYPE_JSON] = $this->JsonService->getTemplate();
106
        $result = $result + $this->getRegisteredDatasourceTemplates();
107
        return $result;
108
    }
109
110
    /**
111
     * Get the data from a datasource;
112
     *
113
     * @NoAdminRequired
114
     * @param int $datasource
115
     * @param $option
116
     * @return array|NotFoundException
117
     * @throws NotFoundException
118
     */
119
    public function read(int $datasource, $option)
120
    {
121
        //$this->logger->debug('DataSourceController 66: Datasource Id: ' . $datasource . ', Option: ' . json_encode($option));
122
        if ($datasource === self::DATASET_TYPE_INTERNAL_FILE) $result = $this->FileService->read($option);
123
        elseif ($datasource === self::DATASET_TYPE_GIT) $result = $this->GithubService->read($option);
124
        elseif ($datasource === self::DATASET_TYPE_EXTERNAL_FILE) $result = $this->ExternalFileService->read($option);
125
        elseif ($datasource === self::DATASET_TYPE_REGEX) $result = $this->RegexService->read($option);
126
        elseif ($datasource === self::DATASET_TYPE_JSON) $result = $this->JsonService->read($option);
127
        else $result = new NotFoundException();
128
129
        return $result;
130
    }
131
132
    private function getRegisteredDatasourceTemplates()
133
    {
134
        $event = new DatasourceEvent();
135
        $this->dispatcher->dispatchTyped($event);
136
137
        $datasources = [];
138
        foreach ($event->getDataSources() as $class) {
139
            $uniqueId = $this->uniqueId(\OC::$server->get($class)->getId());
140
141
            if (isset($datasources[$uniqueId])) {
142
                $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

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

142
                /** @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...
143
                continue;
144
            }
145
146
            $datasources[$uniqueId] = \OC::$server->get($class)->getTemplate();
147
        }
148
        return $datasources;
149
    }
150
151
    private function getRegisteredDatasourceNames()
152
    {
153
        $event = new DatasourceEvent();
154
        $this->dispatcher->dispatchTyped($event);
155
156
        $datasources = [];
157
        foreach ($event->getDataSources() as $class) {
158
            $uniqueId = $this->uniqueId(\OC::$server->get($class)->getId());
159
            $datasources[$uniqueId] = \OC::$server->get($class)->getName();
160
        }
161
        return $datasources;
162
    }
163
164
    private function uniqueId($id)
165
    {
166
        return '99' . $id;
167
    }
168
}