SequenceSizeAnalyzer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B analyze() 0 26 8
1
<?php
2
3
/**
4
 * @copyright Copyright (c) 2018 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\Analyzer;
23
24
use OCA\RansomwareDetection\Monitor;
25
26
class SequenceSizeAnalyzer
27
{
28
    /**
29
     * Size of information files.
30
     *
31
     * @var int
32
     */
33
    const SIZE_OF_INFO_FILES = 5000000;
34
35
    /**
36
     * Sequence size class.
37
     *
38
     * @var int
39
     */
40
    const EQUAL_SIZE = 1;
41
    const DIFF_SIZE = 2;
42
43
    /**
44
     * Compares the sum of the size of the files written and deleted.
45
     *
46
     * @param array $sequence
47
     *
48
     * @return int
49
     */
50
    public function analyze($sequence)
51
    {
52
        $sizeOfWrittenFiles = 0;
53
        $sizeOfDeletedFiles = 0;
54
55
        foreach ($sequence as $file) {
56
            switch ($file->getCommand()) {
57
                case Monitor::WRITE:
58
                    $sizeOfWrittenFiles = $sizeOfWrittenFiles + $file->getSize();
59
                    break;
60
                case Monitor::READ:
61
                    break;
62
                case Monitor::RENAME:
63
                    break;
64
                case Monitor::DELETE:
65
                    $sizeOfDeletedFiles = $sizeOfDeletedFiles + $file->getSize();
66
                    break;
67
                default:
68
                    break;
69
            }
70
        }
71
72
        if ($sizeOfWrittenFiles <= ($sizeOfDeletedFiles + self::SIZE_OF_INFO_FILES) && $sizeOfWrittenFiles >= $sizeOfDeletedFiles) {
73
            return self::EQUAL_SIZE;
74
        } else {
75
            return self::DIFF_SIZE;
76
        }
77
    }
78
}
79