Passed
Push — master ( a3fb04...28fab3 )
by Marcel
04:29 queued 14s
created

FlowOperation   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 91
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isAvailableForScope() 0 3 1
A handle() 0 6 2
A getDisplayName() 0 3 1
A onEvent() 0 24 6
A __construct() 0 15 1
A getIcon() 0 3 1
A getDescription() 0 3 1
A validateOperation() 0 2 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Analytics
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the LICENSE.md file.
8
 *
9
 * @author Marcel Scherello <[email protected]>
10
 * @copyright 2019-2022 Marcel Scherello
11
 */
12
13
namespace OCA\Analytics\Flow;
14
15
use OCA\Analytics\Controller\DataloadController;
16
use OCP\EventDispatcher\Event;
0 ignored issues
show
Bug introduced by
The type OCP\EventDispatcher\Event 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...
17
use OCP\EventDispatcher\IEventDispatcher;
0 ignored issues
show
Bug introduced by
The type OCP\EventDispatcher\IEventDispatcher 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...
18
use OCP\Files\Folder;
0 ignored issues
show
Bug introduced by
The type OCP\Files\Folder 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...
19
use OCP\Files\Node;
0 ignored issues
show
Bug introduced by
The type OCP\Files\Node 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...
20
use OCP\Files\NotFoundException;
0 ignored issues
show
Bug introduced by
The type OCP\Files\NotFoundException 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\IL10N;
0 ignored issues
show
Bug introduced by
The type OCP\IL10N 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...
22
use OCP\IURLGenerator;
0 ignored issues
show
Bug introduced by
The type OCP\IURLGenerator 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...
23
use OCP\Util;
0 ignored issues
show
Bug introduced by
The type OCP\Util 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...
24
use OCP\WorkflowEngine\IManager;
0 ignored issues
show
Bug introduced by
The type OCP\WorkflowEngine\IManager 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...
25
use OCP\WorkflowEngine\IOperation;
0 ignored issues
show
Bug introduced by
The type OCP\WorkflowEngine\IOperation 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...
26
use OCP\WorkflowEngine\IRuleMatcher;
0 ignored issues
show
Bug introduced by
The type OCP\WorkflowEngine\IRuleMatcher 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...
27
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface 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...
28
use OCP\WorkflowEngine\Events\RegisterOperationsEvent;
0 ignored issues
show
Bug introduced by
The type OCP\WorkflowEngine\Events\RegisterOperationsEvent 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...
29
use Psr\Container\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Container\ContainerInterface 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...
30
31
class FlowOperation implements IOperation
32
{
33
34
    /** @var IL10N */
35
    private $l;
36
    /** @var IURLGenerator */
37
    private $urlGenerator;
38
    private $logger;
39
    private $DataloadController;
40
    private $eventDispatcher;
41
    private ContainerInterface $container;
42
43
    public function __construct(
44
        IL10N $l,
45
        IURLGenerator $urlGenerator,
46
        LoggerInterface $logger,
47
        DataloadController $DataloadController,
48
        IEventDispatcher $eventDispatcher,
49
        ContainerInterface $container
50
    )
51
    {
52
        $this->l = $l;
53
        $this->urlGenerator = $urlGenerator;
54
        $this->logger = $logger;
55
        $this->DataloadController = $DataloadController;
56
        $this->eventDispatcher = $eventDispatcher;
57
        $this->container = $container;
58
    }
59
60
    public function handle(Event $event): void {
61
        if (!$event instanceof RegisterOperationsEvent) {
62
            return;
63
        }
64
        $event->registerOperation($this->container->get(FlowOperation::class));
65
        Util::addScript('analytics', 'flow');
66
    }
67
68
    public function getDisplayName(): string
69
    {
70
        return $this->l->t('Analytics');
71
    }
72
73
    public function getDescription(): string
74
    {
75
        return $this->l->t('Read file and add its data to an existing dataset');
76
    }
77
78
    public function getIcon(): string
79
    {
80
        return $this->urlGenerator->imagePath('analytics', 'app.svg');
81
    }
82
83
    public function isAvailableForScope(int $scope): bool
84
    {
85
        return $scope === IManager::SCOPE_USER;
86
    }
87
88
    /**
89
     * @param $name
90
     * @param array $checks
91
     * @param $operation
92
     * @since 9.1
93
     */
94
    public function validateOperation($name, array $checks, $operation): void
95
    {
96
    }
97
98
    public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatcher): void
99
    {
100
        $flow = $ruleMatcher->getFlows(true);
101
        $datasetId = (int)$flow['operation'];
102
103
        if ($eventName === '\OCP\Files::postRename') {
104
            /** @var Node $oldNode */
105
            list(, $node) = $event->getSubject();
106
        } else {
107
            $node = $event->getSubject();
108
        }
109
110
        list(, , $folder, $file) = explode('/', $node->getPath(), 4);
111
        if ($folder !== 'files' || $node instanceof Folder) {
112
            return;
113
        }
114
        $file = '/' . $file;
115
116
        try {
117
            $this->DataloadController->importFile($datasetId, $file, true);
118
        } catch (NotFoundException $e) {
119
            return;
120
        } catch (\Exception $e) {
121
            return;
122
        }
123
    }
124
}
125