Passed
Pull Request — master (#64)
by Marcel
02:20
created

Operation   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 35
c 4
b 0
f 0
dl 0
loc 89
rs 10
wmc 14
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 2020 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;
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
    private $eventDispatcher;
40
41
    public function __construct(
42
        IL10N $l,
43
        IURLGenerator $urlGenerator,
44
        ILogger $logger,
45
        DataloadController $DataloadController
46
        IEventDispatcher $eventDispatcher;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting ')' on line 46 at column 8
Loading history...
47
    )
48
    {
49
        $this->l = $l;
50
        $this->urlGenerator = $urlGenerator;
51
        $this->logger = $logger;
52
        $this->DataloadController = $DataloadController;
53
        $this->eventDispatcher;
54
    }
55
56
    public function register(): void
57
    {
58
        if (interface_exists(IRuleMatcher::class)) {
59
            $this->eventDispatcher->addListener(IManager::EVENT_NAME_REG_OPERATION, function (GenericEvent $event) {
60
                $operation = \OC::$server->query(Operation::class);
61
                $event->getSubject()->registerOperation($operation);
62
                Util::addScript('analytics', 'flow');
63
            });
64
        }
65
    }
66
67
    public function getDisplayName(): string
68
    {
69
        return $this->l->t('Analytics');
70
    }
71
72
    public function getDescription(): string
73
    {
74
        return $this->l->t('Read file and add its data to an existing report');
75
    }
76
77
    public function getIcon(): string
78
    {
79
        return $this->urlGenerator->imagePath('analytics', 'app.svg');
80
    }
81
82
    public function isAvailableForScope(int $scope): bool
83
    {
84
        return $scope === IManager::SCOPE_USER;
85
    }
86
87
    /**
88
     * @param $name
89
     * @param array $checks
90
     * @param $operation
91
     * @since 9.1
92
     */
93
    public function validateOperation($name, array $checks, $operation): void
94
    {
95
    }
96
97
    public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatcher): void
98
    {
99
        $flow = $ruleMatcher->getFlows(true);
100
        $datasetId = (int)$flow['operation'];
101
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("Analytics Flow Operation 115: storing file '" . $file . "' in report " . $datasetId);
116
        try {
117
            $this->DataloadController->importFile($datasetId, $file);
118
        } catch (NotFoundException $e) {
119
            return;
120
        } catch (\Exception $e) {
121
            return;
122
        }
123
    }
124
}
125