Passed
Push — master ( 953339...7b825e )
by Marcel
02:59
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 OCA\Analytics\Datasource\SurveyService;
0 ignored issues
show
Bug introduced by
The type OCA\Analytics\Datasource\SurveyService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use OCP\AppFramework\Controller;
22
use OCP\Files\NotFoundException;
23
use OCP\ILogger;
24
use OCP\IRequest;
25
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
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 $SurveyService;
36
    private $userId;
37
    /** @var EventDispatcherInterface */
38
    protected $dispatcher;
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
    const DATASET_TYPE_SURVEY = 7;
48
49
    public function __construct(
50
        string $AppName,
51
        IRequest $request,
52
        $userId,
53
        ILogger $logger,
54
        GithubService $GithubService,
55
        FileService $FileService,
56
        RegexService $RegexService,
57
        JsonService $JsonService,
58
        SurveyService $SurveyService,
59
        ExternalFileService $ExternalFileService,
60
        EventDispatcherInterface $dispatcher
61
    )
62
    {
63
        parent::__construct($AppName, $request);
64
        $this->userId = $userId;
65
        $this->logger = $logger;
66
        $this->ExternalFileService = $ExternalFileService;
67
        $this->GithubService = $GithubService;
68
        $this->RegexService = $RegexService;
69
        $this->FileService = $FileService;
70
        $this->JsonService = $JsonService;
71
        $this->SurveyService = $SurveyService;
72
        $this->dispatcher = $dispatcher;
73
    }
74
75
    /**
76
     * Get the data from a datasource;
77
     *
78
     * @NoAdminRequired
79
     * @param int $datasource
80
     * @param $option
81
     * @return array|NotFoundException
82
     * @throws NotFoundException
83
     */
84
    public function read(int $datasource, $option)
85
    {
86
        //$this->logger->debug('DataSourceController 66: Datasource Id: ' . $datasource . ', Option: ' . json_encode($option));
87
        if ($datasource === self::DATASET_TYPE_INTERNAL_FILE) $result = $this->FileService->read($option);
88
        elseif ($datasource === self::DATASET_TYPE_GIT) $result = $this->GithubService->read($option);
89
        elseif ($datasource === self::DATASET_TYPE_EXTERNAL_FILE) $result = $this->ExternalFileService->read($option);
90
        elseif ($datasource === self::DATASET_TYPE_REGEX) $result = $this->RegexService->read($option);
91
        elseif ($datasource === self::DATASET_TYPE_JSON) $result = $this->JsonService->read($option);
92
        elseif ($datasource === self::DATASET_TYPE_SURVEY) $result = $this->SurveyService->read($option);
93
        else $result = new NotFoundException();
94
95
        return $result;
96
    }
97
98
    /**
99
     * template for options & settings
100
     *
101
     * @NoAdminRequired
102
     * @param int $datasource
103
     * @return array
104
     */
105
    public function getTemplates()
106
    {
107
        $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...
108
        $result[self::DATASET_TYPE_GIT] = $this->GithubService->getTemplate();
109
        $result[self::DATASET_TYPE_EXTERNAL_FILE] = $this->ExternalFileService->getTemplate();
110
        $result[self::DATASET_TYPE_REGEX] = $this->RegexService->getTemplate();
111
        $result[self::DATASET_TYPE_JSON] = $this->JsonService->getTemplate();
112
        $result[self::DATASET_TYPE_SURVEY] = $this->SurveyService->getTemplate();
113
        return $result;
114
    }
115
116
    public function EventTest()
117
    {
118
        $event = new DatasourceEvent(DatasourceEvent::class);
119
        $this->dispatcher->dispatch(DatasourceEvent::class, $event);
0 ignored issues
show
Bug introduced by
OCA\Analytics\Datasource\DatasourceEvent::class of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

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

119
        $this->dispatcher->dispatch(/** @scrutinizer ignore-type */ DatasourceEvent::class, $event);
Loading history...
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

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

119
        $this->dispatcher->/** @scrutinizer ignore-call */ 
120
                           dispatch(DatasourceEvent::class, $event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
120
121
        $datasources = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $datasources is dead and can be removed.
Loading history...
122
        foreach ($event->getDataSources() as $datasource) {
123
            $datasource->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Closure. ( Ignorable by Annotation )

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

123
            $datasource->/** @scrutinizer ignore-call */ 
124
                         getName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
        }
125
    }
126
}