Passed
Push — master ( b4b724...0a9272 )
by Matthias
09:03 queued 11s
created

Application::addStorageWrapperCallback()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 14
rs 10
1
<?php
2
3
/**
4
 * @copyright Copyright (c) 2017 Matthias Held <[email protected]>
5
 * @author Matthias Held <[email protected]>
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace OCA\RansomwareDetection\AppInfo;
23
24
use OC\Files\Filesystem;
0 ignored issues
show
Bug introduced by
The type OC\Files\Filesystem 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 OCA\RansomwareDetection\Monitor;
26
use OCA\RansomwareDetection\Events\FilesEvents;
27
use OCA\RansomwareDetection\FilesHooks;
28
use OCA\RansomwareDetection\Classifier;
29
use OCA\RansomwareDetection\Analyzer\EntropyAnalyzer;
30
use OCA\RansomwareDetection\Analyzer\SequenceAnalyzer;
31
use OCA\RansomwareDetection\Analyzer\SequenceSizeAnalyzer;
32
use OCA\RansomwareDetection\Analyzer\FileTypeFunnellingAnalyzer;
33
use OCA\RansomwareDetection\Analyzer\EntropyFunnellingAnalyzer;
34
use OCA\RansomwareDetection\Analyzer\FileCorruptionAnalyzer;
35
use OCA\RansomwareDetection\Analyzer\FileExtensionAnalyzer;
36
use OCA\RansomwareDetection\Entropy\Entropy;
37
use OCA\RansomwareDetection\Notification\Notifier;
38
use OCA\RansomwareDetection\StorageWrapper;
0 ignored issues
show
Bug introduced by
The type OCA\RansomwareDetection\StorageWrapper 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...
39
use OCA\RansomwareDetection\Connector\Sabre\RequestPlugin;
40
use OCA\RansomwareDetection\Service\FileOperationService;
41
use OCA\RansomwareDetection\Mapper\FileOperationMapper;
0 ignored issues
show
Bug introduced by
The type OCA\RansomwareDetection\Mapper\FileOperationMapper 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...
42
use OCP\AppFramework\App;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\App 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...
43
use OCP\App\IAppManager;
0 ignored issues
show
Bug introduced by
The type OCP\App\IAppManager 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...
44
use OCP\Files\IRootFolder;
0 ignored issues
show
Bug introduced by
The type OCP\Files\IRootFolder 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...
45
use OCP\AppFramework\Utility\ITimeFactory;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Utility\ITimeFactory 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...
46
use OCP\Files\Storage\IStorage;
0 ignored issues
show
Bug introduced by
The type OCP\Files\Storage\IStorage 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...
47
use OCP\Notification\IManager;
0 ignored issues
show
Bug introduced by
The type OCP\Notification\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...
48
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...
49
use OCP\SabrePluginEvent;
0 ignored issues
show
Bug introduced by
The type OCP\SabrePluginEvent 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...
50
use OCP\ILogger;
0 ignored issues
show
Bug introduced by
The type OCP\ILogger 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...
51
use OCP\IConfig;
0 ignored issues
show
Bug introduced by
The type OCP\IConfig 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...
52
use OCP\IUserSession;
0 ignored issues
show
Bug introduced by
The type OCP\IUserSession 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...
53
use OCP\ISession;
0 ignored issues
show
Bug introduced by
The type OCP\ISession 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...
54
use OCP\IRequest;
0 ignored issues
show
Bug introduced by
The type OCP\IRequest 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...
55
56
class Application extends App
57
{
58
    const APP_ID = 'ransomware_detection';
59
60
    public function __construct()
61
    {
62
        parent::__construct(self::APP_ID);
63
64
        $container = $this->getContainer();
65
66
        // mapper
67
        $container->registerService('FileOperationMapper', function ($c) {
68
            return new FileOperationMapper(
69
                $c->query('ServerContainer')->getDb()
70
            );
71
        });
72
73
        // services
74
        $container->registerService('FileOperationService', function ($c) {
75
            return new FileOperationService(
76
                $c->query(FileOperationMapper::class),
77
                $c->query('ServerContainer')->getUserSession()->getUser()->getUID()
78
            );
79
        });
80
81
        // classifier
82
        $container->registerService('Classifier', function ($c) {
83
            return new Classifier(
84
                $c->query(ILogger::class),
85
                $c->query(FileOperationMapper::class),
86
                $c->query(FileOperationService::class)
87
            );
88
        });
89
90
        // entropy
91
        $container->registerService('Entropy', function ($c) {
92
            return new Entropy(
93
                $c->query(ILogger::class)
94
            );
95
        });
96
97
        // analyzer
98
        $container->registerService('SequenceSizeAnalyzer', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

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

98
        $container->registerService('SequenceSizeAnalyzer', function (/** @scrutinizer ignore-unused */ $c) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
            return new SequenceSizeAnalyzer();
100
        });
101
102
        $container->registerService('FileTypeFunnellingAnalyzer', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

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

102
        $container->registerService('FileTypeFunnellingAnalyzer', function (/** @scrutinizer ignore-unused */ $c) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
            return new FileTypeFunnellingAnalyzer();
104
        });
105
106
        $container->registerService('EntropyFunnellingAnalyzer', function ($c) {
107
            return new EntropyFunnellingAnalyzer(
108
                $c->query(ILogger::class)
109
            );
110
        });
111
112
        $container->registerService('FileExtensionAnalyzer', function ($c) {
113
            return new FileExtensionAnalyzer(
114
                $c->query(ILogger::class),
115
                $c->query(Entropy::class)
0 ignored issues
show
Unused Code introduced by
The call to OCA\RansomwareDetection\...Analyzer::__construct() has too many arguments starting with $c->query(OCA\Ransomware...Entropy\Entropy::class). ( Ignorable by Annotation )

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

115
            return /** @scrutinizer ignore-call */ new FileExtensionAnalyzer(

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...
116
117
            );
118
        });
119
120
        $container->registerService('SequenceAnalyzer', function ($c) {
121
            return new SequenceAnalyzer(
122
                $c->query(SequenceSizeAnalyzer::class),
123
                $c->query(FileTypeFunnellingAnalyzer::class),
124
                $c->query(EntropyFunnellingAnalyzer::class)
125
            );
126
        });
127
128
        $container->registerService('EntropyAnalyzer', function ($c) {
129
            return new EntropyAnalyzer(
130
                $c->query(ILogger::class),
131
                $c->query(IRootFolder::class),
132
                $c->query(Entropy::class),
133
                $c->query('ServerContainer')->getUserSession()->getUser()->getUID()
134
            );
135
        });
136
137
        $container->registerService('FileCorruptionAnalyzer', function ($c) {
138
            return new FileCorruptionAnalyzer(
139
                $c->query(ILogger::class),
140
                $c->query(IRootFolder::class),
141
                $c->query('ServerContainer')->getUserSession()->getUser()->getUID()
142
            );
143
        });
144
145
        $container->registerService('Monitor', function ($c) {
146
            return new Monitor(
147
                $c->query(IRequest::class),
148
                $c->query(IConfig::class),
149
                $c->query(ITimeFactory::class),
150
                $c->query(IAppManager::class),
151
                $c->query(ILogger::class),
152
                $c->query(IRootFolder::class),
153
                $c->query(EntropyAnalyzer::class),
154
                $c->query(FileOperationMapper::class),
155
                $c->query(FileExtensionAnalyzer::class),
156
                $c->query(FileCorruptionAnalyzer::class),
157
                $c->query('ServerContainer')->getUserSession()->getUser()->getUID()
158
            );
159
        });
160
161
        $container->registerService('FilesEvents', function ($c) {
162
            return new FilesEvents(
163
                $c->query(ILogger::class),
164
                $c->query(Monitor::class),
165
                $c->query('ServerContainer')->getUserSession()->getUser()->getUID()
166
            );
167
        });
168
    }
169
170
    /**
171
     * Register hooks.
172
     */
173
    public function register()
174
    {
175
        // register sabre plugin to catch the profind requests
176
        $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
177
        $eventDispatcher->addListener('OCA\DAV\Connector\Sabre::addPlugin', function (SabrePluginEvent $event) {
178
            $logger = $this->getContainer()->query(ILogger::class);
179
            $config = $this->getContainer()->query(IConfig::class);
180
            $userSession = $this->getContainer()->query(IUserSession::class);
181
            $session = $this->getContainer()->query(ISession::class);
182
            $service = $this->getContainer()->query(FileOperationService::class);
183
            $notifications = $this->getContainer()->query(IManager::class);
184
            $classifier = $this->getContainer()->query(Classifier::class);
185
            $sequenceAnalyzer = $this->getContainer()->query(SequenceAnalyzer::class);
186
            $event->getServer()->addPlugin(new RequestPlugin($logger, $config, $userSession, $session, $service, $notifications, $classifier, $sequenceAnalyzer));
187
        });
188
        Util::connectHook('OC_Filesystem', 'post_create', FilesHooks::class, 'onFileCreate');
189
        // Util::connectHook('OC_Filesystem', 'post_update', FilesHooks::class, 'onFileUpdate');
190
        Util::connectHook('OC_Filesystem', 'post_rename', FilesHooks::class, 'onFileRename');
191
        Util::connectHook('OC_Filesystem', 'post_write', FilesHooks::class, 'onFileWrite');
192
        Util::connectHook('OC_Filesystem', 'post_delete', FilesHooks::class, 'onFileDelete');
193
        // Util::connectHook('OC_Filesystem', 'post_touch', FilesHooks::class, 'onFileTouch');
194
        // Util::connectHook('OC_Filesystem', 'post_copy', FilesHooks::class, 'onFileCopy');
195
        $this->registerNotificationNotifier();
196
    }
197
198
    protected function registerNotificationNotifier()
199
    {
200
        $this->getContainer()->getServer()->getNotificationManager()->registerNotifierService(Notifier::class);
201
    }
202
}
203