Passed
Push — master ( dd544e...5f5496 )
by Marcel
02:27
created

Operation::onEvent()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 16
nop 3
dl 0
loc 26
rs 8.8333
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Data 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 Marcel Scherello
11
 */
12
13
namespace OCA\Analytics\Flow;
14
15
use OCA\Analytics\Controller\DataloadController;
16
use OCP\EventDispatcher\Event;
17
use OCP\EventDispatcher\IEventDispatcher;
18
use OCP\Files\Folder;
19
use OCP\Files\Node;
20
use OCP\Files\NotFoundException;
21
use OCP\IL10N;
22
use OCP\ILogger;
23
use OCP\IURLGenerator;
24
use OCP\Util;
25
use OCP\WorkflowEngine\IManager;
26
use OCP\WorkflowEngine\IOperation;
27
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...
28
use Symfony\Component\EventDispatcher\GenericEvent;
29
30
class Operation implements IOperation
31
{
32
33
    /** @var IL10N */
34
    private $l;
35
    /** @var IURLGenerator */
36
    private $urlGenerator;
37
    private $logger;
38
    private $DataloadController;
39
40
    public function __construct(
41
        IL10N $l,
42
        IURLGenerator $urlGenerator,
43
        ILogger $logger,
44
        DataloadController $DataloadController
45
    )
46
    {
47
        $this->l = $l;
48
        $this->urlGenerator = $urlGenerator;
49
        $this->logger = $logger;
50
        $this->DataloadController = $DataloadController;
51
    }
52
53
    public static function register(IEventDispatcher $dispatcher): void
54
    {
55
        $dispatcher->addListener(IManager::EVENT_NAME_REG_OPERATION, function (GenericEvent $event) {
0 ignored issues
show
Bug introduced by
The constant OCP\WorkflowEngine\IMana...VENT_NAME_REG_OPERATION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56
            $operation = \OC::$server->query(Operation::class);
57
            $event->getSubject()->registerOperation($operation);
58
            Util::addScript('analytics', 'flow');
59
        });
60
    }
61
62
    public function getDisplayName(): string
63
    {
64
        return $this->l->t('Add to Analytics');
65
    }
66
67
    public function getDescription(): string
68
    {
69
        return $this->l->t('Read a file and add its data to an existing report');
70
    }
71
72
    public function getIcon(): string
73
    {
74
        return $this->urlGenerator->imagePath('analytics', 'app.svg');
75
    }
76
77
    public function isAvailableForScope(int $scope): bool
78
    {
79
        return $scope === IManager::SCOPE_USER;
0 ignored issues
show
Bug introduced by
The constant OCP\WorkflowEngine\IManager::SCOPE_USER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
80
    }
81
82
    /**
83
     * Validates whether a configured workflow rule is valid. If it is not,
84
     * an `\UnexpectedValueException` is supposed to be thrown.
85
     *
86
     * @param $name
87
     * @param array $checks
88
     * @param $operation
89
     * @since 9.1
90
     */
91
    public function validateOperation($name, array $checks, $operation): void
92
    {
93
        $this->logger->debug("validateOperation");
94
    }
95
96
    public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatcher): void
97
    {
98
        $flow = $ruleMatcher->getFlows(true);
99
        $datasetId = (int)$flow['operation'];
100
101
        try {
102
            if ($eventName === '\OCP\Files::postRename') {
103
                /** @var Node $oldNode */
104
                list(, $node) = $event->getSubject();
105
            } else {
106
                $node = $event->getSubject();
107
            }
108
109
            list(, , $folder, $file) = explode('/', $node->getPath(), 4);
110
            if ($folder !== 'files' || $node instanceof Folder) {
111
                return;
112
            }
113
            $file = '/' . $file;
114
115
            $this->logger->debug("Flow Operation 119: storing file '" . $file . "' in report " . $datasetId);
116
            try {
117
                $this->DataloadController->importFile($datasetId, $file);
118
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
119
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
120
            }
121
        } catch (\OCP\Files\NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
122
        }
123
    }
124
}