Passed
Push — master ( 5e440a...6ae27f )
by Marcel
02:39
created

FlowOperation::getIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 FlowOperation 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
47
    )
48
    {
49
        $this->l = $l;
50
        $this->urlGenerator = $urlGenerator;
51
        $this->logger = $logger;
52
        $this->DataloadController = $DataloadController;
53
        $this->eventDispatcher = $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(FlowOperation::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();
0 ignored issues
show
Bug introduced by
The method getSubject() does not exist on OCP\EventDispatcher\Event. It seems like you code against a sub-type of OCP\EventDispatcher\Event such as OCP\EventDispatcher\GenericEvent. ( Ignorable by Annotation )

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

104
            /** @scrutinizer ignore-call */ 
105
            list(, $node) = $event->getSubject();
Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\ILogger::debug() has been deprecated: 20.0.0 use \Psr\Log\LoggerInterface::debug ( Ignorable by Annotation )

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

115
        /** @scrutinizer ignore-deprecated */ $this->logger->debug("Analytics Flow Operation 115: storing file '" . $file . "' in report " . $datasetId);

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...
116
        try {
117
            $this->DataloadController->importFile($datasetId, $file);
118
        } catch (NotFoundException $e) {
119
            return;
120
        } catch (\Exception $e) {
121
            return;
122
        }
123
    }
124
}
125